Trouble saving a struct to a binary file

Hello all I've been at this for a good long 4 hours now and see nothing wrong with my code...I am still a bit of a newbie so any help would be appreciated.

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
	struct customerInfo
{
	string user;
	string pass;
	string credit;
	string mail;
	string movieOut;
	bool oneOut;
};

	customerInfo nCustomer;


NewCustomer::NewCustomer(string userName,string password,string checkPassword,string creditCard,string eMail)
{
	nCustomer.user=userName;
	nCustomer.pass=password;
	while(nCustomer.pass!=checkPassword)
	{
		system("cls");
		cout<<"Your passwords do not match please re-enter the password\n"
			<<"password: ";
		getline(cin,nCustomer.pass);
		cout<<endl;
		cout<<"Please re-enter your password\n"
			<<"password: ";
		getline(cin,checkPassword);
	}
	nCustomer.credit=creditCard;
	nCustomer.mail=eMail;
	nCustomer.oneOut=false;
}
void addToDataBase()
{
	ofstream inFile ("customerData.dat",ios::app|ios::binary);
	if (!inFile.fail())
	{
		cout<<"cannot write to file\n"
			<<endl;
		system("pause");
		inFile.close();
	}
	else
	{
		inFile.seekp((long)0,ios::beg);
		int count=1;
		while(!inFile.eof())
		{
			inFile.seekp((long (sizeof (customerInfo))*count),ios::beg);
			count++;
		}
		inFile.write((char*)&nCustomer,sizeof (customerInfo));
		inFile.close();
		cout<<"Welcome to Joe's DVD Shed!\n";
	}
}


This is the code i have so far in a class called NewCustomer, when i compile it with out the addToDataBase method it compiles fine so I know the problem is in there this is my first program saving to a binary file so i'm pretty sure thats what i'm doing wrong.
So what happens when you run the program? Does it crash or something? Does customerInfo a class? Maybe it contains string object that cause it to fail.

[edit]

Maybe this would help.
http://cplusplus.com/forum/beginner/23163/#msg121662
Last edited on
Topic archived. No new replies allowed.