employee database using vectors problem

Pages: 12
figured adding break; before case 3: solved my problem. thank you so much.
Last edited on
okay i have a new problem here. If a user add option 3 a number of times it just add to outFile.
okay i figured it out. here is my snippet:
1
2
3
4
5
6
7
8
9
10
11
case 3:
			if(outFile)
			{
			for(int i=0;i<database.size();i++)
			{
				database[i]->save(outFile);
				cout<<endl;
			}
			outFile.close();
			}
			break;
this problem won't end anywhere i guess. As soon as i choose option 3,option 2 and option 3 i am back to same old situation. What am i doing wrong here??
It would be good to make functions for each case that makes the situation more clear.

I don't know what the old situation is, but I'd suggest in case 3: to open the file -> write -> close the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
case 3:
{ // These braces are necessary otherwise switch won't allow local variabel
	ofstream outFile("out.txt");
			if(outFile)
			{
			for(int i=0;i<database.size();i++)
			{
				database[i]->save(outFile);
				cout<<endl;
			}
			outFile.close();
			}
}
			break;
Remove line 13

[EDIT]

To make sure that the data is always newly written add flags when open the file:

ofstream outFile("out.txt", ofstream::out | ofstream::trunc);

See: http://cplusplus.com/reference/iostream/ofstream/ofstream/

[/EDIT]
Last edited on
coder777 you are one amazing person. You just helped me a lot. You got a blog or something i can follow?? Thank u so much!!
i will see if there are other problems before marking it as solved!!
well, you were on the right track and willing to solve the problem. so not that amazing...
Topic archived. No new replies allowed.
Pages: 12