calculator program

Started by
7 comments, last by phil67rpg 10 years, 8 months ago

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;
 
 
float answer=0.0;
 
 
char oper;
cout << "Enter first number, operator, second number: ";
 
first = getche();
oper = getche();
second = getche();
 
 
switch(oper)
{
case '+':
answer=first+second;
break;
case '-':
answer=first-second;
break;
case '*':
answer=first*second;
break;
case '/':
answer=first/second;
break;
}
cout << endl; 
cout << "Answer: " << answer << endl;
 
system("pause");
return 0;
}

Advertisement

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

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.

You posted a thread about this the other month anyway...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

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

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.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

cool thanks

This topic is closed to new replies.

Advertisement