📜  getline int - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:55.710000             🧑  作者: Mango

代码示例1
#include
#include

int main(){
    std::stringstream ss;

    ss.clear();
    ss.str("");

    std::string input = "";

    int n;

    while (true){
        if (!getline(cin, input))
            return -1;

        ss.str(input);

        if (ss >> n)
            break;

        std::cout << "Invalid number, please try again" << std::endl;

        ss.clear();
        ss.str("");
        input.clear();
}