C++ iostream: Using cin >> var and getline(cin, var) input errors

前端 未结 5 1041
旧时难觅i
旧时难觅i 2020-12-06 23:11

I\'m creating a simple console application in C++ that gets string and char inputs from the user. To make things simple, I would like to use the string and

5条回答
  •  我在风中等你
    2020-12-06 23:29

    std::cin.ignore() will ignore the first character of your input.

    For your case, use std::cin.ignore() after std::cin and then getline() to ignore newline character as:

    cin>>ch;
    cin.ignore();  //to skip the newline character in the buffer
    getline(cin,var);
    

提交回复
热议问题