Save to File problem

I have a project that is suppose to take data for employees, put them into vectors, then allows the user to sort out the data based on the separate inputs (ex: ID, first name, etc.) then save the info to a file once done. So far everything works except for when I go to save it to file, it only creates the file, but not save it. The code is also suppose to check if a file already exists and asks the user if he/she wants to overwrite it. Here is the code:

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
void Save()
{
	char file[100];
	int i = 0;
	char ch;
	fstream openfile;
	
	cout << "Enter file name (be sure to add extension, \".txt\" to the end and use no spaces: ";
	cin >> file;
	
	openfile.open(file, ios::out | ios::in);	//don't create file
	
	if(openfile.is_open())
	{
		cout << "File already exists.  Want to overwrite it?";
		cin >> ch;
		
		switch(ch)
		{	
			case 'y':
				openfile.clear();
				openfile.open(file, ios::out);	//create if necessary
			
				while(i < nID.size())
				{
					openfile << nID[i]
							 << ';'
							 << nfname[i]
							 << ';'
							 << nlname[i]
							 << ';'
							 << nsalary[i]
							 << ';'
							 << nhpw[i]
							 << ';'
							 << nwork[i]
							 << '\n';
					i++;
				}
				cout << "Save complete" << endl << endl;
				return;
				
			case 'n':
				openfile.close();
				cout << "File was not saved." << endl << endl;
				return;
				
			default:
				openfile.close();
				cout << "Invalid input. File was not saved." << endl << endl;
				return;
		}
	}
	
	openfile.open(file, ios::out);
	while(i < nID.size())
	{
		openfile << nID[i]
				 << ';'
				 << nfname[i]
				 << ';'
				 << nlname[i]
				 << ';'
				 << nsalary[i]
				 << ';'
				 << nhpw[i]
				 << ';'
				 << nwork[i]
				 << '\n';
		i++;
	}
	cout << "Save complete" << endl << endl;
}


Any help will be appreciated. Thanks.
Last edited on
I was able to get it to work.
Topic archived. No new replies allowed.