2013-10-15 44 views
1

例如:截斷的整數的所有數字,除了前兩個

int input; 
cout << "Please enter how many burgers you'd like" << endl; 
cin >> input; 

什麼是縮短「輸入」和只接受前兩個 數字的最簡單的方法。 繼續該示例:

用戶輸入:432534. 值輸入的:43.

用戶輸入:12342342341234123412341235450596459863045896045. 值輸入的:12.

(編輯:說 '數字'代替「位」)

+10

那些不是位,它們是數字。 – chris

+2

你想要發生什麼與剩餘的數字?他們應該被扔掉嗎?如果用戶輸入「-123」怎麼辦?應該是'-1'還是'-12'? – BoBTFish

+0

我剛編輯過這個問題。對不起,@chris,您的評論現在看起來毫不相關! –

回答

3

閱讀前兩個數字,並形成一個整數:

int digit1 = std::cin.get() - '0'; 
int digit2 = std::cin.get() - '0'; 
int input = digit1 * 10 + digit2; 

然後丟棄輸入的休息:如果用戶輸入任何

int input = 1; 

int digit1 = std::cin.get(); 
if (digit1 == '-') { 
    input = -1; 
    digit1 = std::cin.get(); 
} 

digit1 -= '0'; 
int digit2 = std::cin.get() - '0'; 
input *= (digit1 * 10) + digit2; 

如下評論說,這不起作用:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

要處理一個負號,你可以做這樣的事情除了兩個數字作爲前兩個字符。這很容易通過閱讀和使用std::isdigit進行檢查來檢查。這取決於你繼續或拋出某種錯誤。

如果用戶只輸入一個數字,這也不起作用。如果您還需要這樣做,則可以讀取整個字符串並使用其大小或檢查EOF。

對輸入操作本身也沒有錯誤檢查,但應該有實際的代碼。

+2

評價的順序是否有保證? – Dirk

+0

@Dirk,好的,謝謝。 – chris

+2

用戶輸入單個數字時不起作用。 – zch

0

input作爲串並轉換一日2個字符作爲int

#include <sstream> 
#include <string> 
#include<iostream> 

int main() 
{ 
    std::string input ; 

    std::cin>>input; 
    std::stringstream iss(input.substr(0,2)); 
    int num; 

    iss >> num; 

    std::cout<<num; 

} 
+0

istringstream iss(input.substr(0,2))''有什麼問題? – IInspectable

+0

@IInspectable是啊它沒有出現在我的腦海,因爲第一次想到,謝謝,更新 – P0W

4

我覺得std::string操作可以送你回家。

std::string inputstr; 
cout << "Please enter how many burgers you'd like" << endl; 
cin >> inputstr; 
inputstr = inputstr.substr(0, 2); 

int input  
input = std::stoi(inputstr);  // C++11 
input = atoi(inputstr.cstr()); // pre-C++11 

文檔:
http://en.cppreference.com/w/cpp/string/basic_string/stol
http://en.cppreference.com/w/cpp/string/byte/atoi

+0

我推薦使用'std :: strtol' pre-C++ 11。它具有與'std :: stoi'相同的錯誤檢查功能。 – chris

0
int i; 
cin >> i; 
while (i >= 100 || i <= -100) { 
    i = i/10; // remove and ignore the last digit 
} 

這不是真正的大量工作,由於整數溢出。我只是把它作爲一個非常簡單的算法。

-3

讓我重寫它爲你回答這個問題:

如何從標準輸入讀取一個字符串,前兩個字符寫到標準輸出?

+2

這是如何回答這個問題的? –

+0

@David它將一個非平凡的數字問題轉化爲一個字符串操作問題,因此任何人都可以解決這個問題:'string input; cin >>輸入; istringstream iss(input.substr(0,2)); int result(0); iss >>結果;' – IInspectable

+2

而且...... *這是應該進入答案的。 –

0

閱讀的字符串,並提取前2個字符:

std::string input; 
cout << "Please enter how many burgers you'd like" << endl; 
cin >> input; 
int first2; 
if (input.size()>=1) 
{ 
    if (input[0] == '-') 
     std::cerr << "Negative num of burgers"; 
    else 
     first2 = atoi(input.substr(0,2).c_str()); 
} 
else 
    std::cout << "Null number"; 
0

我覺得這個方法很容易理解。

int input; 
cout << "Please enter how many burgers you'd like" << endl; 
cin >> input; 

char cTemp[50]; 

itoa(input, cTemp, 10); 

char cResult[3] = {cTemp[0], cTemp[1], '\0'}; 

int output = atoi(cResult); 
+0

除了我的口味太C,不可讀,它有一個很好的緩衝區溢出。 -1 – IInspectable

+0

@IInspectable,除非sizeof(int)很大,否則至少不會有緩衝區溢出。 – chris

+0

@chris如果你不想'atoi'訪問'cResult [2]'(第三元素),你將不得不阻止這種情況的發生。就目前而言,這不是代碼的作用。這是未定義的行爲。 – IInspectable

0

我很驚訝沒有人提到fscanf。雖然C++純粹主義者可能會反對,但在這種情況下,這需要比cin少得多的代碼(並且實際上更好的錯誤檢查)。

int res = 0; 
std::cout << "Please enter how many burgers you'd like" << std::endl; 
if (fscanf(stdin, "%02d", &res) != 1) { 
    std::cerr << "Invalid Input" << std::endl; 
} 
int c; 
do { 
    c = fgetc(stdin); 
} while (c != '\n' && c != EOF); 
std::cout << "Got " << res << std::endl; 
+0

請注意,這需要'cin'與'stdin'同步,而stdin是默認的默認值 –

相關問題