calculator program

I'm trying to write a calculator program that takes in two numbers and an operator and solves it. I have the entire program working fine, except I can't get out of the loop when typing the exit statement. It sends it into an infinite loop reprinting the last phrase that was printed. Any help guys?



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//calculator.cpp : Defines the entry point for the console application.
//The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculator program.
//When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed 
//(i.e. prompting the user for each number, displaying the result, prompting the user to add two more different numbers) 
//until the user enters a sentinel value to end the chosen arithmetic operation.
//If the user chooses division, do not allow the user to divide by 0. Display an error message to user and ask the user to choose another denominator.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;


int main ()
{	//Declare variables.
	double num1, num2, solution;
	char symbol;
	//Don't need to declare these, but I did for ease of seeing all input and output variables.
	char add = '+';
	char subtract = '-';
	char multiply = '*';
	char divide = '/';

	//prompt user to see if they would like to use the calculator app.
	cout << "This calulator takes an equation in the form (number operator number).\nIt adds, subtracts, multiplies and divides any two whole numbers.";
	cout << "\nExample is 5 + 5. You don't need a space between the characters.\n";
	cout << "Please make sure you enter in a whole number.\nTo exit, please type fin.\n\n"; 

	//Ask user to type in equation in the form num1 symbol num2
	cout << "please enter in the equation you would like to be solved: ";
	cin >> num1 >> symbol >> num2;
	while (num1 != 'f' && symbol != 'i' && num2 != 'n')
	{
		while (symbol == '+')
		{
			solution = num1 + num2;
			cout << "The solution to " << num1 << " + " << num2 << " = " << solution;
			cout << "\nplease enter in the equation you would like to be solved: ";
			cin >> num1 >> symbol >> num2;
		}
		while (symbol == '-')
		{
			solution = num1 - num2;
			cout << "The solution to " << num1 << " - " << num2 << " = " << solution;
			cout << "\nplease enter in the equation you would like to be solved: ";
			cin >> num1 >> symbol >> num2;
		}
		while (symbol == '*')
		{
			solution = num1 * num2;
			cout << "The solution to " << num1 << " * " << num2 << " = " << solution;
			cout << "\nplease enter in the equation you would like to be solved: ";
			cin >> num1 >> symbol >> num2;
		}
		while (symbol == '/')
		{	
			while(num2 == 0)
			{
				cout << "Error: The denominator cannot be 0!!\nPlease enter a new value for the denominator: ";
				cin >> num2;
			}
				solution = num1 / num2;
				cout << "The solution to " << num1 << " / " << num2 << " = " << solution;
				cout << "\nplease enter in the equation you would like to be solved: ";
				cin >> num1 >> symbol >> num2;			
		}
	break;
	}
	cout << "\n\t\tHave a nice day!! :)";
	return 0;
}

Last edited on
how do i put the code in the right way?
Sorry dear... i have no any Idea for this time...
It's not getting out of the loop when I type the fin statement. Everything else works fine.
How are you exiting out of the second set of while loops? e.g.

1
2
3
4
5
6
7
8
9
10
11
12
13

while (num1 != 'f' && symbol != 'i' && num2 != 'n')
	{
		while (symbol == '+')// how are you leaving this one?
		{
			solution = num1 + num2;
			cout << "The solution to " << num1 << " + " << num2 << " = " << solution;
			cout << "\nplease enter in the equation you would like to be solved: ";
			cin >> num1 >> symbol >> num2;
		}

break;
}


The while statements should be if statements to make life easier as otherwise they will keep repeating the calculation.


try working with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

while (num1 != 'f' && symbol != 'i' && num2 != 'n')
	{
		if (symbol == '+')
		{
			solution = num1 + num2;
			cout << "The solution to " << num1 << " + " << num2 << " = " << solution;
			cout << "\nplease enter in the equation you would like to be solved: ";
			cin >> num1 >> symbol >> num2;
		}
                else if (.......){.....}
                break;
}


or you could use a switch statement on the operator.
Last edited on
That is an excellent point. I actually switched them to if statements before I read this and it works great now. Thanks. Also, it wasn't running the quitting portion because num1 and num2 are integers and don't accept characters, so I took that part out and just added a continue question after each equation was solved.
Topic archived. No new replies allowed.