employee database using vectors problem

Pages: 12
Aug 21, 2012 at 11:16pm
figured adding break; before case 3: solved my problem. thank you so much.
Last edited on Aug 21, 2012 at 11:16pm
Aug 22, 2012 at 12:57am
okay i have a new problem here. If a user add option 3 a number of times it just add to outFile.
Aug 22, 2012 at 1:38am
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;
Aug 22, 2012 at 10:22am
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??
Aug 22, 2012 at 10:51am
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 Aug 22, 2012 at 11:31am
Aug 22, 2012 at 11:27am
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!!
Aug 22, 2012 at 4:48pm
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