[NEED HELP] Something wrong with functions from fstream and "cout" them.

I just need to know how to solve my problem. The file is in french, but I will explain you.

Just test this code and enter :
3
Whatever String
Whatever String
Integer
Whatever String
Integer
Whatever String
Integer

Now you can see that the last two entry you entered are popping up two times, and I really don't understand why.

After to close, you can enter 'N' and 9.

I don't need you to judge my code, I know this is not the best way to do what I've done but it was the way needed for my homework. We are just in the beginning of learning C++.

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <string.h>
#include <iomanip>
#include <fstream>

using namespace std;

void RemplirTableau(char tabchar2[], int &nblettre2)
{
	char lettre;
	nblettre2=0;
	fstream fichierlecture("fichierlettres.txt",ios::in);
	if (!fichierlecture)
    {
		cout<<"Impossible d'ouvrir le fichier"<<endl;
    	exit;
	}
	
	fichierlecture>>lettre;
   
    while(!(fichierlecture.eof()))
    {
    	tabchar2[nblettre2]=lettre;
    	nblettre2++;
    	fichierlecture>>lettre;
    }
	fichierlecture.close();
}

void AfficheTableau(char tabchar2[],int nblettre2)
{
	cout<<"{";
	for(int i=0;i<nblettre2;i++)
	{
		cout<<tabchar2[i];
		if(i==nblettre2-1)
		{
			cout<<"}";
		}
		else
		{
			cout<<",";
		}
	}
	cout<<endl<<endl;
}

void Recettes()
{
	string nomrecette,nomingredient,string1,string2;
	int quantiteingredient;
	int lengthstring=0;
	int i;
	char choix;
	
	fstream fichierecriture("recettes.txt", ios::out);
	if (!fichierecriture)
    {
		cout<<"Impossible d'ouvrir le fichier"<<endl;
	}
	
	do
	{
		cout<<endl<<"Entrez le nom de votre recette : ";
		cin>>nomrecette;
		fichierecriture<<"Recette:"<<endl;
		fichierecriture<<nomrecette<<endl;
		fichierecriture<<"Nom"<<endl;
		fichierecriture<<"Quantite"<<endl;
		
		for(i=1;i<4;i++)
		{
			cout<<endl<<"Entrez le nom de l'ingredient "<<i<<" ainsi que sa quantite : "<<endl;
			cout<<"Veuillez entrez au maximum 3 ingredients ou entrez (fin) dans le nom de l'ingredient pour terminer"<<endl;
			cin>>nomingredient;
			if(nomingredient=="fin")
			{
				break;
			}
			fichierecriture<<nomingredient<<endl;
			cin>>quantiteingredient;
			fichierecriture<<quantiteingredient<<endl;
			
			if(nomingredient.length()>lengthstring)
			{
				lengthstring=nomingredient.length();
			}
			
		}

		
		fstream fichierlecture("recettes.txt",ios::in);
		
		cout<<endl;
		
		while(! (fichierlecture.eof() ) )
		{
			fichierlecture>>string1>>string2;
			cout<<setw(lengthstring+3)<<setfill(' ')<<left<<string1<<string2<<endl;
			cout<<setw(lengthstring+11)<<setfill('-')<<"-"<<endl;
		}


		cout<<"Voulez vous entrez une autre recette (O/N) ? ";
		cin>>choix;
		choix=toupper(choix);
		if(choix=='N')
		{
			fichierecriture.close();
			fichierlecture.close();
		}
	}
	while(choix!='N');
	cout<<endl;

}





int main()
{
	char lettre;
	char tabchar[50];
	int nblettre;
	int choix=0;
	bool ended=false;

	while(ended==false)
	{
		cout<<"1 - Remplir le tableau de lettres"<<endl;
		cout<<"2 - Afficher le tableau de lettres"<<endl;
		cout<<"3 - Saisie des recettes"<<endl;
		cout<<"9 - Quitter"<<endl;
		cout<<"Entrez votre choix : ";
		cin>>choix;
	
		switch(choix)
    	{ 
			case 1: RemplirTableau(tabchar,nblettre);
					cout<<"Il y a eu "<<nblettre<<" lettres de placees dans le tableau"<<endl<<endl;
					break;
		    case 2: AfficheTableau(tabchar,nblettre);
		            break;
		    case 3: Recettes();
		            break;
		    case 9: cout<<"Fin du programme";
		    		ended=true;
		    		break;
		    default: cout<<"Choix Invalide"<<endl<<endl;
		    		 break;
		}
	}
	
	
}
Hello PichDereck,

I would say the problem is in line 96. Doing a while condition based on "eof" does not work the way you think it does. What you are seeing is that "eof" is reached inside the while loop, so when the input fails the last read is processed a second time before the while condition can check for "eof".

Generally the file read is done in the while condition, so that when the read fails the while loop fails. Without doing some translation it looks like the while condition would be while (fichierlecture>>string1>>string2) or at least while (fichierlecture>>string1). This way when you try to read past "eof" the while condition will fail.

Just for information line 130 in main while (ended == false) can be written as while (!ended). Both say the same thing and work the same. And the switch in main is easier to read when done like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch (choix)
{
	case 1:
            RemplirTableau(tabchar, nblettre);
	    cout << "Il y a eu " << nblettre << " lettres de placees dans le tableau" << endl << endl;
	    break;
	case 2:
	    AfficheTableau(tabchar, nblettre);
	    break;
	case 3:
	    Recettes();
	    break;
	case 9:
	    cout << "Fin du programme";
	    ended = true;
	    break;
	default:
	    cout << "Choix Invalide" << endl << endl;
	    break;
}


Hope that helps,

Andy
Thanks a lot Andy, you really helped me !!! Thanks for your time I appreciate it :)
Registered users can post here. Sign in or register to post.