0

Hey guys, I've got a beginner question about this

for some reason the getline function will not work, does anyone know why? I can't seem to figure it out

int main ()
{
        string studentname;
        int test1, test2;

        cout << "Enter first test score";
        cin >> test1;
        cout << "Enter second test score";
        cin >> test2;

        cout << "Enter student name ";
        getline (cin, studentname);

        cout << studentname << "'s average is " << (test1 + test2) / 2.0 << endl;
        return 0;
}
4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by imput1234
1

add cin.ignore(); after the use of cin before the use of getline, from where you use cin it still holds \n from when you pressed enter so when you use getline the 1st thing it reads is what were last there, which because of the cin earlier happens to be enter.

Votes + Comments
fast and straight-to-the-point answer
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.