Need help with a project! I/O streaming

Hello there everyone. First of all i am new to the forums. I have been reading this forum since last year and i like it. I need a help with a project that i have to deliver for the university. The project is supposed to open accounts, close accounts, save information to a file, read from file etc. The whole idea is that the user is asked to open a file. Then the file should be created. He can choose to do some operations such as open account, close account, rent movie and more. And when the quit function is used the information are stored into that file. I have almost finished my project but i need some help. I cant figure out why a file isnt created. I run it in visual studio 2010,i click start without debugging and it opens, the operations work just fine but a file isnt created. Will post my code in 2 posts because it wont fit in the text ;p Can some1 help me please?
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
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;

	struct openaccount
	{
		string name;
		string lastname;
		string phonenumber;
		string address;
		string city;
		int postcode;
		int accountnumber;
		double balance;
		double movies;
	};

	void openacc(openaccount[],int&);
	void closeacc(openaccount[],int&);
	void deposit(openaccount[],int&);
	void printresult(openaccount[],int&);
	void rentmovie(openaccount[],int&);
	void returnmovie(openaccount[],int&);
	void accountinfo(openaccount[],int&);
	void searchforaccount(openaccount[],int&);
	double quit(openaccount[],int&,char&,ofstream&);
int main()
{
	const int size=100;
	char choice='d';
	char response;
	string filename;
	ifstream infile;
	ofstream outfile;
	openaccount information[size];
	int personcount=0;


	cout<<"Enter file to open: "<<endl;
	cin>>filename;
	cout<<filename.c_str();
		infile.open(filename.c_str());

		if(!infile.fail()){
			cout<<"File exists. Overwrite (y or n)?"<<endl;
			cin>>response;
		}
		else
			cout<<"The was created."<<endl;
		
		
		if('n'==tolower(response))
		{
			cout<<"Goodbye"<<endl;
			return 1;
		}
		
	while(choice!='q')
	{
	
	cout<<"o: Open account"<<endl;
cout<<"c: Close account"<<endl;
cout<<"d: deposit"<<endl;
cout<<"r: Rent Movie"<<endl;
cout<<"t: Return Movie"<<endl;
cout<<"a: Account info"<<endl;
cout<<"p: Print all accounts"<<endl;
cout<<"s: Search for account"<<endl;
cout<<"q: quit"<<endl;
cout<<"Enter operation"<<endl;
cin>>choice;
cin.ignore();
//do{
	//cin>>choice;
	//cin.ignore();
	
//}
//while (choice!='o'||choice!='c'||choice!='d'||choice!='r'||choice!='t'||choice!='a'||choice!='p'||choice!='s'||choice!='q');


switch(choice)
	{
	case 'o':
		openacc(information,personcount);
		break;
	case 'c':
		closeacc(information,personcount);
	case 'd':
		deposit(information,personcount);
		break;
	case 'p':
		printresult(information,personcount);
		break;
	case 'r':
		rentmovie(information,personcount);
		break;
	case 't':
		returnmovie(information,personcount);
		break;
	case 'a':
		accountinfo(information,personcount);
		break;
	case 's':
		searchforaccount(information,personcount);
		break;
	case 'q':
		quit(information,personcount,choice,outfile);
		break;
	}
	}
	
	return 0;

}



