Do...While Loops

I have been working on this program for a few days. It is a simple calculator in a do...while loop. The loop is supposed to run and continuously compute the total of all inputs until the user enters an 'X' as their input. The loop I have runs once and then stops.
The other problem I'm having with this is that I want to output "Cannot divide by 0" if a person tries to and I have not been able to figure this out.
Here is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;

/*Write a calculator which asks the user for an operation and then a number.
Calculate the total of that number and the previous total.  Begin the total at "0".
Stop the program when the user enters "X".
*/ 

int main ()

{
	double total=0, response=0;
	char operation;
	do
		{
		cout << "Current total is " << total << endl;
		cout << "Enter an operation: + - * / (or enter X to exit): ";
		cin >> operation;
		cin.ignore();
		if (operation=='X') break;
		cout << "Enter a number: ";
		cin >> response;
		cin.ignore();
		//Calculations are working correctly.
		if (operation=='+')
			total=total+response;
		if (operation=='-')
			total=total-response;
		if (operation=='*')
			total=total*response;
		if (operation=='/')
			total=total/response;
		if ((operation=='/') && (response=='0'))
			cout << "Can not divide by zero!" << endl;  /*This cout isn't displaying when the operation is division and the response is 0 */			 
		cout << "Current total is " << total << endl;
		}
		while ((operation!='X') && ((operation=='+') && (operation=='-') &&(operation=='*') 
		&& (operation=='/')));  /*The program isn't looping.  
               I want it to keep going until 'X' is entered*/ 
		
}	


closed account (Lv0f92yv)
Take a look at your condition in the while(). Check your logical operators - you're using &&'s, and that condition will never be true. That is why the loop breaks.

Great-- thank you! The loop is now working. How can I make "Can not divide by zero!" the output if the user enters division and 0?
Uh. Try "if"...?
Are you saying to try "if" before ((operation=='/') && (response=='0'))? If not, where are you suggesting I put it?
I just saw that your program already has the check for zero.
The check has to be done before the division is performed, otherwise it'll be too late.
That should be VERY obvious, though.
Last edited on
I understand what you're talking about in theory but I still am not sure where to put the check. I don't care if 0 is entered for anything other than division. I tried moving
1
2
if ((operation=='/') && (response=='0'))
			cout << "Can not divide by zero!" << endl; 

around but haven't had any success with it. Where would you recommend I put it?
slg5094 wrote:
...where to put the check.
Athar wrote:
...before the division is performed...
Did you figure out where the
1
2
if ((operation=='/') && (response=='0'))
			cout << "Can not divide by zero!" << endl; 


goes? I tried putting it in my program as well (I guess we're in the same class, lol) and it keeps giving me an answer of 1.#NF.
the quotes around '0' is making 0 ASCII character 0 which equals 0x30 you need to remove the quotes, response == 0 since response is a double.
Topic archived. No new replies allowed.