[英]Check if input is a correct integer
我在检查输入内容时遇到了一些问题。 我知道这里已经存在类似的问题,但是诚恳地,这并不能解决我的问题。 我已经在使用其他问题的建议之一了,它不起作用。
#include <iostream>
#include <stdio.h>
#include <ctype.h>
int main ()
{
int a;
printf("Please type in your number: ");
scanf_s("%d", &a);
if(isdigit(a))
{
printf("True.");
}
else
{
printf("False.");
}
std::cin.get();
std::cin.get();
return 0;
}
我不知道自己在做什么错,但是当我输入数字时,我的输出始终为“ False”。 当我输入字母时,编译器出现错误。
a
已经是一个整数变量。 std::isdigit
仅检查字符是否对应于数字,即字符'0'
至'9'
。
尽量不要将C库( printf
, scanf
等)与C ++库( cout
和cin
)混合使用。 坚持任一个。
至于如何实际检查用户是否输入了数字, 请参阅此答案 。
您没有正确使用isdigit
函数。
即使isdigit
接收一个int
作为其唯一参数,它也希望char
值(表示ASCII字母的字节/数字)不是您现在正在使用的纯int
。
IE浏览器:
is_digit('1'); // true
is_digit('a'); // false
scanf
已经返回一个已解析的整数值,因此您无需检查它是否为数字。
也许如果您指定要完成的任务,我们可以提供更多帮助。
尽管isdigit()
将int
作为参数,但它会检查变量的字符值。 这个:
scanf_s("%d", &a);
给您一个整数值,而不是一个字符值。
您正在使用带有%d的scanf
,它将把数字转换为整数,但是isdigit
旨在用于尚未转换的原始字符。 如果使用它,您(可能)想读取一个字符串,然后检查输入的所有字符是否都是数字(带有isdigit
),只有在这样做之后,您才尝试转换为整数。
或者,您可以使用诸如strtol
东西将字符串转换为整数,并告诉您失败的地方(以及是否失败)。
如果需要在c中执行此操作,请遵循已经发布的其他ppl的答案。
如果您想用c ++做到这一点,我建议:
int num = boost::lexical_cast<int>( somestring );
以及关于boost :: lexical_cast的文档
isdigit接受字符,并根据字符是否为数字返回真或假。 因此,您可以在整数的字符串表示形式的每个字符上使用此字符,但是您的“ a”已经是由scanf_s设置的整数
scanf_s只会将输入的前几位转换为等效的整数,然后将停止扫描。 相反,您需要处理输入的实际字符,因此您可能想要fgets之类的东西。
有了字符串后,您可以遍历字符串中的每个字符,以检查它是否是数字(或者您可以使用诸如strtol之类的库函数为您完成此操作,然后返回整数值,然后检查该字符串是否消耗了所有输入它这样做(拒绝像123X这样的东西)
我建议将一行读入字符串,然后尝试根据需要解析该字符串。 如果解析失败,只需再次提示用户。 您可以将杂乱的细节埋在函数模板中:
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
T read(std::string prompt)
{
for (; ;)
{
std::cout << prompt;
std::string line;
getline(std::cin, line);
std::istringstream ss(line);
T x;
if ((ss >> x) && (ss >> std::ws).eof()) return x;
}
}
int main ()
{
int a = read<int>("Please type in your number: ");
std::cout << "You entered " << a << '\n';
}
不要使用scanf
和家人。 使用fgets
然后使用strtol
/ strtoul
/ strtol
解析数字。
如果您确实确定要使用scanf,则可以通过检查scanf的返回值来验证转换是否发生。 它返回成功处理的格式项的数量,如果错误则返回-1。 在您的情况下,成功输入应从scanf返回1。
这是更符合C ++精神的另一种方法(尽管我不是C ++专家,所以可能有更好的方法来做到这一点):
#include <iostream>
#include <exception>
#include <cctype>
int main()
{
using namespace std;
int a;
cout << "Please type in your number: " << flush;
/**
* Tell the input operation to throw an exception if the input
* is not properly formatted for the target data type; note that
* this will only catch bad input that *starts* with a non-numeric
* character.
*/
cin.exceptions(ios_base::failbit);
try
{
cin >> a;
if (!isspace(cin.get())
cout << "false" << endl; // non-numeric, non-whitespace character found
// at end of input string, e.g. "12w"
else
cout << "true" << endl;
}
catch(ios_base::failure& e) // non-numeric, non-whitespace character found
{ // at beginning of input string, e.g. "x23"
cout << "false" << endl;
}
return 0;
}
现在,我找到了适合我的解决方案,但是仍然存在一些问题。 这是我修改的代码:
#include <iostream>
#include <stdio.h>
#include <ctype.h>
int main ()
{
int a;
printf("Please type in your number: ");
if(scanf_s("%d", &a) == 1)
{
printf("True.");
}
else
{
printf("False.");
}
std::cin.get();
std::cin.get();
return 0;
}
现在的问题是,我必须为输入的每个字母添加一个“ std :: cin.get()”。这太疯狂了。 因此,当我要检查“ hello”是否为数字时,我必须总共放入7倍的“ std :: cin.get()”来保持屏幕,以便我可以看到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.