I have write a program that required to enter a list of data, and then store in array in order to display later. when i write the code to insert data into array, i use fflush(stdin) after i get the data, but i found that usually we just need to enter once of the enter key in order to continue, but after i include fflush(stdin) i need to enter two times enter key in order to continue the next.
Can anyone know how to solve this giving me the idea.
//validation for year of birth
if (yob < 0)
cout << "(Error)-Yob is invalid!\n";
else
if(yob<1900 || yob>2007)
cout << "(Error)-Yob only between 1900 - 2007!\n";
//validation for student type
if (val!="i"&& val!="o")
cout << "(Error)-Student type only allow i/o!\n";
else
{
if( val=="i"||val=="I")
type= "in";
else
type= "out";
}
}while(val!="i" && val!="o" );
//confirmation message for saving the record
do{
cout << "Do you want to save the record(y/n)?";
fflush(stdin);
getline(cin,val,'\n');
//display the saved record when user enter "y"
if(val=="y")
{
colleage.getPtrArr()->setStudentNumber(++num);
colleage.getPtrArr()->setStudentName(name);
colleage.getPtrArr()->setStudentAddress(address);
colleage.getPtrArr()->setStudentGender(gender);
colleage.getPtrArr()->setStudentYearOfBirth(yob);
colleage.getPtrArr()->setStudentType(type);
colleage.setPtrArr() ;
colleage.setCurrentStudent(num);
cout << "\n\t*** SAVED !!!! ***\n";
}
cout << "Please press any key to continue.";
getch();
}
Please help me on this, coz i am beginner, so no idea on this.
Please keep in mind that this is the end of the year, so it might take a few days for people to respond. I cannot run your code because it is incomplete, but looking through it it seems you have the right ideas to begin with.
You don't need fflush(stdin) or any of that other fluff (like getch()).
You have actually already organized your input the correct way: read by lines then use a stringstream to collect data out of the line. For example, your year-of-birth. If I may make a simple suggestion, cheat and stick the escape condition in one spot:
string val;
int yob;
while (true)
{
// Ask the user for information
cout << "Year of Birth: " << flush;
// Get it as a complete line of text
getline( cin, val );
// Endeavor to convert it to an integer value
if (!(stringstream( val ) >> yob)
or (yob < 1900)
or (yob > 2007))
{
cout << "(Error)-Yob must be a number between 1900 and 2007 inclusive!\n";
}
// Success! yob is a valid number!
elsebreak;
}
Having input everything as a complete line of text, there is no need to worry about whether or not the input pointer is at the proper place. It is.
I know end of year all is busy, so i will patient to wait who can help me on this. Thanks a lot !!!! The following reply is my whole code. Actually i have try to dun use fflush(stdin), but it really can't work correctly. It will get the name input store as address, then address input is no store at all. So please help me to have the right way to run this program.
//validation for year of birth
if (yob < 0)
cout << "(Error)-Yob is invalid!\n";
else
if(yob<1900 || yob>2007)
cout << "(Error)-Yob only between 1900 - 2007!\n";
//validation for student type
if (val!="i"&& val!="o")
cout << "(Error)-Student type only allow i/o!\n";
else
{
if( val=="i"||val=="I")
type= "in";
else
type= "out";
}
}while(val!="i" && val!="o" );
//confirmation message for saving the record
do{
cout << "Do you want to save the record(y/n)?";
fflush(stdin);
getline(cin,val,'\n');
//display the saved record when user enter "y"
if(val=="y")
{
colleage.getPtrArr()->setStudentNumber(++num);
colleage.getPtrArr()->setStudentName(name);
colleage.getPtrArr()->setStudentAddress(address);
colleage.getPtrArr()->setStudentGender(gender);
colleage.getPtrArr()->setStudentYearOfBirth(yob);
colleage.getPtrArr()->setStudentType(type);
colleage.setPtrArr() ;
colleage.setCurrentStudent(num);
cout << "\n\t*** SAVED !!!! ***\n";
}
cout << "Please press any key to continue.";
getch();
}
//display report of type and gender
void report1()
{
case '3' :
system("CLS");
cout << "Student report of type and gender" << endl;
cout << "-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-" << endl;
if (colleage.getCurrentStudent()==0)
{
cout << "\nThere is no any student record!!";
cout << "\n\nPlease press any key to continue.";
getch();
}
else
report1();
break;