Question on getline c++

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: "


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <stdlib.h>        
using namespace 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;
}






Last edited on
Consider this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <limits>

using namespace std;

int
main()
{
    int i;
    string str;
    cout << "Enter integer: ";
    cin >> i;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Enter string: ";
    getline(cin, str);

    cout << "integer is " << i << ". string is " << str << '\n';
    return 0;
}


Thanks a lot. <3333333
Topic archived. No new replies allowed.