0
我想檢查兩個單獨的輸入,如果他們是整數或不。我能夠錯誤地檢查一個輸入,但我不太確定如果我使用'get'函數並且兩個輸入都來自'cin'流,如何檢查兩個單獨的輸入。使用C++。錯誤檢查兩個單獨的輸入
我檢查一個整數的代碼顯示如下。
#include <iostream>
using namespace std;
int main() {
int input;
cout << "Enter an integer: ";
cin >> input;
char next;
int x=0;
int done = 0;
while (!done){
next = cin.get();
if (next == ' ' || next == '\n'){
cout << "The Integer that you have entered is: " << input << "\n";
done = 1;
}
else if (next == '.'){
cerr << "Error: Invalid Input. Not an Integer." << "\n";
done = 1;
}
else{
cerr << "Error: Invalid Input. Not a number." << "\n";
done = 1;
}
}
return 0;
}
使用'std :: getline'而不是'operator >>'。 –
並退出使用名稱空間標準..原因? [Here](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – amanuel2
@ amanuel2儘管有其他文章,但實際上沒有理由不使用整個名稱空間在這個特殊情況下。 –