-
while loop problem
Ok, this is another question I hate to even post but here goes.
The program never waits for an input for cin >> newstudent .
I have determined that the "while(cin >> x)" is the problem by commenting this part out, but I don't understand why.
Thanks in advance.
Code:
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
#include<vector>
using namespace std;
struct Student_info
{
string name;
double midterm, final;
vector<double> homework;
};
int main()
{
Student_info record;
string newstudent = "y";
while (newstudent == "y")
{
cout << "Name: ";
cin >> record.name;
cout << "\tMidterm: ";
cin >> record.midterm;
cout << "\tFinal: ";
cin >> record.final;
cout << endl << "Please enter homework grades followed by end-of-file.";
cout << endl;
vector<double> homework;
int x;
while(cin >> x)
record.homework.push_back(x);
ofstream outfile("Student_records.txt");
outfile << record.name << " ";
outfile << record.midterm << " ";
outfile << record.final << " ";
for(int i = 0; i != record.homework.size(); ++i)
outfile << record.homework[i] << " ";
outfile.close();
cout << "Student has been entered into record." << endl;
cout << "Enter another student? (y/n) " << endl;
cin >> newstudent;
}
system("pause");
return 0;
}
-
Well, what happens when someone enters an int?
They have to type some numbers, then press enter, at which point the data is put in the buffer and the numbers extracted. However, the data following the last number, like the newline, are not removed from the input buffer, so the next string read grabs "\n" immediately.
-
Thanks Cat.
I understand your explanation. Great help.
Is there a way to flush the input buffer or is there a better solution? (i'm very new at this if you haven't figured that out yet :) )
I'm looking through other resources in the meantime but figured I'd ask while I've got you here.
-
-
Thanks for the links, but I'm still not getting it to work. I'm honestly not even sure what to ask now. I've tried everyway I can think of using the info in the example you provided with no luck.
Sorry I'm a little slow picking up on this.
Thanks for any more help you can provide.
-
Ok, I'm going to post the code I have so far so somebody can tell me if I'm way off base here. I'm still not able to get it to ask if there are more students. As always any and all help is GREATLY appreciated.
Code:
#include<iostream>
#include<string>
#include<cstdlib>
#include<fstream>
#include<vector>
using namespace std;
struct Student_info
{
string name;
double midterm, final;
vector<double> homework;
};
int main()
{
Student_info record;
string newstudent = "y";
char ch;
while (newstudent == "y")
{
cout << "Name: ";
cin >> record.name;
while ((ch = getchar()) != '\n' && ch != EOF); //flush input
cout << "Midterm: ";
cin >> record.midterm;
while ((ch = getchar()) != '\n' && ch != EOF); //flush input
cout << "Final: ";
cin >> record.final;
while ((ch = getchar()) != '\n' && ch != EOF); //flush input
cout << endl << "Please enter homework grades followed by end-of-file.";
cout << endl;
vector<double> homework;
int x;
while (cin >> x)
{
record.homework.push_back(x);
}
while ((ch = getchar()) != '\n' && ch != EOF); //flush input
ofstream outfile("Student_records.txt");
outfile << record.name << " ";
outfile << record.midterm << " ";
outfile << record.final << " ";
for(int i = 0; i != record.homework.size(); ++i)
outfile << record.homework[i] << " ";
outfile.close();
cout << "Student has been entered into record." << endl;
cout << "Enter another student? (y/n) " << endl;
cin >> newstudent;
}
system("pause");
return 0;
}
-
Simple question: what happens in your code? Tell us what is happening and not only what you wanted it to do.
-
As I have it now, It gets a name, midterm grade, final grade and then homework grades until an EOF is reached. When the user enters and EOF (ctrl z, is this correct?) it then waits until Enter is pressed again. When enter is pressed it then asks for another name, etc.
I want it to ask whether or not there are more students and if yes restart the loop.
I understand what Cat said about the input buffer and have read the links provided by Confuted but obviously I'm still not getting it right.
-
simple
try this:
cin.clear();
cin >> newstudent;
-
Wow that was simple. It works great. Thanks Pratip. Now I'll go read up on that to see exactly what it does. :)
And thanks for all of the other replies too. I learned something new from each one which has lead me on several different tracks trying to figure out other things. Must.....stay....focused.
Again thanks.
-
usually I would just use cin.ignore(1)... all that does is ignore the next 1 character...
-
For some reason the cin.ignore(1) is not working. It still enters the infinite loop. The cin.clear() works perfect, but I still wanted to try your advice. Any idea why this doesn't work?
Also, I was thinking about this and perhaps I don't understand what Cat said as well as I thought. If the problem is caused by a /n in the input buffer because <enter> was pressed in the previous entry, then how am I able to input the name and then input the midterm grade? Why doesn't this second cin statement just take the newline also?