# calculator program

This topic is 2177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am building a very simple calculator program. I am confused on how to use the getche() command.  Here is the code I am working on.

int main()
{

int first,second;

char oper;
cout << "Enter first number, operator, second number: ";

first = getche();
oper = getche();
second = getche();

switch(oper)
{
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
}
cout << endl;

system("pause");
return 0;
}

##### Share on other sites

when I put in 2+2 I get 100 as the answer. there might me a problem using the char oper declaration.

##### Share on other sites

That's because getche (which I haven't seen used in a very, very long time, how old are your learning materials?) returns the ASCII code for the character typed. The ASCII code of '2' is 50, so your program works (well works as expected anyway, which isn't what you want).

A few things:

1) You can only enter single digits if you do it like that. You can convert a single digit entry ASCII character to decimal like this

char ch = getche(); // getche() might return an int instead of a char, I dunno. It still returns the ASCII value though

int charAsInt = ch - '0';

EDIT: Don't do that though. You have to manually convert a stream of digits to an int to cope with multiple digits, which isn't hard but not worth doing, since you should just let the user type in a complete expression and extract the operands and operator from the complete expression (see end of post).

2) You don't do any error checking, so the above method won't work if you don't enter a single digit number, or an invalid operator.

What you should be doing is allowing the user to enter a line of text, extract the operands and the operator by parsing the text line (and handle errors etc., including dividing by zero), and output the result. Use getline to read the text into a string then use a stringstream to extract the operands and operator from the string.

##### Share on other sites

I am using an older book because it some good exercises.

##### Share on other sites

OK. Well I've told you why it doesn't work as you expect (and a crappy way of making it work, but if you only enter single digit operands and enter a valid operator), and what you should be doing instead. So you've got something to try out for yourself now.

##### Share on other sites

I will work on it some more, thanks for the help.

##### Share on other sites

well I found that I can use cin >> first  >> oper  > second , instead of getche()

##### Share on other sites

Yes, although I thought you were using getche() to avoid having to press return after each input, that is what it is for. EDIT: Although if you do use it, which you shouldn't because it has been deprecated (use _getche() instead) you would have to parse the input yourself.

EDIT2: Thinking about it you only need to press return once since parsing an int and a char and another int should work with cin anyway since it stops parsing ints after the first non-numerical character. Long time since I used cin ;) So use that instead. Remember to check for input errors though.

cool thanks

• 13
• 18
• 29
• 11
• 20