当输入字符而不是整数时,程序中断 我是C++新手,很难在CIN上找到信息。这是我的第一个程序,非常简单。它确定输入的数字是素数还是复合数,同时过滤掉负数和小数等坏值。在10次输入失败后,程序也将超时
我想添加到这个程序的最后一个功能是过滤掉字符输入的能力,但我没有尝试不同解决方案的运气。我尝试将字符转换为float/double/int,但没有成功。我只需要cin的帮助,如果用户输入字母“K”或任何其他无效字符,我希望程序能够识别,然后生成“无效值”cout消息+新条目,并尝试x次 当前,当输入一个字符时,程序崩溃,同时进行所有10次尝试,使终端中的输出结果非常杂乱当输入字符而不是整数时,程序中断 我是C++新手,很难在CIN上找到信息。这是我的第一个程序,非常简单。它确定输入的数字是素数还是复合数,同时过滤掉负数和小数等坏值。在10次输入失败后,程序也将超时,c++,character,exit,cin,C++,Character,Exit,Cin,我想添加到这个程序的最后一个功能是过滤掉字符输入的能力,但我没有尝试不同解决方案的运气。我尝试将字符转换为float/double/int,但没有成功。我只需要cin的帮助,如果用户输入字母“K”或任何其他无效字符,我希望程序能够识别,然后生成“无效值”cout消息+新条目,并尝试x次 当前,当输入一个字符时,程序崩溃,同时进行所有10次尝试,使终端中的输出结果非常杂乱 #include <iostream> #include <cmath> using namespa
#include <iostream>
#include <cmath>
using namespace std ;
int main (void)
{
//Declare variables
int y, c, i1 ;
double i ;
//Ask for user input
cout << endl << endl << "Please enter a number: " ;
cin >> i ;
//Eliminate bad user input values
i1 = i ;
//Detect when a decimal number is entered for the first attempt
for ( c = 1 ; int(i1) != i ; c ++ )
{
cout << endl << "This value is invalid. Please retry: " ;
cin >> i ;
i1 = i ;
//Terminate program after so many failed attempts
if ( 8 < c )
{
cout << endl << "This value is invalid. Try again next time! \n\n" ;
return 0 ;
}
//Detect if a '0' is entered on the latter attempts
for ( c ; i < 1 ; c ++ )
{
cout << endl << "This value is invalid. Please retry: " ;
cin >> i ;
i1 = i ;
//Terminate program after so many failed attempts
if ( 8 < c )
{
cout << endl << "This value is invalid. Try again next time! \n\n" ;
return 0 ;
}
}
}
//Detect when '0' is entered on the first attempt
for ( c = 1 ; i < 1 ; c ++ )
{
cout << endl << "This value is invalid. Please retry: " ;
cin >> i ;
i1 = i ;
//Terminate program after so many failed attempts
if ( 8 < c )
{
cout << endl << "This value is invalid. Try again next time! \n\n" ;
return 0 ;
}
//Detect if a decimal number is entered on the latter attempts
for ( c ; int(i1) != i ; c ++ )
{
cout << endl << "This value is invalid. Please retry: " ;
cin >> i ;
i1 = i ;
//Terminate program after so many failed attempts
if ( 8 < c )
{
cout << endl << "This value is invalid. Try again next time! \n\n" ;
return 0 ;
}
}
}
//Find prime numbers
for ( y = 1 ; y <= i ; y ++ )
{
//Give instant result if user input is no. 1
if ( i == 1 )
{
cout << endl << "The number: " << i << " is a composite number.\n\n" ;
break ;
}
//Eliminate no. 1 from prime number search
if ( y == 1 )
continue ;
//Search for a whole number division using modulus
if ( fmod(i, y) == 0 )
{
if ( i == y )
cout << endl << "The number: " << i << " is a prime number.\n\n" ;
else if ( i != y )
{
cout << endl << "The number: " << i << " is a composite number.\n\n" ;
break ;
}
}
}
return 0 ;
}
#包括
#包括
使用名称空间std;
内部主(空)
{
//声明变量
int y,c,i1;
双i;
//请求用户输入
cout我终于找到了答案。我只需要使用这个循环。尽管在尝试x次失败后的超时方面仍有很多需要改进的地方。我将不得不将这个循环嵌套到整个其他代码组中
#include <limits>
//Detect character input and refuse
while( 1 == 1 )
{
if ( cin.fail() )
{
cin.clear () ;
cin.ignore (numeric_limits <streamsize> :: max(), '\n' ) ;
cout << endl << "This value is invalid. Please retry: " ;
cin >> i ;
}
if ( !cin.fail() )
break ;
}
#包括
//检测字符输入和拒绝
而(1==1)
{
if(cin.fail())
{
cin.clear();
cin.ignore(数值限制::max(),'\n');
我不能;
}
如果(!cin.fail())
打破
}
代码> int main(空)<代码>为什么C++程序中的C样式函数声明:总是检查IO是否成功。<代码> CIN > i;可能在没有给您任何工作的情况下失败。您不需要检查这个情况。{清除流错误标志并删除错误输入}
注意:double
值是不精确的。例如,您认为是5的值实际上可能表示为4.99999997,这使得在要求完全相等的操作中很难使用它们。对于空参数列表显式声明void
,这是当今最常见的样式问题。如果参数列表为空,显然,没有任何参数是必需的。你没有任何的能力。这样做的能力是为了让C++编译器更容易解析C代码,这是相对容易做的事情,但是随着C和C++的进一步漂移,变得越来越难。“如果程序不返回值,则将void放在括号之间”这根本不是这里的情况。main
返回int
,而不管您的void
。您正在混淆函数参数和返回值。