Whenever i input something before the getline. It skips my Enter Student Name: and continues to Enter School Name: Why is that?
it would just show "Enter Student Name: Enter School Name: "
#include <iostream>
#include <string>
#include <stdlib.h>
usingnamespace std;
struct StudentProfile
{
string student;
string school;
string gender;
int seatnumber;
} Student [25];
void display(int count)
{
cout<<Student[count].seatnumber<<".) ";
cout<<Student[count].student<<endl;
}
void seat(int count)
{
Student[count].seatnumber=count+1;
display(count);
}
int main ()
{
char choose;
cout<<"-----------------------Classroom Seat Assignment Program-----------------------";
cout<<"Program Description:"<<endl;
cout<<"The program takes 25 students, each with a name, school, and gender."<<endl;
cout<<"The program will not allow two students with the same school and gender to seat";
cout<<"side-by-side. The program will then display the results by row. "<<endl<<endl;
cout<<" *SEAT PLAN*"<<endl;
cout<<" 1ST ROW 1 2 3 4 5"<<endl;
cout<<" 2ND ROW 6 7 8 9 10"<<endl;
cout<<" 3RD ROW 11 12 13 14 15"<<endl;
cout<<" 4TH ROW 16 17 18 19 20"<<endl;
cout<<" 5Th ROW 21 22 23 24 25"<<endl<<endl;
cout<<"Do you want to start the program (y) or (n):";
cin>>choose;
if (choose=='y' || choose=='Y')
{
system("cls");
cout<<"-----------------------Classroom Seat Assignment Program-----------------------"<<endl;
}
else
{
system("cls");
cout<<" Goodbye!"<<endl<<endl;
return 0;
}
int counter=-1;
do
{
counter++;
cout << "Enter Student Name: ";
getline (cin,Student[counter].student);
cout << "Enter Student School: ";
getline (cin,Student[counter].school);
cout << "Enter Student Gender(Male/Female): ";
getline (cin,Student[counter].gender);
cout<<endl;
if (Student[counter].school==Student[counter-1].school )
{
cout<<"Your input is invalid, please input again"<<endl;
cout<<"Remember: No two students with the same school should be sitting side-by-side"<<endl;
cout<<" No two students with the same gender (M) will be able to sit side-by-side"<<endl;
counter--;
}
if (Student[counter].gender!="Male" && Student[counter].gender!="Female")
{
cout<<"Your input is invalid, please input again"<<endl;
cout<<"Remember: Follow correct spelling given"<<endl;
counter--;
}
if (Student[counter].gender=="Male" && Student[counter-1].gender=="Male")
{
cout<<"Your input is invalid, please input again"<<endl;
cout<<"Remember: No two students with the same school should be sitting side-by-side"<<endl;
cout<<" No two students with the same gender (Male) will be able to sit side-by-side"<<endl;
counter--;
}
}
while(counter<24);
cout<<"1ST ROW"<<endl;
for(int m=0; m<5; m++)
{
seat(m);
cout<<" "<<Student[m].school<<endl;
cout<<" "<<Student[m].gender<<endl;
}
cout<<endl;
cout<<"2ND ROW"<<endl;
for(int m=5; m<10; m++)
{
seat(m);
cout<<" "<<Student[m].school<<endl;
cout<<" "<<Student[m].gender<<endl;
}
cout<<endl;
cout<<"3RD ROW"<<endl;
for(int m=10; m<15; m++)
{
seat(m);
cout<<" "<<Student[m].school<<endl;
cout<<" "<<Student[m].gender<<endl;
}
cout<<endl;
cout<<"4TH ROW"<<endl;
for(int m=15; m<20; m++)
{
seat(m);
cout<<" "<<Student[m].school<<endl;
cout<<" "<<Student[m].gender<<endl;
}
cout<<endl;
cout<<"5TH ROW"<<endl;
for(int m=20; m<25; m++)
{
seat(m);
cout<<" "<<Student[m].school<<endl;
cout<<" "<<Student[m].gender<<endl;
}
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int
main()
{
int i;
string str;
cout << "Enter integer: ";
cin >> i;
cout << "Enter string: ";
getline(cin, str);
cout << "integer is " << i << ". string is " << str << '\n';
return 0;
}
When you run it, it prompts for an integer, then prints "Enter string:" and doesn't wait for you to enter a line. The reason is that there is already a line in the input buffer. The line is the newline you typed after entering your integer (and anything else before the newline). To see this, run the program and give the input
5 Look here is the line
Enter integer: 5 Look here is the line
Enter string: integer is 5. string is Look here is the line
To fix this, you have to ignore anything after the "5" through to the newline: