Question

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;

}

Was it helpful?

Solution

The || operator needs to operate on boolean expressions, which characters are not. You'd need to expand it out to while (inputChar != '1' && inputChar != '2' && ....

Alternatively, you could exploit the fact that the character codes of digits are sequential. In other words, you could do while (inputChar < '0' || inputChar > '9').

Also, in your CalculateResult function, you need to change those = to == - otherwise, you overwrite the Operator variable, rather than comparing to it.

OTHER TIPS

In C++

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

More specifically, a char that has a value that is not specifically 0 (the value, not the character) evaluates to true when compared with the == or != operator.

So your expression

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

Is equivalent to

inputChar != true

You would do better to put all those chars into a container and check if the user input exists in the container.

Untested code

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())
{
...
}

You could also use isdigit to do something like:

while(!isdigit(inputChar)) {  
    // code here  
}  

You want to check whether the inputChar is outside the range '0' to '9', so you need something like this:

while (inputChar < '0' || inputChar > '9') 

Your condition is wrong...you need to check (inputchar != '0') && (inputchar != '1') && ... && (inputchar != '9')

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

You have to compare against each character.

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

Or simply -

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

Next,

if (Operator = '+')

Compiler should have given you warning. Its an assignment. You actually need == operator instead if you intend to do comparison.

Another solution: if (std::string("0123456789").find(inputChar) != std::string::npos). The variable npos - no position - means not found.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top