0

I've been learning C++, and I tried to create a basic calculator app. The goal is to obtain two numbers from 0-9 from the user, and a mathematical operation (+, -, *, /); if some other character is typed, I want to loop the program to keep prompting for the proper input.

But whenever I run the program, it doesn't recognize the numbers 0-9, and keeps repeating the loop. These are the main 3 functions I am using. From main, I'm simply calling them, so I doubt the problem is there. Help please?

Oh and I know I'm never supposed to use go-to, but I wanted practice. And if you could point out more efficient ways of writing this code, that's awesome. Thanks a million.

int GetUserInput(){
using namespace std;
cout << "Please enter a number between 0-9." << endl;

char inputChar; 
cin >> inputChar;

while (inputChar != ('1' || '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9' || '0')) {
    cout << "Please enter a number between 0-9." << endl;
    cin >> inputChar;
}

return static_cast <int> (inputChar);

}

char GetMathematicalOperation(){
using namespace std;
cout << "Please enter a mathematical operator (+, -, *, /)" << endl;

// Storing user input character into char inputChar
char inputChar; 

inputloop:
cin >> inputChar;
switch(inputChar) {
    case('+'):
    case('-'):
    case('*'):
    case('/'):
        break;
    default:
        cout << "Please enter a mathematical operator (+, -, *, /)" << endl;
        goto inputloop;
    }

return inputChar;

}

int CalculateResult(int x, char Operator, int y){
if (Operator = '+')
    return x+y;
if (Operator = '-')
    return x-y;
if (Operator = '*')
    return x*y;
if (Operator = '/')
    return x/y;
return 0;

}

4

7 回答 7

1

您还可以使用isdigit执行以下操作:

while(!isdigit(inputChar)) {  
    // code here  
}  
于 2013-07-18T21:05:40.317 回答
1

在 C++ 中

('1' || '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9' || '0') == true

更具体地说,当与or运算符比较时char,具有非特定0值(值,而不是字符)的 a 的计算结果为。true==!=

所以你的表情

inputChar != ('1' || '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9' || '0')

相当于

inputChar != true

您最好将所有这些chars放入容器中并检查容器中是否存在用户输入。

未经测试的代码

char mychars[] = {'1','2','3','4','5','6','7','8','9','0'};
std::set<char> inputChars;
inputChars.insert(mychars, mychars+10);

if(inputChars.find(inputChar) != inputChars.end())
{
...
}
于 2013-07-18T20:54:50.143 回答
1

运算符需要对||布尔表达式进行操作,而字符不是。您需要将其扩展为while (inputChar != '1' && inputChar != '2' && ....

或者,您可以利用数字的字符代码是连续的这一事实。换句话说,你可以做while (inputChar < '0' || inputChar > '9').

此外,在您的CalculateResult函数中,您需要将它们更改===- 否则,您将覆盖Operator变量,而不是与它进行比较。

于 2013-07-18T20:56:22.883 回答
0

您的条件错误...您需要检查 (inputchar != '0') && (inputchar != '1') && ... && (inputchar != '9')

于 2013-07-18T21:02:44.837 回答
0

另一种解决方案:if (std::string("0123456789").find(inputChar) != std::string::npos). 变量npos- no position - 表示未找到。

于 2013-07-18T23:20:05.063 回答
0

你想检查inputChar是否在'0'到'9'的范围之外,所以你需要这样的东西:

while (inputChar < '0' || inputChar > '9') 
于 2013-07-18T20:56:33.463 回答
0
while (inputChar != ('1' || '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9' || '0'))

您必须与每个角色进行比较。

while ((inputChar != '1') ||  (inputChar != '2') ....

或者干脆——

while ((inputChar < 47) || (inputChar > 57))

下一个,

if (Operator = '+')

编译器应该给你警告。它的任务。==如果您打算进行比较,您实际上需要运算符。

于 2013-07-18T20:58:07.640 回答