Last edited on
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
void openacc(openaccount information[],int&personcount)
{
	static int i=100;
	cout<<"Enter name"<<endl;
	getline(cin,information[personcount].name);
	cout<<"Enter surname"<<endl;
	getline(cin,information[personcount].lastname);
	cout<<"Enter Phone Number"<<endl;
	getline(cin,information[personcount].phonenumber);
	cout<<"Enter address"<<endl;
	getline(cin,information[personcount].address);
	cout<<"Enter city"<<endl;
	getline(cin,information[personcount].city);
	cout<<"Enter postcode"<<endl;
	cin>>information[personcount].postcode;
	
	information[personcount].accountnumber=i++;
	cout<<"The account number is: "<<information[personcount].accountnumber;
	information[personcount].balance=0;
	information[personcount].movies=0;
	personcount++;
}
void closeacc(openaccount information[],int&personcount)
{
	int accnumber;
	int i;
	cout<<"Enter account number"<<endl;
	cin>>accnumber;

	for(i=0;i<personcount;i++)
	{
	if(accnumber==information[i].accountnumber)
	{
	if(information[i].movies!=0)
		cout<<"You cannot close this account because its has not returned all the movies"<<endl;
		else if(information[i].movies==0)
	{
				information[i].accountnumber=-1;
				information[i].address="";
				information[i].balance=0;
				information[i].city="";
				information[i].lastname="";
				information[i].movies=0;
				information[i].name="";
				information[i].phonenumber="";
				information[i].postcode=0;
}
}
}
}
					
			
void deposit(openaccount information[],int&personcount)
{
	int accnumber;
	cout<<"Enter account number: "<<endl;
	cin>>accnumber;
	int i=0;
	for(i=0;i<personcount;i++)
	{
		if(accnumber==information[i].accountnumber)
		{
			double depositamount;
			cout<<"Enter amount to deposit"<<endl;
	        cin>>depositamount;
			information[i].balance+=depositamount;
			cout<<depositamount<<"Euro have been deposited to the account"<<information[i].accountnumber<<endl;
		}
	}
}
void printresult(openaccount information [],int&personcount)
{
	int i=0;
	for(i=0;i<personcount;i++)
	{
		if(information[i].accountnumber!=-1)
		{
		cout<<information[i].name<<endl;
		cout<<information[i].lastname<<endl;
		cout<<information[i].address<<endl;
		cout<<information[i].city<<endl;
		cout<<information[i].postcode<<endl;
		cout<<information[i].phonenumber<<endl;
		cout<<information[i].accountnumber<<endl;
		cout<<information[i].balance<<endl;
		cout<<information[i].movies<<endl;
	}
}
}
void rentmovie(openaccount information[],int&personcount)
{
	int accnumber;
	cout<<"Enter account number"<<endl;
	cin>>accnumber;
	
	for(int i=0;i<personcount;i++)
	{
		if(accnumber==information[i].accountnumber)
		{
	if(information[i].balance>0)
	{
	information[i].movies+=1;
	cout<<"You have rented "<<information[i].movies<<" movies"<<endl;
	cout<<"You account balance is: "<<(information[i].balance-=1)<<endl;
		}
	else if(information[i].balance<=0)
		cout<<"Your account balance is less than 0"<<endl;
	}
}
}
void returnmovie(openaccount information[],int&personcount)
{ 
	int accnumber;
int i=0;
int returnmoviesamount;
	cout<<"Enter account number"<<endl;
	cin>>accnumber;
	for(i=0;i<personcount;i++)
	{
	if(accnumber==information[i].accountnumber)
	{
	cout<<"How many movies would you like to return?"<<endl;
	cin>>returnmoviesamount;
	information[i].movies=information[i].movies-returnmoviesamount;
	cout<<"You have returned "<<returnmoviesamount<<" movies"<<endl;
	cout<<"Your current number of movies is: "<<information[i].movies<<endl;
	}
	}
}
	void accountinfo(openaccount information[],int&personcount)
	{
		int accnumber;
		int i=0;
		cout<<"Enter account number"<<endl;
		cin>>accnumber;
		for(i=0;i<personcount;i++)
		{
			if(accnumber==information[i].accountnumber)
			{
				cout<<"Account "<<accnumber<<" belongs to "<<information[i].name<<" "<<information[i].lastname<<endl;
				cout<<information[i].address<<" "<<information[i].postcode<<" "<<information[i].city<<endl;
				cout<<"Phone Number: "<<information[i].phonenumber<<endl;
				cout<<"Current Balance: "<<information[i].balance<<" Euro"<<endl;
				cout<<"Current Number of movies: "<<information[i].movies<<endl;
			}
		}
	}
	
	void searchforaccount(openaccount information[],int&personcount)
	{
		int searchoption;
		cout<<"Enter search option"<<endl;
		cout<<"1:last name"<<endl;
		cout<<"2:phone number"<<endl;
		cin>>searchoption;
		cin.ignore();
		if(searchoption==1)
		{

			string lastnamez;
			cout<<"Enter last name"<<endl;
			getline(cin,lastnamez);
	
			int i=0;
			for (i=0;i<personcount;i++)
			{
				if (lastnamez.compare(information[i].lastname)==0)
				{
					cout<<"Account "<<information[i].accountnumber<<" belongs to "<<information[i].name<<" "<<information[i].lastname<<endl;
				cout<<information[i].address<<" "<<information[i].postcode<<" "<<information[i].city<<endl;
				cout<<"Phone Number: "<<information[i].phonenumber<<endl;
				cout<<"Current Balance: "<<information[i].balance<<" Euro"<<endl;
				cout<<"Current Number of movies: "<<information[i].movies<<endl;
				}
			
			}
		}
		else if (searchoption==2)
		{
			
			string telephonenumber;
			cout<<"Enter telephone number"<<endl;
			getline(cin,telephonenumber);
			
			int counter;
			for(counter=0;counter<personcount;counter++)
			{
				if ((telephonenumber.compare(information[counter].phonenumber)==0))
				{
				cout<<"Account "<<information[counter].accountnumber<<" belongs to "<<information[counter].name<<" "<<information[counter].lastname<<endl;
				cout<<information[counter].address<<" "<<information[counter].postcode<<" "<<information[counter].city<<endl;
				cout<<"Phone Number: "<<information[counter].phonenumber<<endl;
				cout<<"Current Balance: "<<information[counter].balance<<" Euro"<<endl;
				cout<<"Current Number of movies: "<<information[counter].movies<<endl;
				}
				
			}
		}
		}
	double quit(openaccount information[],int&personcount,char&choice,ofstream& outFile)
	{
		ifstream infile;
		ofstream outfile;
		char svtofile;
		cout<<"Do you want to save these information to a file? (y/n)"<<endl;
		cin>>svtofile;
		if(svtofile=='y')
		{
			string svfilename;
			cout<<"Enter filename"<<endl;
			cin>>svfilename;
			cout<<"Account info saved to"<<svfilename.c_str();
			infile.open(svfilename.c_str());
		int i=0;
		for(i=0;i<personcount;i++)
		{
			outFile<<information[i].name<<endl;
		outFile<<information[i].lastname<<endl;
		outFile<<information[i].address<<endl;
		outFile<<information[i].city<<endl;
		outFile<<information[i].postcode<<endl;
		outFile<<information[i].phonenumber<<endl;
		outFile<<information[i].accountnumber<<endl;
		outFile<<information[i].balance<<endl;
		outFile<<information[i].movies;
			outfile.close();
		}
	
	
	}
		else
			cout<<"Good Bye"<<endl;
		return choice;
	}
	
