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 >>

Recommended Answers

All 3 Replies

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

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

That fixed it :)

Be a part of the DaniWeb community

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