Problem on using fflush(stdin)

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.

Thanks You.
Last edited on
Source code pls
Question: fflush( stdin );
Answer: Don't do that.

(Honestly, it is bad, immoral, wrong, and just plain undefined [behaviorly].)

This stickied thread is dedicated to that very problem:
http://www.cplusplus.com/forum/beginner/1988/

This Article thread also addresses it:
http://www.cplusplus.com/forum/articles/6046/

Hope this helps.
The following is my source code

void insertStu()
{
string val;
string name;
string address;
string type;
string year;
char gender('\0');
int yob;
int num;

num = colleage.getCurrentStudent();
cout << "Student Number: "<< num+1 <<endl<<endl;//generated the student number

do{
cout <<"Name:";
fflush(stdin);
getline(cin,name,'\n');

//validation for student name
if (name.length()>20)
cout << "(Error)-Name cannot more than 20 character!\n";

}while(name.length()>20);

do{
cout << "Address: ";
fflush(stdin);
getline(cin,address,'\n');


//validaton for student address
if (address.length()>100)
cout << "(Error)-Address cannot more than 100 character!\n";

}while(address.length()>100);

do{
cout << "Gender(f/m): ";
fflush(stdin);
getline(cin,val,'\n');

//validation for gender
if (val!="m" && val!="f")
cout << "(Error)-Gender only allow f/m!\n";
else
gender = val[0];

}while(val!="m" && val!="f");

do{
cout << "Year of Birth: ";
fflush(stdin);
getline(cin,val,'\n');
stringstream(val) >> yob ;

//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";

}while(yob<1900 || yob>2007);

do{
cout << "Student Type(i-in,o-out): ";
fflush(stdin);
getline(cin,val,'\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');

if( val!="y" && val!="n")
cout << "(Error-invalid input!)\n";

}while(val!="y" && 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.
Hi Duoas,

I know this is not a good way, but coz i have try to no put the fflush(stdin) but it can't work well, it will only save the name in to address.

If u have an alternative way that can help me on this, can you please share with me?

Thanks You.
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:
1
2
3
4
5
6
7
#include <ciso646>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>

using namespace std;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  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!
    else break;
    }


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.

Hope this helps.


[edit] BTW, please use [code] [/code] tags.
Last edited on
Hi Duoas,

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.

Thanks You.

#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>
using namespace std;

class Student
{

private:
int student_no;
string student_name;
string student_add;
char student_gender;
int student_year_of_birth;
string student_type;


public:

Student();

void setStudentNumber(int stuNo){student_no = stuNo;};
void setStudentName(string name){student_name=name;};
void setStudentAddress(string address){student_add = address;};
void setStudentGender(char gender){student_gender = gender;};
void setStudentYearOfBirth(int yob){student_year_of_birth= yob;};
void setStudentType(string type){student_type = type;};

char getStudentNumber(){return student_no;};
string getStudentName(){return student_name;};
string getStudentAddress(){return student_add;};
char getStudentGender(){return student_gender;};
int getStudentYearOfBirth(){return student_year_of_birth;};
string getStudentType(){return student_type;};

};


Student::Student()
{

student_no = 0;
student_name="";
student_add = "";
student_gender = '\0';
student_year_of_birth = 0;
student_type = "";
}


class Colleage
{

private:
Student * PtrArr;
int currentStudent;

public:
Colleage();

void setPtrArr(){ PtrArr = PtrArr + 1;};
void setCurrentStudent(int num){currentStudent = num;};

Student *getPtrArr(){return PtrArr;};
int getCurrentStudent(){return currentStudent;};

};


Colleage::Colleage()
{
Student arrStudent[3000];
PtrArr = arrStudent;
currentStudent =0;
}

Colleage colleage;
//function for insert new srudent
void insertStu()
{
string val;
string name;
string address;
string type;
string year;
char gender('\0');
int yob;
int num;

num = colleage.getCurrentStudent();
cout << "Student Number: "<< num+1 <<endl<<endl;//generated the student number

do{
cout <<"Name:";
fflush(stdin);
getline(cin,name,'\n');

//validation for student name
if (name.length()>20)
cout << "(Error)-Name cannot more than 20 character!\n"

}while(name.length()>20);

do{
cout << "Address: ";
fflush(stdin);
getline(cin,address,'\n');

//validaton for student address
if (address.length()>100)
cout << "(Error)-Address cannot more than 100 character!\n";

}while(address.length()>100);

do{
cout << "Gender(f/m): ";
fflush(stdin);
getline(cin,val,'\n');

//validation for gender
if (val!="m" && val!="f")
cout << "(Error)-Gender only allow f/m!\n";
else
gender = val[0];

}while(val!="m" && val!="f");

do{
cout << "Year of Birth: ";
fflush(stdin);
getline(cin,val,'\n');
stringstream(val) >> yob ;

//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";

}while(yob<1900 || yob>2007);

do{
cout << "Student Type(i-in,o-out): ";
fflush(stdin);
getline(cin,val,'\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');

if( val!="y" && val!="n")
cout << "(Error-invalid input!)\n";

}while(val!="y" && 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()
{

Student *sortStudent[3000], *ptrStudent;
Student tempArr;
string type;

ptrStudent = colleage.getPtrArr();

for(int num=0; num <colleage.getCurrentStudent(); num++)
{
ptrStudent -- ;
sortStudent[num] = ptrStudent;
}

for(int i = 0; i <colleage.getCurrentStudent() ; i++)
{
for (int j=0; j<colleage.getCurrentStudent()-1;j++)
{

if(sortStudent[j]->getStudentName()>sortStudent[j+1]->getStudentName())
{
ptrStudent = sortStudent[j];
sortStudent[j]=sortStudent[j+1];
sortStudent[j+1] = ptrStudent;
}
}
}

cout << "\nFull-time student\n(male)";
for(int x =0 ; x <colleage.getCurrentStudent() ; x++)
{
if (sortStudent[x]->getStudentType() =="in" &&
sortStudent[x]->getStudentGender()=='m')
{
cout << "\n\t\tName: " << sortStudent[x]->getStudentName();
cout << "\n\t\tStudent Number: " <<
int(sortStudent[x]->getStudentNumber()) ;
cout << "\n\t\tAddress: "<< sortStudent[x]->getStudentAddress();
cout << "\n\t\tGender: " << sortStudent[x]->getStudentGender() ;
cout << "\n\t\tYear of Birth: "<<
int(sortStudent[x]->getStudentYearOfBirth());
cout << "\n\t\tStudent Type: " << sortStudent[x]->getStudentType() ;
cout << endl;
}

}

cout << "\n\nFull-time student\n(female)";
for(int x1 =0 ; x1 < colleage.getCurrentStudent() ; x1++)
{
if (sortStudent[x1]->getStudentType() =="in" &&
sortStudent[x1]->getStudentGender()=='f')
{
cout << "\n\t\tName: " << sortStudent[x1]->getStudentName();
cout << "\n\t\tStudent Number: " <<
int(sortStudent[x1]->getStudentNumber()) ;
cout << "\n\t\tAddress: "<< sortStudent[x1]->getStudentAddress();
cout << "\n\t\tGender: " << sortStudent[x1]->getStudentGender() ;
cout << "\n\t\tYear of Birth: "<<
int(sortStudent[x1]->getStudentYearOfBirth());
cout << "\n\t\tStudent Type: " << sortStudent[x1]->getStudentType()
;
cout << endl;
}

}

cout << "\n\nPart-time student\n(male)";
for(int x2 =0 ; x2 <colleage.getCurrentStudent() ; x2++)
{
if (sortStudent[x2]->getStudentType()== "out" &&
sortStudent[x2]->getStudentGender()=='m')
{
cout << "\n\t\tName: " << sortStudent[x2]->getStudentName();
cout << "\n\t\tStudent Number: " <<
int(sortStudent[x2]->getStudentNumber()-1) ;
cout << "\n\t\tAddress: "<< sortStudent[x2]->getStudentAddress();
cout << "\n\t\tGender: " << sortStudent[x2]->getStudentGender() ;
cout << "\n\t\tYear of Birth: "<<
int(sortStudent[x2]->getStudentYearOfBirth());
cout << "\n\t\tStudent Type: " << sortStudent[x2]->getStudentType()
;
cout << endl;
}

}

cout << "\n\nPart-time student\n(female)";

for(int x3 =0 ; x3 <colleage.getCurrentStudent() ; x3++)
{
if (sortStudent[x3]->getStudentType() == "out" &&
sortStudent[x3]->getStudentGender()=='f')
{
cout << "\n\t\tName: " << sortStudent[x3]->getStudentName();
cout << "\n\t\tStudent Number: " <<
int(sortStudent[x3]->getStudentNumber()-1) ;
cout << "\n\t\tAddress: "<< sortStudent[x3]->getStudentAddress();
cout << "\n\t\tGender: " << sortStudent[x3]->getStudentGender() ;
cout << "\n\t\tYear of Birth: "<<
int(sortStudent[x3]->getStudentYearOfBirth());
cout << "\n\t\tStudent Type: " << sortStudent[x3]->getStudentType()
;
cout << endl;
}

}

cout << "\t\tPlease press any key to continue.";
getch();

}

int errMain=0;

int main()
{
system("CLS");
char entry;

//main menu
cout << "\t\tMenu" << endl;
cout << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << "(1) Insert student" << endl;
cout << "(3) Student report of type and gender" << endl;
cout << "(0) Exit" << endl;

if (errMain==1)
{
cout << "\n(Error)-Pelase enter the valid option!!";
errMain=0;
}

cout << "\nPlaease select one option: ";
cin >> entry;

switch (entry)
{
case '1' :
system("CLS");
cout << "Insert Student" << endl;
cout << "-=-=-=-=-=-=-=-" << endl;
insertStu();
break;

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;

case '0' :
exit(0);
break;
default:
errMain = 1;

}

main();
return 0;
}
1
2
3
4
5
6
Colleage::Colleage()
{
Student arrStudent[3000];//start...
PtrArr = arrStudent;
currentStudent =0;
}//end... 

Where is the memory dear :)
use some thing global like new delete...

Andy...
Topic archived. No new replies allowed.