-1

I have the following simple program, but the last line of code getline(cin, topicClass) is never excuted. However, if I use normal cin>>topicClass that is executed. Could you please help me out of this? Thanks

#include <iostream>
#include <string>
using namespace std;

void InfoInput()
{
    string classID;
    string teacherName;
    int totalStudent;
    string topicClass;
    cout<<"Enter class ID"<<endl;
    getline(cin, classID);
    cout<<"Enter teacher's name"<<endl;
    getline(cin, teacherName);
    cout<<"Enter total number of students"<<endl;
    cin>>totalStudent;
    cout<<"Enter topic of class"<<endl;
    getline(cin, topicClass);
    //cin>>topicClass;
}

int main ()
{

    InfoInput();
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
hn.phuong
  • 835
  • 6
  • 15
  • 24
  • 2
    Possible duplicate of http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input – Algirdas Preidžius Feb 03 '16 at 23:02
  • Possible, nothing. That's a "Nailed it!" @AlgirdasPreidžius – user4581301 Feb 03 '16 at 23:08
  • How did you determine the `getline` was never executed? – David Schwartz Feb 03 '16 at 23:44
  • Off topic: if `cin>>totalStudent;` is not provided an integer (even a horribly impossible integer than can't fit in an `int`) `cin` will be put in an error state that is not cleared, probably killing your program dead. You need to check the state of `cin`, or any other stream, after each read to make sure data was read and to clean up the mess if it wasn't. – user4581301 Feb 04 '16 at 00:06

3 Answers3

2

Your problem is above, with this line:

cin>>totalStudent;

This does not read a line. You enter your input and (I assume) you press ENTER. The \n remains in the buffer of std::cin and is read as an empty line with your next instruction:

getline(cin, topicClass);

To fix, use this code:

cin>>totalStudent;
while(std::isspace(cin.peek())
    cin.ignore();
cout<<"Enter topic of class"<<endl;
getline(cin, topicClass);
utnapistim
  • 26,809
  • 3
  • 46
  • 82
0

There is \n remained in cin after reading totalStudent as integer so you need to get that \n first out of the system and then read the next string

#include <iostream>
#include <string>
using namespace std;

void InfoInput()
{
    string classID;
    string teacherName;
    int totalStudent;
    string topicClass;
    cout<<"Enter class ID"<<endl;
    getline(cin, classID);
    cout<<"Enter teacher's name"<<endl;
    getline(cin, teacherName);
    cout<<"Enter total number of students"<<endl;
    cin>>totalStudent;
    cout<<"Enter topic of class"<<endl;
    getline(cin,topicClass);
    getline(cin, topicClass);
    //cin>>topicClass;
}

int main ()
{

   InfoInput();

}
Pooya
  • 6,083
  • 3
  • 23
  • 43
0

Your classID and teacherName are local variables and disappear after execution leaves the function. You'll have to figure out some way, like passing parameters by reference, to share the variables between functions.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154