First of all, welcome to this forum. Secondly, in the second post line no 213, isn't that supposed to be outfile.open(svfilename.c_str()); ??
Ah yes i think so. But my teacher told me there should be a file created. If i do that will it create a file?
yes i did change it right now. The file IS created finally but there is no data in it. Any1 can help?
@Alexcyp, What are you trying to do man?? (Sorry if I am being rude, but your code wasted my good 2 hours)

You are passing an ofstream reference "outFile" (Note the capital F) to your code, and then you have another ofstream object "outfile" (note the small f). I know that you have used the later one "outfile" and opened the file
i.e. outfile.open(). But you are doing file i/o using outFile.open(). Zeez!! Replace the outfile.open() with outFile.open() and your program should work just fine.

But this is not your fault too. (sorry if I sounded mean). These types of bugs are the hardest to find and debug. Hope your program runs fine and you get a good grade.

changed it like this.
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
double quit(openaccount information[],int&personcount,char&choice,ofstream& outFile)
	{
		ifstream infile;
		ofstream outfile;
		char svtofile;
		cout<<"Do you want to save these information to a file? (y/n)"<<endl;
		cin>>svtofile;
		if(svtofile=='y')
		{
			string svfilename;
			cout<<"Enter filename"<<endl;
			cin>>svfilename;
			cout<<"Account info saved to"<<svfilename.c_str();
			outFile.open(svfilename.c_str());
		int i=0;
		for(i=0;i<personcount;i++)
		{
			outFile<<information[i].name<<endl;
		outFile<<information[i].lastname<<endl;
		outFile<<information[i].address<<endl;
		outFile<<information[i].city<<endl;
		outFile<<information[i].postcode<<endl;
		outFile<<information[i].phonenumber<<endl;
		outFile<<information[i].accountnumber<<endl;
		outFile<<information[i].balance<<endl;
		outFile<<information[i].movies;
			outFile.close();
		}
	


Is that what you mean? Kinda got confused mate. I noticed now that information are written on the file but only for the first account that i open. The program is also supposed not to overwrite the data each time is run. I AM SO CONFUSED here. Can you please see if this is ok so far?
You are closing the file inside the for loop
1
2
3
4
5
6
7
8
9
10
11
12
13
for(i=0;i<personcount;i++)
		{
			outFile<<information[i].name<<endl;
		outFile<<information[i].lastname<<endl;
		outFile<<information[i].address<<endl;
		outFile<<information[i].city<<endl;
		outFile<<information[i].postcode<<endl;
		outFile<<information[i].phonenumber<<endl;
		outFile<<information[i].accountnumber<<endl;
		outFile<<information[i].balance<<endl;
		outFile<<information[i].movies;
			outFile.close();
		}


Try moving it after the loop. You're also passing an output stream to the function and creating one inside the function which is unnecessary and confusing.
Last edited on
if i moved it after the loop it will only print the one at the "I" position. I am using the loop so it can print all the info since i got an array of structs. Lets say i got 10 accounts, the loop is supposed to outfile all those 10 accounts
Yes, but you close the file after the first account is output.
Sorry for the late reply..

you are supposed to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(i=0;i<personcount;i++)
		{
			outFile<<information[i].name<<endl;
		outFile<<information[i].lastname<<endl;
		outFile<<information[i].address<<endl;
		outFile<<information[i].city<<endl;
		outFile<<information[i].postcode<<endl;
		outFile<<information[i].phonenumber<<endl;
		outFile<<information[i].accountnumber<<endl;
		outFile<<information[i].balance<<endl;
		outFile<<information[i].movies;
		//outFile.close();    Move this downward
		}
               outFile.close();


Topic archived. No new replies allowed.