Error in perfomance of cin function!!!

Mar 5, 2016 at 10:28am
#include<iostream>
using namespace std;

class profile{
public:
int age,name,gender;
char ocuupation[30];
};

int main(){
profile pro1;
cout<<"Lets create a profile."<<endl;
cout<<"Enter your name\t :"<<flush;
cin>>pro1.name;
cout<<"Enter your age\t :"<<flush;
cin>>pro1.age;

return(0);

}




Here, the cin(user input) works in the case of pro1.name in the above mentioned program but in the case of pro1.age it doesn't work. Whats wrong with it??? What's the solution to this problrm?? Plz help...
Last edited on Mar 5, 2016 at 10:37am
Mar 5, 2016 at 11:05am
after u type the pro1.name and press enter the "enter" remains in the stream which is take by the next cin

add cin.ignore('\n',200); or flush the stream after every input

it will work :P
Mar 5, 2016 at 1:39pm
1
2
3
4
5
class profile{
public:
    int age,name,gender;
    char ocuupation[30];
};

Here name is of type int. Unless the person's name is something like 78541 or 1234 that will probably not be suitable.

Maybe this would be better:
1
2
3
4
5
6
7
class profile{
public:
    int age;
    char name[30]; 
    char gender;
    char occupation[30];
};


At this stage, you only need to be concerned about using cin.ignore() when mixing getline() with formatted input cin >>

@Frooster you have the parameters of ignore() in the wrong order.
Last edited on Mar 5, 2016 at 1:58pm
Mar 6, 2016 at 6:05am
Chervil oh mb @Chervil! Thanks!!!
Mar 7, 2016 at 12:44pm
Thank u guys : )
Topic archived. No new replies allowed.