2010-11-08 111 views
-1

我在檢查輸入時遇到了一些問題。我知道那裏已經有類似的問題,但是它並沒有解決我的問題。我已經在使用其他問題的建議之一,但它不起作用。檢查輸入是否是正確的整數

#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; 

} 

我不知道我做錯了,但我的輸出始終是「假」當我在一個號碼類型。當我輸入一個字母時,編譯器有錯誤。

+4

你爲什麼要標記這個C? – GManNickG 2010-11-08 20:49:32

+0

[我如何在C++中驗證用戶輸入爲double?](http://stackoverflow.com/questions/3273993/how-do-i-validate-user-input-as-a-double-in -c)和其他許多... – 2010-11-08 20:54:38

+0

當你嘗試'48'時會發生什麼? :-) – pmg 2010-11-08 21:03:24

回答

3
  1. a已經是一個整數變量。 std::isdigit僅檢查字符是否對應於數字,即字符'0''9'

  2. 儘量不要混合使用C++庫(coutcin)C庫(printfscanf等)。堅持任何一個。

  3. 至於如何實際檢查用戶是否輸入了號碼,see this answer

2

您正在使用isdigit功能不正確。

即使isdigit收到int作爲其唯一的參數,它需要一個char值(字節/代表ASCII字母數字)不是一個純粹的int,你現在正在使用它。

即:

is_digit('1'); // true 
is_digit('a'); // false 

scanf將已返回解析整數值,所以你並不需要檢查它是否是一個數字或沒有。

也許如果你指定了你正在努力完成什麼,我們可以幫助更多。

+0

好的,但是當我輸入一個字母時scanf會做什麼?我的編譯器什麼都不做它只是停止。所以在這種情況下,必須有一種方法來要求正確的輸入。 – Ordo 2010-11-09 06:51:05

2

儘管isdigit()int作爲參數,但它會檢查字符字符的值。這:

scanf_s("%d", &a); 

給你一個整數值,而不是一個字符值。

2

您使用scanf與%d,這將數字(S)轉換爲整數,但isdigit旨在與尚未轉換尚未原始字符工作。如果你使用它,你可能會(可能)想要讀取一個字符串,然後檢查輸入的所有字符是否都是數字(isdigit),並且只有在完成後纔會嘗試轉換爲整數。

或者,您可以使用類似strtol的字符串將字符串轉換爲整數,並告訴您它在哪裏(以及如果)失敗。

1

isdigit接受一個字符並根據字符是否是數字返回true或false。所以你可以在你的整數的字符串表示的每個字符上使用它,但是你的'a'已經是一個整數,由scanf_s設置scanf_s只會將輸入的第一個數字轉換爲等效的整數,然後將停止掃描。你需要處理輸入的實際字符,所以你可能需要像fgets這樣的東西。

一旦你有字符串,你可以遍歷每一個字符,檢查它是否是一個數字(或者你可以使用像strtol這樣的庫函數爲你做這件事,並返回整數值,然後檢查它消耗所有的輸入,當它這樣做(拒絕像123X這樣的東西)

1

我建議讀一行到一個字符串,然後試圖根據您的需要解析該字符串如果解析失敗,只需再次提示用戶。可以將函數模板中的雜亂的細節埋入:

#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'; 
} 
2

如果您需要在c中執行此操作,請遵循以下答案o其他人已經發布。

如果你想這樣做,在C++中,我建議:

int num = boost::lexical_cast<int>(somestring); 

而且在boost::lexical_cast

1

的文件,不要使用scanf和家人。使用fgets然後strtol/strtoul /等。解析數字。

1

如果您真的決定使用scanf,則可以通過檢查scanf的返回值來驗證轉換是否發生。它返回成功處理的格式項的數量,或者返回-1。在你的情況下,成功的輸入應該從scanf返回1。

http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

+0

謝謝,這對我很有用。 – Ordo 2010-11-09 07:02:38

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; 
} 
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()」中的每個字母i鍵入這是瘋了。所以,當我想檢查「你好」是否是一個數字,我必須把7x「std :: cin.get()」的總量放在屏幕上,這樣我才能看到結果。