我输入了这个,它要求用户输入两个整数,然后变成变量。从那里它将执行简单的操作。
如何让计算机检查输入的内容是否为整数?如果没有,请让用户键入一个整数。例如:如果有人输入“a”而不是2,那么它会告诉他们重新输入一个数字。
谢谢
#include <iostream>
using namespace std;
int main ()
{
int firstvariable;
int secondvariable;
float float1;
float float2;
cout << "Please enter two integers and then press Enter:" << endl;
cin >> firstvariable;
cin >> secondvariable;
cout << "Time for some simple mathematical operations:\n" << endl;
cout << "The sum:\n " << firstvariable << "+" << secondvariable
<<"="<< firstvariable + secondvariable << "\n " << endl;
}
你可以这样检查:
int x;
cin >> x;
if (cin.fail()) {
//Not an int.
}
此外,您可以继续获取输入,直到获得int via:
#include <iostream>
int main() {
int x;
std::cin >> x;
while(std::cin.fail()) {
std::cout << "Error" << std::endl;
std::cin.clear();
std::cin.ignore(256,'\n');
std::cin >> x;
}
std::cout << x << std::endl;
return 0;
}
编辑:要解决以下关于10abc等输入的注释,可以修改循环以接受字符串作为输入。然后检查字符串中是否有任何字符而不是数字并相应地处理该情况。在那种情况下,人们不需要清除/忽略输入流。验证字符串只是数字,将字符串转换回整数。我的意思是,这只是袖口。可能有更好的方法。如果你接受浮点数/双打(在搜索字符串中必须添加'。'),这将不起作用。
#include <iostream>
#include <string>
int main() {
std::string theInput;
int inputAsInt;
std::getline(std::cin, theInput);
while(std::cin.fail() || std::cin.eof() || theInput.find_first_not_of("0123456789") != std::string::npos) {
std::cout << "Error" << std::endl;
if( theInput.find_first_not_of("0123456789") == std::string::npos) {
std::cin.clear();
std::cin.ignore(256,'\n');
}
std::getline(std::cin, theInput);
}
std::string::size_type st;
inputAsInt = std::stoi(theInput,&st);
std::cout << inputAsInt << std::endl;
return 0;
}
c中有一个叫做isdigit()
的函数。这很适合你。例:
int var1 = 'h';
int var2 = '2';
if( isdigit(var1) )
{
printf("var1 = |%c| is a digit\n", var1 );
}
else
{
printf("var1 = |%c| is not a digit\n", var1 );
}
if( isdigit(var2) )
{
printf("var2 = |%c| is a digit\n", var2 );
}
else
{
printf("var2 = |%c| is not a digit\n", var2 );
}
来自here
如果istream无法插入,则会设置失败位。
int i = 0;
std::cin >> i; // type a and press enter
if (std::cin.fail())
{
std::cout << "I failed, try again ..." << std::endl
std::cin.clear(); // reset the failed state
}
您可以在do-while循环中设置它以获得正确插入的正确类型(在本例中为int
)。
有关更多信息:http://augustcouncil.com/~tgibson/tutorial/iotips.html#directly
嘿,这是一个老问题,可以使用更好的答案。
用户输入应作为字符串获取,然后尝试转换为您想要的数据类型。方便的是,这也允许您回答诸如“我的输入是什么类型的数据?”之类的问题。
这是我经常使用的一个功能。存在其他选项,例如在Boost中,但基本前提是相同的:尝试执行字符串→类型转换并观察成功或失败:
template <typename T>
std::optional <T> string_to( const std::string& s )
{
std::istringstream ss( s );
T result;
ss >> result >> std::ws; // attempt the conversion
if (ss.eof()) return result; // success
return {}; // failure
}
使用optional
类型只是一种方式。您还可以抛出异常或在失败时返回默认值。什么适合你的情况。
以下是使用它的示例:
int n;
std::cout << "n? ";
{
std::string s;
getline( std::cin, s );
auto x = string_to <int> ( s );
if (!x) return complain();
n = *x;
}
std::cout << "Multiply that by seven to get " << (7 * n) << ".\n";
限制和类型识别
当然,为了实现这一点,必须存在一种从流中明确提取数据类型的方法。这是C ++中事物的自然顺序 - 也就是说,照常运行。所以这里没有惊喜。
下一个警告是某些类型包含其他类型。例如,如果你试图区分int
和double
,首先检查int
,因为任何转换为int
的东西也是double
。
您可以使用变量名称本身来检查值是否为整数。例如:
#include <iostream>
using namespace std;
int main (){
int firstvariable;
int secondvariable;
float float1;
float float2;
cout << "Please enter two integers and then press Enter:" << endl;
cin >> firstvariable;
cin >> secondvariable;
if(firstvariable && secondvariable){
cout << "Time for some simple mathematical operations:\n" << endl;
cout << "The sum:\n " << firstvariable << "+" << secondvariable
<<"="<< firstvariable + secondvariable << "\n " << endl;
}else{
cout << "\n[ERROR\tINVALID INPUT]\n";
return 1;
}
return 0;
}
你可以使用:
int a = 12;
if (a>0 || a<0){
cout << "Your text"<<endl;
}
我很确定它有效。