Plz guys i have a problem in my c++ simple calculator,
I made everything great but i just need an important hint for my assignment that i can't figure it out.
for example when i add two numbers(2+4) the result is (6)...
i need the number (6) to be stored in order to complete for further calculations..
i need to store the result to maximize the calculations.
plz can someone help me with this problem???

5
Contributors
9
Replies
10
Views
10 Years
Discussion Span
Last Post by gbantsi_2

I don't know for sure what kind of calculator you are using but if it is a TI version this will work

example:

[LIST=1]
[*]Prompt x, y

[*]x+y→e

[*]disp e

[/LIST]

disp e makes it display e to the screen. the makes it store the answer to the variable e...it should be the same for most calculators most of these functions are under the catalog section.

i can't figure it out yet,i decided to send you this so as you can help me more in my project.but please i need this solution today...thnx anyway
f.y.i: i made this program with DEV C++...Please try to figure out the solution..

-----------------------------------------------------------------------

#include <iostream>
using namespace std;
int main()
{
int op1;
int op2;
char oper;
int result ;
while (cin >> op1 >> oper >> op2){
switch (oper)
{
case '+': result = op1 + op2 ;break;
case '-': result = op1 - op2 ;break;
case '*': result = op1 * op2 ;break;
case '/': result = op1 / op2 ;break;
default: cout << " Invalid Operation " << endl;break;
}
cout << "Result Is: " << result << endl;
cout << "\nEnter another one:  " ;
}

return 0;
}

Edited by Dani: Formatting fixed

I'd do it something like this:

oper = '@';
cout << " enter first operand: " << '\n';
cin >> op1;
while (op != '~')
{
cout << "\nEnter operator: " ;
cout << "enter the tilde sign ('~') to stop" << '\n';
cin >> oper;
if(oper != '~')
{
cout << "enter second operand" << '\n';
cin >> op2
switch (oper)
{
}
op1 = result;
cout << "Result Is: " << result << endl;
}
}

if I wasn't allowed or didn't know how to use a stack

I am not exactly sure what you wish for the program to do, but I am guessing you want it to evaluate statements like 5 + 2 + 8, instead of only 2 operands and an operation for each evaluation.

If what I am guessing you want to do is correct, you will need a terminating character to let the program know you are done adding operators and operands to the expression to evaluate.

What I did here is I made it so it reads the first operand, then in a loop, it reads the operator, and if it isn't an equal sign it performs the previous result to the second operand. The equal sign is placed at the end of the expression to terminate the sequence.

This way you can now type in:
5 + 2 * 2 =

and it will result in 14 (Not going to work with operator precedence right now haha)

Your code was already on track for this and only needed a few changes.

#include <iostream>
using namespace std;
int main()
{
int op1;
int op2;
char oper;
int result ;
while (cin >> op1)
{
result = op1;
while (cin >> oper){
if (oper == '=')
break;
cin >> op2;
switch (oper)
{
case '+': result += op2 ;break;
case '-': result -= op2 ;break;
case '*': result *= op2 ;break;
case '/': result /= op2 ;break;
default: cout << " Invalid Operation " << endl;break;
}
}
cout << "Result Is: " << result << endl;
cout << "\nEnter another one: " ;
}
system("PAUSE");
return 0;
}

You might want to consider giving result a floating point data type as you are allowing the user to perform division operations!

actually i figured out a way..thnx for yr advice..
but now i need the final thing to make my program run perfectly..
at the end,i added an option saying(enter another calculation or press q to quit or press c to clear the operation and start a new one)
but i can't figure out how to assign the letter 'q' to quit and how to assign the letter 'c' to clear the result and start a new operation...plz i'd be really glad if u helped me with this last touch.

Here is how you might check to see if they want to continue or quit.

#include <iostream>
using namespace std;
int main()
{
int op1;
int op2;
char oper, choice; // declare choice
int result ;
do // no cin inside the loop condition, in fact make it a do-while
{
cout << "Enter an expression: " ;
cin >> op1;
result = op1;
while (cin >> oper){
if (oper == '=')
break;
cin >> op2;
switch (oper)
{
case '+': result += op2 ;break;
case '-': result -= op2 ;break;
case '*': result *= op2 ;break;
case '/': result /= op2 ;break;
default: cout << " Invalid Operation " << endl;break;
}
}
cout << "Result Is: " << result << endl;
do // see if user wants to continue or quit and validate input
{

cout << "\nPress 'q' to quit or 'c' to continue: ";
cin >> choice;
} while (choice != 'c' && choice != 'q'); // ask until choice = q or c
} while (choice == 'c');
system("PAUSE");
return 0;
}

that didn't work out...listen this is my .cpp so far till now..
i just want to add the 2 methods (first to press q to quit)and(second to press c to begin a new calculation)....

-----------------------------

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int op1;
int op2;
char oper;
int result;
cin >> name;
cout << " \n\nWelcome To My Calculator " << name << "," ;
cin >> op1;
while (cin >> oper >> op2)
{
switch (oper)
{
case '+': result = op1 + op2 ;break;
case '-': result = op1 - op2 ;break;
case '*': result = op1 * op2 ;break;
case '/': result = op1 / op2 ;break;
default: cout << " Invalid Operation " << endl;break;
}
cout << "Result Is: " << result << endl;
op1 = result;
cout << "\nEnter Another Calculation Or Press (Q) To Quit Or Press (C) To Clear:  ";
}
return 0;
}

Edited by Dani: Formatting fixed

Look at the two do-while loops in the code I provided earlier, the second one gets the choice of whether or not to continue from the user, the first checks to see if the program should continue or quit. I intentionally put comments next to important parts if you have a look at those.

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int op1;
int op2;
char oper;
int result;
cin >> name;
cout << " \n\nWelcome To My Calculator " << name << "," ;
cin >> op1;
while (cin >> oper >> op2)
{
switch (oper)
{
case '+': result = op1 + op2 ;break;
case '-': result = op1 - op2 ;break;
case '*': result = op1 * op2 ;break;
case '/': result = op1 / op2 ;break;
default: cout << " Invalid Operation " << endl;break;
}
cout << "Result Is: " << result << endl;
op1 = result;
cout << "\nEnter Another Calculation Or Press (Q) To Quit Or Press (C) To Clear: ";
}
return 0;
}
can you include other operators in this code for me. like the square root, ^ and others