我想创建一个程序,该程序从用户接收整数输入,然后在用户根本不输入任何内容时终止(即,只需按Enter键).但是,我在验证输入时遇到了问题(确保用户输入的是整数,而不是字符串.atoi()将不起作用,因为整数输入可以超过一位数.
验证此输入的最佳方法是什么?我尝试了类似下面的内容,但我不知道如何完成它:
char input
while( cin>>input != '\n')
{
//some way to check if input is a valid number
while(!inputIsNumeric)
{
cin>>input;
}
}
Run Code Online (Sandbox Code Playgroud) c++ ×1