0
int main(){ 
    string name, StudentNumber, Marks;
    cout << "Enter name:\n";
    cin >> name;
    cout << "Enter space separated class marks:\n";
    getline(cin, Marks);
    cout << "\n";
    cout << Marks;
}

When I run the above code, the getline(cin, Marks) statement is skipped and program terminates i.e user cannot enter the class marks. I tried modifying the code slightly:

int main(){ 
    string name, Marks;
    cout << "Enter name:\n";
    cin >> name;
    cout << "Enter space separated class marks:\n";
    cin >> Marks;
    getline(cin, Marks);
    cout << "\n";
    cout << Marks;
}

This is the output I'm getting:

Enter name:
John
Enter space separated class marks:
78 76 54 79

 76 54 79

The first mark (78) has not been added to the variable Marks. Could someone please help explain why this is happening? I am a novice in C++.

craig-nerd
  • 569
  • 1
  • 6
  • 22

1 Answers1

-2

write like this

    int main(){ 
        string name, Marks;
        cout << "Enter name:\n";
        cin >> name;
        cout << "Enter space separated class marks:\n";
getline(cin, Marks);
    cout << "\n";
    cout << Marks;