Yesterday I decided to compose my own calculator just for fun and right now I'm trying to make it able to repeat operations via do-while loop. Algorithm looks like right, but it doesn't work properly. Could someone say what's wrong?
What you are doing doesn't make sense. You need to get 2 numbers as input and 1 sign from the user. In the beginning of the code give the user some guide, something like "Enter two numbers followed by a space: " or "Enter first number: ". You seem to take one number and then set the second to the first (c = a).
You are doing this:
1 2
cin >> a;
c = a;
Then:
a = c;
That doesn't make sense: c is already a, so why do you set a to c?
Also, you don't need two do-while loops.
#include <iostream>
int main()
{
double n1, n2, result; //n1 and n2 will be the two numbers, result will hold the result
char sign, sign2; //sign will be the Math operator, sign 2 will be option to continue or exit
do
{
//Get the 1st number
std::cout << "Enter a number: ";
std::cin >> n1;
//Get the 2nd number
std::cout << "Enter another number: ";
std::cin >> n2;
//Get the operator
std::cout << "Enter an operator (+, -, * or /): ";
std::cin >> sign;
//Check the operator
switch(sign)
{
//If its '+' then add
case'+':
result = n1+n2;
break;
case'-': //If its '-' then subtract
result = n1-n2;
break;
case'*': //If its '*' then multiply
result = n1*n2;
break;
case'/': //If its '/' then divide
if(n2 != 0) //Divide if 2nd number isn't 0
result = n1/n2;
else //If 2nd number is 0, give a Math error
{
std::cout << "Math error: Div by 0!" << std::endl;
result = 0;
}
break;
default: //If the Math operator isn't applicable, give an error message
std::cout << "Invalid operation!" << std::endl;
result = 0;
break;
}
//Display the result
std::cout << "Result: " << result << std::endl;
//Get the option to continue or exit. Repeat the menu until the user enters a correct option
do
{
std::cout << "\nEnter an option:\nN - New calculation\nE - Exit" << std::endl;
std::cout << "Option: ";
std::cin >> sign2;
std::cout << "\n";
if(!(sign2 == 'E' || sign2 == 'N'))
std::cout << "Invalid option!" << std::endl;
} while(!(sign2 == 'E' || sign2 == 'N'));
} while(!(sign2 == 'E'));
//The following will wait until the user presses ENTER. This is better than system("pause").
std::cin.get();
std::cin.ignore();
return 0;
}
is attempt to compel my program to work again with previous result. As you could see, I want to have three options after obtaining first result: continue calculations with result that program have already yielded, start new calculation and end of program.
If user would choose 'Continue' - program assign previous result as 'a' and then do further calculations, if he would choose 'New', program offer to write new number and then assign this new number to result that we obtained before.
So, in this way we could repeat inner loop and avoid conflict with previous results, that could occur if user would choose new calculation. I hope now it's clear. Maybe another(more simple and descriptive) algorithm could exist.
Anyway, thanks a lot! I found a lot of clues in your code which could be useful!
Thanks for clarifying your question. To do what you want, you just need to modify my code by a bit:
1) Add an if statement before getting the first number to only get the first number if the user wants to do a new calculation.
2) After getting the options, if the option is 'C', set n1 equal to result.
#include <iostream>
int main()
{
double n1, n2, result; //n1 and n2 will be the two numbers, result will hold the result
char sign, sign2 = 'N'; //sign will be the Math operator, sign 2 will be option to continue or exit
do
{
//Get the 1st number only if user wants to do a new calculation
if(sign2 == 'N')
{
std::cout << "Enter a number: ";
std::cin >> n1;
}
//Get the 2nd number
std::cout << "Enter another number: ";
std::cin >> n2;
//Get the operator
std::cout << "Enter an operator (+, -, * or /): ";
std::cin >> sign;
//Check the operator
switch(sign)
{
//If its '+' then add
case'+':
result = n1+n2;
break;
case'-': //If its '-' then subtract
result = n1-n2;
break;
case'*': //If its '*' then multiply
result = n1*n2;
break;
case'/': //If its '/' then divide
if(n2 != 0) //Divide if 2nd number isn't 0
result = n1/n2;
else //If 2nd number is 0, give a Math error
{
std::cout << "Math error: Div by 0!" << std::endl;
result = 0;
}
break;
default: //If the Math operator isn't applicable, give an error message
std::cout << "Invalid operation!" << std::endl;
result = 0;
break;
}
//Display the result
std::cout << "Result: " << result << std::endl;
//Get the option to continue or exit. Repeat the menu until the user enters a correct option
do
{
std::cout << "\nEnter an option:\nN - New calculation\nC - Continue calculations\nE - Exit" << std::endl;
std::cout << "Option: ";
std::cin >> sign2;
std::cout << "\n";
if(!(sign2 == 'E' || sign2 == 'N' || sign2 == 'C'))
std::cout << "Invalid option!" << std::endl;
} while(!(sign2 == 'E' || sign2 == 'N' || sign2 == 'C'));
//If user wants to continue
if(sign2 == 'C')
n1 = result;
} while(!(sign2 == 'E'));
//The following will wait until the user presses ENTER. This is better than system("pause").
std::cin.get();
std::cin.ignore();
return 0;
}