Dears,
I am facing a strange problem at my end. I am using getline(cin,name) function for reading strings from console. According to my Code Hierarchy i enter data for one user and then the program again asks me to enter the data for the next user.
When my program reads the data for the first time, it takes input correctly but when it asks for the second user's data. The program just bypasses the getline(cin, name) statement as if it is not there.
Kindly advise what is wrong with it. I am sharing the Code as below:
Code:
class testClass
{
private:
int firstvalue;
string name;
char secondvalue;
double thirdvalue;
public:
void settingtestClass();
void print();
testClass();//default constructor
};
int main()
{
testClass one;
while(1){
one.settingtestClass();
cout<<"Output--------------------------------- "<<endl;
one.print();
cout<<"-------------------------------------------"<<endl;
cout<<endl;
}
return 0;
}
testClass::testClass()
{
firstvalue = 0;
thirdvalue = 0.0;
secondvalue = 'a';
name="";
}
void testClass::settingtestClass()
{
cout<<"enter the name"<<endl;
getline(cin,name);
cout<<"enter any value of int data type"<<endl;
cin>>firstvalue;
cout<<"enter any value of character data type "<<endl;
cin>>secondvalue;
cout<<"enter any value of int data type"<<endl;
cin>>thirdvalue;
}
void testClass::print()
{
cout<<"first value = "<<firstvalue<<endl;
cout<<"name = "<<name<<endl;
cout<<"secondvalue = "<<secondvalue<<endl;
cout<<" thirdvalue = "<<thirdvalue<<endl;
}