Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculator program

Status
Not open for further replies.

moduro

Programmer
Jul 27, 2000
1
0
0
US
can someone help with writing a simple calculator (+,-,/,*) in C++ or Visual C++?<br><br>Thanks<br><br>moduro
 
You need to input the expression as a string, parse the string (seperate it into the first number, operator, and second number), and then perform that operation on the numbers.<br><br>Inputting the string is simple. When you have it, find the position of the operator in the string, save that character for later. Make two new strings, one with everything before the operator, one with everything after it. Convert those strings to integers (that's simple too, start with 0, add the ascii value of the character minus the ascii value of zero times 10 to the power of how many places from the ones place you are). Then, use a switch statement for the operator, case '+' write num1 + num2, case '*' write num1 * num2, etc. I hope I explained that well enough.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top