Writing Data input by user to test file.

Hi, Im writing a function to store data inputted by a user. I have to press enter twice after first name and after adress fro the program to print out the next line. whats wrong, how should i fix it? Also, it does not write the Cutomer Id number to the text file. I have the following code already:

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
void Customer::Add()
{

	string fname;
	char middle;
	string lname;
	string trn;
	string address;
	string tele;
	string email;
	string custid;
	int d, d1, m, m1, y, y1;
	bool mem;

	cout<<"*********** ADD CUSTOMER ************\n"<<endl;
	cout<<"Please enter the iformation requested"<<endl;

	cout<<"Enter New Customer ID Number: ";
	getline(cin, custid, '\n');
	cin.ignore(10, '\n');

	cout<<"First Name: ";
	getline(cin, fname, '\n');
	cin.ignore(15, '\n');

	cout<<"Middle Initial: ";
	cin>>middle;

	cout<<"Last Name: ";
	getline(cin, lname, '\n');
	cin.ignore(25, '\n');

	cout<<"Enter Year of Membership: ";
	cin>>y;
	DateofMem.setyear(y);

	cout<<"TRN: ";
	cin>>trn;
	
	cout<<"Address: ";
	getline(cin, address, '\n');
	cin.ignore(500, '\n');
	
	cout<<"Telephone Number: ";
	getline(cin, tele, '\n');
	cin.ignore(15, '\n');
	
	cout<<"Email Address: ";
	getline(cin, email, '\n');
	cin.ignore(50, '\n');

	ofstream outCustomerFile;
	outCustomerFile.open("Customer.txt", ios::out);

	if( !outCustomerFile)
	{
		cerr<<"File could not be opened"<<endl;
		exit(1);
	}

	//Customer::setMember(true);

	outCustomerFile<<"Customer Id Number: "<<custid<<endl;
	outCustomerFile<<"First Name: "<<fname<<endl;
	outCustomerFile<<"Middle Name: "<<middle<<endl;
	outCustomerFile<<"Last Name: "<<lname<<endl;
	outCustomerFile<<"TRN Number: "<<trn<<endl;
	outCustomerFile<<"Address: "<<address<<endl;
	outCustomerFile<<"Telephone: "<<tele<<endl;
	outCustomerFile<<"Email Address: "<<email<<endl;

}
Last edited on
cin.ignore is the problem. It is used to discard characters from the stream, commonly used after using cin>> because it leaves a newline in the stream. However, in the cases where you use getline, the stream is empty so cin.ignore waits until it sees a newline or n characters entered (where n is the first parameter). So, the solution is to remove the calls to cin.ignore whenever you use getline, and to use cin.ignore after each call to cin>>.

For the matter of the text file, you don't need to provide the parameter ios::out because that's already implied by using ofstream. I have no idea why it wouldn't write the cutomer ID to the text file. Do all the other values get written to the text file correctly?
Yes all the others write.
Shacktar, Thanks so much!!!!!1 it works perfectly now.... all i have to do is write the other functions now, delete record update record and display record! lol not easy for me, but i gotta get it done!! thanks alot.

kimmi
So, the customer ID problem was solved as well?
Topic archived. No new replies allowed.