[英]Why is my input checking if else statement not working?
我是 C++ 初學者,目前正在研究一個簡單的計算器項目。 我希望輸入只有數字,所以我做了一個 if else 語句來檢查輸入是否只有數字,如果是,那么進行計算並返回“輸入運算符”bla bla。 請忽略大小寫 2 3 4,因為我想讓輸入檢查器工作。 我做錯了什么,我該如何解決這個問題? 先感謝您。 但由於某種原因,它只是行不通。
# include <iostream>
using namespace std;
int main() {
bool whileRunning = true;
while(whileRunning){
char op;
float num1, num2;
cout << "\n Enter operator either 1 (+) or 2 (-) or 3 (*) or 4 (/) or 5 to exit: ";
cin >> op;
switch (op)
{
case '1':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) == 0 && isdigit(num2) == 0) {
cout << num1 + num2;
}
else
cout << " Please only input numbers";
break;
break;
case '2':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) && isdigit(num2)) {
cout << num1 - num2;
}
else
break;
break;
case '3':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) && isdigit(num2)) {
cout << num1 * num2;
}
else
break;
break;
case '4':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) && isdigit(num2)) {
if (num2 == 0) {
cout << "You tried to divide by 0, Try again!";
break;
}
else
cout << num1 / num2;
}
else
break;
break;
case '5':
whileRunning = false;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! Your input must be >= 1 and <= 5. Please try again";
break;
}
}
return 0;
};
您在isDigit
中進行了錯誤的檢查,並且isDigit
不能與 float number 一起使用。
正如C++所說,參考返回值為:
如果 c 確實是十進制數字,則該值不同於零(即,真)。 否則為零(即假)。
因此,您需要檢查isDigit
function返回的值是否不同於 0 。 正確的代碼是這樣的:
# include <iostream>
using namespace std;
int main() {
bool whileRunning = true;
while(whileRunning){
char op;
int num1, num2;
cout << "\n Enter operator either 1 (+) or 2 (-) or 3 (*) or 4 (/) or 5 to exit: ";
cin >> op;
switch (op)
{
case '1':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (isdigit(num1) != 0 && isdigit(num2) != 0) {
cout << num1 + num2;
}
else
cout << " Please only input numbers";
break;
....
default:
// If the operator is other than 1, 2, 3, 4, 5 error message is shown
cout << "Error! Your input must be >= 1 and <= 5. Please try again";
break;
}
}
return 0;
};
在您的代碼中,您測試相反的代碼。
您可以在此站點上找到有關isDigit
function 的進一步閱讀。
對於浮點數,您可以使用其他方法:
if(scanf("%f", &userNum) == 1) {
// Handle item float here
}
所以你的代碼看起來像:
# include <iostream>
using namespace std;
int main() {
bool whileRunning = true;
while(whileRunning){
char op;
float num1, num2;
cout << "\n Enter operator either 1 (+) or 2 (-) or 3 (*) or 4 (/) or 5 to exit: ";
cin >> op;
switch (op)
{
case '1':
cout << "Enter two operands: ";
if(scanf("%f %f", &num1, &num2) == 2){ {
cout << num1 + num2;
}
else
cout << " Please only input numbers";
break;
....
default:
// If the operator is other than 1, 2, 3, 4, 5 error message is shown
cout << "Error! Your input must be >= 1 and <= 5. Please try again";
break;
}
}
return 0;
};
您可以在此處找到 scanf 參考。
首先: std::isdigit
僅檢查您傳遞的參數是否是來自'0'...'9'
的數字。 是的,令人困惑的是它的參數是int
類型,而不是char
類型,而且更令人困惑,因為你可以給它一個float
。
cin >> num1 >> num2;
if (isdigit(num1) == 0 && isdigit(num2) == 0) {
cout << num1 + num2;
}
基本上會檢查num1 >= '0' && num1 <= '9'
。 如果我們用 ascii 代碼替換那個字符,你會得到num1 >= 48 && num1 <= 57
(與num2
相同)。 到目前為止,為什么它不能以這種方式工作。
如何修復它:
cin >> num1 >> num2;
無論如何,總是會嘗試閱讀float
。 如果您輸入一個字符串,它不會神奇地將字符串存儲在變量中,而是會設置一個失敗位。
進行輸入驗證的常用方法是:
if (cin >> num1 >> num2) {
cout << num1 + num2;
}
else {
cout << " Please only input numbers";
// ignore faulty input until the next line or eof
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// reset flags to allow further input
cin.clear();
}
* std::
為簡潔起見省略
這是因為std::istream::operator>>
返回對自身的引用,然后可以將其轉換為 bool ,因此當且僅當用戶輸入兩個浮點數時才會產生true
。
我決定重新編寫代碼。 謝謝大家的幫助!
#include<iostream>
#include<string.h>
using namespace std;
bool ifstring(string number) {
for (int i = 0; i < number.length(); i++)
if (isdigit(number[i]) == true)
return false;
return true;
}
int main() {
bool whileRunning = true;
while(whileRunning){
char op;
string num1, num2;
float dnum1, dnum2;
cout << "\n Enter operator either 1 (+) or 2 (-) or 3 (*) or 4 (/) or 5 to exit: ";
cin >> op;
switch (op)
{
case '1':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (ifstring(num1) == 0 && ifstring(num2) == 0) {
dnum1 = stof(num1);
dnum2 = stof(num2);
cout << dnum1 + dnum2;
}
else
cout << " INPUT PLS";
break;
break;
case '2':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (ifstring(num1) == 0 && ifstring(num2) == 0) {
dnum1 = stof(num1);
dnum2 = stof(num2);
cout << dnum1 - dnum2;
}
else
break;
break;
case '3':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (ifstring(num1) == 0 && ifstring(num2) == 0) {
dnum1 = stof(num1);
dnum2 = stof(num2);
cout << dnum1 * dnum2;
}
else
break;
break;
case '4':
cout << "Enter two operands: ";
cin >> num1 >> num2;
if (ifstring(num1) == 0 && ifstring(num2) == 0) {
dnum1 = stof(num1);
dnum2 = stof(num2);
if (dnum2 == 0) {
cout << "You tried to divide by 0, Try again!";
break;
}
else
cout << dnum1 / dnum2;
}
else
break;
break;
case '5':
whileRunning = false;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! Your input must be >= 1 and <= 5. Please try again";
break;
}
}
return 0;
};
如前所述, C++
沒有固有的 function 來檢查輸入是否為數字。 因此,您可以使用以下 function 來檢查字符串是否為數字:
bool isNumber(const std::string& s)
{
std::istringstream iss(s);
float f;
char c;
return iss >> f && !(iss >> c);
}
將此應用於您的簡單計算器程序,它可以寫成:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
bool isNumber(const std::string& s)
{
std::istringstream iss(s);
float f;
char c;
return iss >> f && !(iss >> c);
}
int main() {
bool whileRunning = true;
while (whileRunning) {
char op;
float num1, num2;
string str1, str2;
cout << "\n Enter operator either 1 (+) or 2 (-) or 3 (*) or 4 (/) or 5 to exit: ";
cin >> op;
if (op != '5')
{
cout << "Enter two operands: ";
cin >> str1 >> str2;
if (isNumber(str1) && isNumber(str2))
{
num1 = static_cast<float>(atof(str1.c_str()));
num2 = static_cast<float>(atof(str2.c_str()));
}
else
continue;
}
switch (op)
{
case '1':
cout << num1 + num2;
break;
case '2':
cout << num1 - num2;
break;
case '3':
cout << num1 * num2;
break;
case '4':
if (num2 == 0)
cout << "You tried to divide by 0, Try again!";
else
cout << num1 / num2;
break;
case '5':
whileRunning = false;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! Your input must be >= 1 and <= 5. Please try again";
break;
}
}
return 0;
};
只有一個提示:如果你想避免強制轉換和/或你想對數值計算有很好的精度,請使用double
而不是float
。
希望能幫助到你?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.