I can't seem to get my do and while loop to work. For this assignment, I have to make a calculator that will keep looping with functions + - * and /. Here's what I have so far.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
double total = 0, number;
string math;
do
{
cout << "Current total is " << total << endl;
cout << "Enter an operation: + - * / (or enter X to exit): " ;
cin >> math;
cout << "Enter a number: " ;
cin >> number;
} while ((math != "x") && (math != "X") && (math == "+") && (math == "-")
&& (math == "*") && (math == "/"));
if (math == "+")
{
cout << total + number << endl;
}
if (math == "-")
{
cout << total - number << endl;
}
if (math == "*")
{
cout << total * number << endl;
}
if (math == "/")
{
cout << total / number << endl;
}
}
So far, I have it asking for the mathematic symbol and then doing the equation with a number. However, it wont loop after one try! I can't figure out how to fix it. Any ideas?
You only ever assign a value to total when you intialize it. Instead of total + number, you should say total += number. The same goes for the other operations.
Single character constants should also be in ' 's as opposed to " ", i.e. '+', '-', '='.
Your while loop logic is also flawed - how can math be both + and * at the same time? I would make math a character as opposed to string as it should only be holding one operator as that is its intended use.
Your calculator also won't loop - once it somehow gets out of the do while loop and fulfills the if loops, that's the end of the program. I believe the if loops should be inside your do while loop.
How about putting all those if statements inside the loop? And if you want to use character literals as bluezor suggested, you need to change variable math's type to char.
Ok, I put them into the loop and I also changed the variable type. I don't understand why I'm getting the odd formatting between with the number though.
Just think for a minute. You're changing total's value (you really should use the proper operator as I suggested, so you don't have to type "total" twice) and then, instead of just outputting total, you're outputting the result of performing the operation again.
So, instead of, say cout << total + number << endl; you really want to say cout << total << endl;
I'm sorry but that makes absolutely no sense to me.
If I change total + number to total += number, I get an error that reads:
STD::basic_ostream<_Elem, _Traits<
I'm trying really hard to reread and understand all of this but isn't making sense to me.
You only ever assign a value to total when you intialize it. Instead of total + number, you should say total += number. The same goes for the other operations.
Wow, I feel so stupid. I made such a mistake taking this class online.. my instructor seriously sent out an e-mail saying we're only allowed to ask 3 questions the entire term.
Thank you so much filipe.. I really should pay you for this! Haha.
Here's what I'm getting, along with my code, with the 0.0
To fix that you need to test for that 0.0 before performing division. The statement you just added will never be executed if math == '/'. You should do what firedraco suggested.