[英]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.