Trouble with obtaining strings from console.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Result::Input() {
    system("cls");
    std::cout<<"ENTER STUDENT'S ROLL. NO.: ";
    std::cin>>rollno;
    std::cout<<"ENTER STUDENT'S NAME: ";
    gets(name);
    std::cout<<"ENTER MARKS IN ENGLISH: ";
    std::cin>>eng;
    std::cout<<"ENTER MARKS IN MATHSEMATICS: ";
    std::cin>>mat;
    std::cout<<"ENTER MARKS IN SCIENCE: ";
    std::cin>>sci;
    Compute();
}


In the above code, it skips right through
1
2
std::cout<<"ENTER STUDENT'S NAME: ";
    gets(name);

I've also tried
cin.getline(name, 20);
Any suggestions?
you need to catch the newline.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Result::Input() {
    system("cls");
    std::cout<<"ENTER STUDENT'S ROLL. NO.: ";
    std::cin>>rollno;
    std::cin.ignore();
    std::cout<<"ENTER STUDENT'S NAME: ";
    std::getline(std::cin, name);
    std::cout<<"ENTER MARKS IN ENGLISH: ";
    std::cin>>eng;
    std::cin.ignore();
    std::cout<<"ENTER MARKS IN MATHSEMATICS: ";
    std::cin>>mat;
    std::cin.ignore();
    std::cout<<"ENTER MARKS IN SCIENCE: ";
    std::cin>>sci;
    std::cin.ignore();
    Compute();
}
Last edited on
Topic archived. No new replies allowed.