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?
//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>
usingnamespace 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;
}
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;
}
elseif (.......){.....}
break;
}
or you could use a switch statement on the operator.
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.