string/char input

I want to take input for name of a student ...
when i do char name[20]; and take input with cin>>name; it prints only upto 1st space like if i type "jimmy green" then it will print only "jimmy"

when i take string name; and try to input with getline(cin,name) then it prints the next line before the input like this

here is my code :

#include
#include
using namespace std;

struct student {
string name;
int age;
};

int main() {
int n;
cout<<"Enter the total no. of students : ";
cin>>n;

student s1[n];

for(int i=0;i cout<<"Student "< getline(cin,s1.name);
cout<<"Age : ";
cin>>s1.age;
}
}


And here is the output :


Enter the total no. of students : 3
Student 1 :
Name : Age :


You see that the "Age" is appearing before entering the "name" ... so how can i fix this problem ??????

Replies

  • Neohacker
    Neohacker
    nvm 😛 i added cin.ignore() after cin and now it's working fine .
  • ManojKiran Eda
    ManojKiran Eda
    Another alternative is to use flushall() function after the scanning of an interger.

You are reading an archived discussion.

Related Posts

Yo

Name: Neo Hacker *Engineering Trade: Information Technology Location: India *Occupation: Student *Hobbies & Interests: Only and only video games 😛 *Aim in life: Earn enough money to buy all video...
hello, i have made an inverter using cd4047 as per the circuit diagram i have uploaded here.. as per the circuit diagram gate pulse are given to the mosfet from...
pretty old video by DD (published in Nov 2013) Interested folks can watch it here. The video describes MOM mission, components and activities which the satellite was expected to perform
Sampriti Bhattacharyya, a graduate student in mechanical engineering at the Massachusetts Institute of Technology along with her advisor Professor Harry Asada has developed a football-sized underwater robot that can inspect...
Hi Folks, Can you please tell me which are the best online resources to start with JSP programming with eclipse IDE. 1. Resources should contain the worked examples with explanations...