Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

calculator program

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

If you intended to correct an error in the post then please contact us.

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;
 
 
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;
}

 

Share this post


Link to post
Share on other sites
Advertisement

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...

Edited by Paradigm Shifter

Share this post


Link to post
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 this post


Link to post
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.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!