calculator functions

Hello,
I am working on a code to ask a user for a mathematical operation and a number and then calculate the total until the user enters "X" to exit the program.
I have the correct cout and cin but the calculator function isn't working. The total is constantly outputted as "0" instead of calculating throughout the output.
Any help please?
Thanks,
Sarah

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
43
44
45
#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;
		cout << "Enter a number: ";
		cin >> response;
	}while ((operation!='X') && ((operation=='+') || (operation=='-') || (operation=='*') 
		|| (operation=='/')));
	do
		{
			cout << total+response << endl;
		} while (operation=='+');
		
		{
			cout << total-response << endl;
		} while (operation=='-');
		
		{
			cout << total*response << endl;
		} while (operation=='*');
		
		{
			cout << total/response << endl;
		} while (operation=='/');
		
		{
			cout << "Can not divide by zero!" <<endl;
		} while ((operation='/') && (response=0));
	
			
}
You need to use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); after every std::cin statement. Also lines 23 - 42 are very hard to read and also incorrect. Try changing it to a switch-case.
I'm just beginning with C++ so I'm not sure what std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'): is. Same for switch-case. I do need to use do... while loops and if statements. I tried switching lines 23 to 42 to if statements but the program still isn't working. Do you have any tips for making this program work using do...while loops and if statements?
Thanks.
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
43
44
45
46
47
#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;
		cout << "Enter a number: ";
		cin >> response;
	} while (((operation!='X') && ((operation=='+') || (operation=='-') || (operation=='*') 
		|| (operation=='/'))) && ((response<=0)||(response>=0)));
	
	if (operation=='+')
		{
			cout << total+response << endl;
		} 
	if (operation=='-')
		{
			cout << total-response << endl;
		} 
	if (operation=='*')	
		{
			cout << total*response << endl;
		} 
	if (operation=='/')	
		{
			cout << total/response << endl;
		} 
	if ((operation='/') && (response=0))	
		{
			cout << "Can not divide by zero!" <<endl;
		} 
	
			
		
}
When you use cin>> as you are, the enter key the user presses to send the data is left in the input buffer. So you need to use the cin.ignore() function to get rid of it.
Topic archived. No new replies allowed.