How do I check if input is an integer/string?

前端 未结 6 1309
一个人的身影
一个人的身影 2021-01-01 07:33

My problem here is I don\'t know how to insert a rule wherein if a user inputted a number on the string, it will cout a warning saying it\'s not valid, same wit

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 08:27

    you could use cin.fail() method! When cin fails it will be true and you could us a while loop to loop until the cin is true:

    cin>>d;
    while(cin.fail()) {
        cout << "Error: Enter an integer number!"<> d;
    }
    

提交回复
热议问题