
do.. while loops
whats up fellas. another assignment that is due somewhat soon that i have some questions on. the assignment description is:
Write a simple calculator program. Ask the user if s/he wants to add ('+') subtract (''), multiply ('*') or divide ('/') and then ask for the number. Perform the operation with the given number. Start the "display" at zero. Stop when the user enters 'X' (for exit). Use Do.. While and if statements.
.... so far i have this:
Code:
#include <iostream>
using namespace std;
int main()
{
double n;
char operation;
cout << "Current Total is 0" << endl;
do
{
cout << "Enter an operation: +  * / (or enter X to exit):";
cin >> operation;
cout << "Enter a number: ";
cin >> n;
cout << "Current total is " << n << endl;
} while (operation != 'X');
}
the output is coming out somewhat nice, but i am trying to figure out how to incorporate the operation into the output of the numbers. (ie: if user enters +, and then enters the number 20, the total displayed would be 20. but, after that, i am not sure how the next operation the user enters can effect the previously entered number.
trying to explain this differently, using the same example, if the user entered +, then entered 20. the number displayed is 20. then, if the user entered the * operation next, and entered 2 for the next number, the 'new' current total should be 40. this is the part i am not sure how to do.
if any of you guys know how to help me with this, it would be awesome. and if you would like me to try to explain it a little better, i will do so as well.
again, thanks for any help guys.

You will need IFstatements to check what the user has inputted and calculate the new value accordingly. You may also want to put in another input option that it resets n to zero. They key here is not to put n in the cin>> (swap it with another temporary value so it doesn't mess up what you've already got).

thanks for the quick response. i think i am on the right track, but being that the operation comes before the first number entered, the 'new' number displayed is coming out with something all screwey (1#IND).
i remember you mentioning something about making a temporary double number, and setting it to zero, im not sure how to do that. if i make the cin >> n2, then all the 'n = ..." wont work because of not knowing which double to use.
Code:
#include <iostream>
using namespace std;
int main()
{
double n, n2;
char operation;
cout << "Current Total is 0" << endl;
do
{
cout << "Enter an operation: +  * / (or enter X to exit):";
cin >> operation;
cout << "Enter a number: ";
cin >> n;
if (operation = '+') n = n + n;
if (operation = '') n = n  n;
if (operation = '*') n = n * n;
if (operation = '/') n = n / n;
cout << "Current total is " << n << endl;
} while (operation != 'X');
}
thanks again.

= is assignment, == is comparison

i have been working on this forever. i looked in my book everywhere and cant seem to find how to finish this. the if statements work, but i cant seem to find out how to make the previous number be used for the operations following.
Code:
#include <iostream>
using namespace std;
int main()
{
double n, n2=0;
char operation;
cout << "Current Total is " << n2 << endl;
do
{
cout << "Enter an operation: +  * / (or enter X to exit):";
cin >> operation;
cout << "Enter a number: ";
cin >> n;
if (operation == '+')
{
n = n + n;
cout << "Current Total is " << n << endl;
}
if (operation == '')
{
n = n  n;
cout << "Current Total is " << n << endl;
}
if (operation == '*')
{
n = n * n;
cout << "Current Total is " << n << endl;
}
if (operation == '/')
{
n = n / n;
cout << "Current Total is " << n << endl;
}
} while (operation != 'X');
}
i think i am getting on the right track.
thanks for any help.

you should enter the scond number as well.
don't you want to ask user to enter it?

yeah, the user is supposed to enter the number after putting in the desired operation. so if the number was 40, and the user selected '' and put in the number 3, the new number should be 37.
i have been racking my brain forever on this stuff. and it is due tomorrow morning. thank you for the continued help. this c++ stuff is tough.

Something like
Code:
#include <iostream>
using namespace std;
int main()
{
double n, n2=0;
char operation;
cout << "Enter a first operand: ";
cin >> n;
do
{
cout << "Enter an operation: +  * / (or enter X to exit):";
cin >> operation;
if(operation == 'X')
break;
cout << "Enter a second operand: ";
cin >> n2;
if (operation == '+')
{
n += n2;
}
else if (operation == '')
{
n = n2;
}
else if (operation == '*')
{
n *= n2;
}
else if (operation == '/')
{
n /= n2;
}
cout << "Current Total is " << n << endl;
} while (operation != 'X');
cout << "The final result is " << n << endl;
return 0;
}

youre great man. thanks a lot. there is only 1 problem. when the user enters '/' and then enters the # 0, it is supposed to come up with a message and display the previous number.
would that be 2 separate if statements, because i feel as though i would need more than 1, under each statement. again, sorry to be bothersome, but i am a beginner and trying to learn on the go.
thanks again.

i tried doing something like this:
Code:
#include <iostream>
using namespace std;
int main()
{
double n=0, n2=0;
char operation;
cout << "Current Total is " << n << endl;
do
{
esc: cout << "Enter an operation +  * / (or enter X to exit):" << endl;
cin >> operation;
if (operation == 'X')
break;
cout << "Enter a number: " << endl;
cin >> n2;
if (operation == '+')
n += n2;
else if (operation == '')
n = n2;
else if (operation == '*')
n *= n2;
else if (operation == '/')
n /= n2;
else if ((operation == '/') && (n2 = 0))
{
goto esc;
cout << "error";
}
cout << "Current Total is " << n << endl;
} while (operation != 'X');
}
that doesnt seem to work either.

You should change (n2 = 0) to (n2 == 0). Furthermore, you should reorder the statements, otherwise that block will never be reached since for it to be reached, both (operation == '/') and (n2 == 0) must be true, but if that is so, then (operation == '/') is true, hence control enters the block with n /= n2; instead.
Not only that, but just say no to goto until you have enough experience to discern the special cases where it can be tolerated. In this case, you can either use continue, or simply let control pass to the statement that prints the current total since that makes sense.

also using == on doubles is not recomended...

i did what you said, and it is still coming up with what has been coming up. funny enough, my professor told me the exact same thing you (laserlight) told me. i switched them and made it n2 == instead of =, but i am still getting the same problem.
Code:
#include <iostream>
using namespace std;
int main()
{
double n=0, n2=0;
char operation;
cout << "Current Total is " << n << endl;
do
{
cout << "Enter an operation +  * / (or enter X to exit):" << endl;
cin >> operation;
if (operation == 'X')
break;
cout << "Enter a number: " << endl;
cin >> n2;
if (operation == '+')
n += n2;
else if (operation == '')
n = n2;
else if (operation == '*')
n *= n2;
else if (operation == '/')
n /= n2;
else if ((n2 == 0) && (operation == '/'))
{
cout << "error";
}
cout << "Current Total is " << n << endl;
} while (operation != 'X');
}
again, thanks for any help.

The problem is still there: trace the code when operation == '/'. You will see that the error message can never be printed, because the only case where it can be printed is when operation != '/' && n2 == 0 && operation == '/', which is a contradiction.

ahhh, nevermind. i figured that problem out. i had to switch the order of the else if statements (put the one with the error message before just the one with the / symbol).
now i have to modify this to work with switch.. case programs. ill start working on it and post if i need some help (which i am sure i will need).
thanks again.