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;
}

Do you get any errors? Because syntax wise, it looks okay to me..

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.

commented: fast and straight-to-the-point answer +1

Alright I understand it, thanks a lot guys.

ncm, answered my own question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.