gpt4 book ai didi

c++ - 为什么我的输入检查 if else 语句不起作用?

转载 作者:行者123 更新时间:2023-12-03 06:56:59 25 4
gpt4 key购买 nike

我是一名 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 一起使用

正如C++引用 所说,返回值为:

A value different from zero (i.e., true) if indeed c is a decimaldigit. Zero (i.e., false) otherwise.

所以你需要检查 isDigit 是否函数返回一个不同于 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 上找到更多阅读内容在此功能 site .

对于 float ,您可以使用其他方法:

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引用here .

关于c++ - 为什么我的输入检查 if else 语句不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65000236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com