whenever i use cin.get in a loop it skips over the first cin.get. for example, in the code below, the programs skips over the cin.get for the stu.name??


struct student
	char name[20];
	char ssn[20];
	char dob[20];
	float gpa;

	student stu;
	ofstream outfile;
	char choice;

	outfile.open("h:/name.dat", ios::out);

		cout << "What's your name?" <<endl;
	cin.get(stu.name, 20);//**skips this line in the 2nd loop???
	cin.ignore(80, '\n');

	cout << "What's your GPA?" <<endl;
	cin >> stu.gpa;
	cout << "What's your date of birth?" <<endl;
	cin.get(stu.dob, 20);

	cout << "What's your social security number?" <<endl;
	cout << "xxx-xx-xxx" <<endl;
	cin.get(stu.ssn, 20);
	cin.ignore(80, '\n');


	cout << " " <<endl;
	cout << " " <<endl;

	cout << stu.name <<endl;
	cout << stu.ssn <<endl;
	cout << stu.dob <<endl;
	cout << stu.gpa <<endl;


	if (outfile)
		outfile << stu.name <<endl;
		outfile << stu.ssn <<endl;
		outfile << stu.dob <<endl;
		outfile << stu.gpa <<endl;
		cout << "An error has occured while opening the file." <<endl;
	cout << "Enter q to quit" <<endl;
	cin >> choice;
	while(choice != 'q');

	return 0;

<< moderator edit: added [code][/code] tags >>

Re: cin.get issue 80 80

all the cin.get's work on the first loop but on subsequent loops it skips over the first cin.get??

Re: cin.get issue 80 80

Perhaps cin.ignore the newline following the Enter q to quit prompt. And probably after the GPA, too.

Re: cin.get issue 80 80

That fixed it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.