I am trying to put multiple OR conditions in my WHILE loop so that unless my "decision" variable is equal to "1" or "2", it will continue looping. However, even when I am entering "1" or "2", it still loops.
cout << "Would you like to add or subtract these numbers?"
<< endl
<< "(1) for addition"
<< endl
<< "(2) for subtraction"
<< endl
<< "Select one: ";
cin >> decision;
while ((decision != 1) || (decision != 2))
{
cout << "Please pick (1) for addition or (2) for subtraction: ";
cin >> decision;
}
if (decision == 1)
{
cout << firstNum << " + " << secondNum << " = " << firstNum + secondNum;
}
else if (decision == 2)
{
cout << firstNum << " - " << secondNum << " = " << firstNum - secondNum;
}
Individually the conditions work i.e.
while (decision != 1)
or
while (decision != 2)
But not when together. Am I using OR operator wrong?
I have only just started learning C++ so please bear with me, any advice would be much appreciated!
That would be while ((decision != 1) && (decision != 2)).
But I recommend this way.
bool decision_check(int decision) {
switch (decision) {
case 1:
cout << firstNum << " + " << secondNum << " = " << firstNum + secondNum;
return true;
case 2:
cout << firstNum << " - " << secondNum << " = " << firstNum - secondNum;
return true;
default:
cout << "Please pick (1) for addition or (2) for subtraction: ";
return false;
}
}
Use this function in while.
Related
I'm ALMOST done with this assignment, but still having trouble getting the code to display the error messages continuously until the correct input is entered.
For example, if the user enters "4" for operation (which should be between 1-3), it correctly displays: "Your operation choice isn't valid! Please try again, using 1, 2, or 3." However, if the user enters another invalid number for the operation (such as 5), it doesn't repeat the error message, but just continues forward.
Anyone able to help me figure out how to get each error message to repeat until valid numbers or characters are entered for each prompt?
Note: I am very new to coding, and still figuring out stackoverflow...I think I have followed all the MCVE suggestions/format. THANK YOU!!
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
{
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " <<
endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
}
while (operation > 3 || operation < 1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: "
<< endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "What is " << num1 << " - " << num2 << " ?: " <<
endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." <<
endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?:
" << endl;
cout << "" << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " <<
endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." <<
endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?:
" << endl;
cout << "" << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
}
do
{
cout << "Would you like to play again? Press Y for yes or Q for
quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
{
cout << "That is not a valid choice. Please choose Y for yes
or Q to quit. " << endl;
cout << "" << endl;
}
}
while(play !='Y' && play !='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}
while(play=='Y');
return 0;
}
/*Sample Run:
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
3
You chose multiplication.
What is 4 * 1 ?:
4
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
1
You chose addition.
What is 6 + 1 ?:
7
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
2
You chose subtraction.
What is 5 - 0 ?:
5
That is correct!
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
1
You chose addition.
What is 7 + 1 ?:
9
That is incorrect. Please try again.
What is 7 + 1 ?:
10
Would you like to play again? Press Y for yes or Q for quit
Y
Thank you for playing! Let's play again!
Choose an operation.
Enter 1 to add, 2 to subtract, or 3 to multiply:
2
You chose subtraction.
What is 7 - 3 ?:
5
That is incorrect. Please try again.
What is 7 - 3 ?:
6
Would you like to play again? Press Y for yes or Q for quit
Q
Thank you for playing! See you next time!
Process returned 0 (0x0) execution time : 43.057 s
Press any key to continue.
*/
P29: Practice Arithmetic Skills (if/else , loop)
Description:
"Write a program to let a child practice arithmetic skills.
The program should first ask for what kind of practice is wanted: +, -, * , and let the user repeat the practice as many times as desired, until "Q" is entered.
Two random numbers will be generated from (0 - 9).
If the child answers the equation correctly, a message should appear , and they can then go to the next problem(two different numbers generated).
If the child answers incorrectly, a message should appear & the problem should be repeated (same numbers used)."
Finally fixed!:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int operation, num3, guess, num1, num2, temp;
char play;
srand(time(0));
do
{
num1 = rand() % 10;
num2 = rand() % 10;
if (num1 < num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
}
}while (operation > 3 || operation < 1);
switch(operation)
{
case 1:
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 2:
cout << "You chose subtraction." << endl;
num3 = num1 - num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
case 3:
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "" << endl;
do
{
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cout << "" << endl;
cin >> guess;
cout << "" << endl;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
}
} while (guess != num3);
if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
break;
}
do
{
cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
cout << "" << endl;
cin >> play;
if (play != 'Y' && play != 'Q')
{
cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
cout << "" << endl;
}
}
while(play !='Y' && play !='Q');
if (play == 'Y')
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
}
while(play=='Y');
return 0;
}
Your do while loop
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
}
while (operation > 3 || operation < 1);
should be
do
{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3."
<< endl;
}
}
while (operation > 3 || operation < 1);
Do not use the do-while loop if you want to give only one reattempt to the user to select the correct operation. Keep that block this way
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try
again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
OR
if you want to print the error message only for first wrong attempt and for all succedding wrong attempts you can do in the following way:
int flag=0;
do{
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "<<endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
if ((operation > 3 || operation < 1)&&flag==0)
{
flag=1;
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "" << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: "
<< endl;
cout << "" << endl;
cin >> operation;
cout << "" << endl;
}
} while(operation > 3 || operation < 1);
My program will compile but I'm running into a couple problems. My first cout statement requiring e/E to end works, but in my second while loop where I state (+ || - || * || /) won't run. +/-/*// returns "Operation type invalid". Can you guys help me see my error?
First sentinel loop, just learning loops:
#include <iostream>
using namespace std;
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation == "e" || "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
while (operation == "+" || operation == "-" || operation == "*" || operation == "/")
{
cout << "Please enter integer one: " << endl;
cin >> numOne;
cout << "Please enter integer two: " << endl;
cin >> numTwo;
if (operation == "+")
{
result = numOne + numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "-")
{
result = numOne - numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "*")
{
result = numOne * numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "/")
{
if (numTwo == 0)
{
cout << "You cannot divide by zero!" << endl;
}
else
{
result = numOne / numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
}
}
return 0;
}
while (operation == "e" || "E")
Here you are comparing one of two conditions:
Does operation == "e"?
If not, is "E" a valid pointer?
That second condition is your problem: "E" is of course a valid pointer, so that condition will always be true. Always. Notice that in the second condition, you are not comparing operation to "E".
You are forever stuck here:
while (operation == "e" || "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
You simply need to have:
while (operation == "e" || operation == "E")
This is probably just a typo or an oversight more than anything else.
This question already has answers here:
Immediate exit of 'while' loop in C++ [closed]
(10 answers)
Closed 6 years ago.
Hey can anyone lend me a hand? How can I make my loop end after the first calculation? After i complete one calculation such as addition, I would like to end it. Sorry for the noobie questions. I'm just learning loops now in my class. Greatly appreciated.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation == "e" || operation == "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
while (operation == "+" || operation == "-" || operation == "*" || operation == "/")
{
cout << "Please enter integer one: ";
cin >> numOne;
cout << "Please enter integer two: ";
cin >> numTwo;
if (operation == "+")
{
result = numOne + numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "-")
{
result = numOne - numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << "." << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "*")
{
result = numOne * numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
else if (operation == "/")
{
if (numTwo == 0)
{
cout << "You cannot divide by zero!" << endl;
}
else
{
result = numOne / numTwo;
cout << "The numbers you entered were " << numOne << "," << numTwo << endl;
cout << "The operation you chose was " << operation << "." << endl;
cout << "The operations result is " << result << endl;
cout << "Your equation was: " << numOne << " " << operation << " " << numTwo << " = " << result << ".";
}
}
}
return 0;
}
I'm not allowed to use break. Is there another way?
You can end a while loop using...
break;
ie
if(!(operation == "e" || operation == "E")) {
break;
}
Normally to end a while loop the (EXPRESSION) in the following ie
while((EXPRESSION) == true) { execute_code_here();}
needs to evaluate to false. The expression can be made up of many logical pieces ie
while (a == b && c != d && f++ < 1000) {do_something_here();}
The break keyword is used to terminate a while loop early but this can also be achieved by adding something to the expression that allows you to terminate the while loop ie make the expression evaluate to false, this is normally done using a flag or a counter.
To achieve the same effect as break without using it you can use this technique with continue. You then use the flag or counter along with continue to achieve the same thing as break ie, this is a counter example
while(a == b && flag++ < 1000) {
if(this_returns_true()) {
flag = 10000;
continue;
}
/*The code here may not be executed*/
}
Using a boolean flag it would be...
while(a == b && flag == true) {
if(this_returns_true()) {
flag = false;
continue;
}
/*The code here may not be executed*/
}
Just change the while to if. There's no need for a loop if you only want to execute something once.
You can use break; with an if statement inside the while loop. So that if a condition is true you will break out from the while loop.
Example
if (operation != "e")
break;
If you're not allowed to use break, I suggest you don't use a loop. And just calculate it once.
I am not very experienced with C++ but I think you mean the break; statement
https://msdn.microsoft.com/library/37zc9d2w.aspx
Use
break;
This will end the loop each time. You can wrap an if statement around it to apply conditions for exiting.
If you have to use the while loop just add break; and that will break out of the loop. If you don't have to I would change that to an if statement. Example: if(varName == '+'){
Num1 + num2;}
Sorry for format, on mobile
The code block:
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation == "e" || operation == "E")
{
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
Doesn't do what you're promising the user:
Please enter what operation you'd like to perform or e/E to end program:
If "e" or "E" is entered, the loop won't stop and ask for input again.
You probably want to have something like this:
do {
cin >> operation;
while (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// ...
}
} while(operation != "e" && operation != "E");
As for your silly restrictions not to use do {} while(); (kick your professor in the ass, do twice at least):
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while(operation != "e" && operation != "E") {
if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// ...
}
else {
cout << "Operation type invalid." << endl;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
}
I guess that what you are actually trying to achieve is this.
The first loop should be refactored (ie: loop until operation is nether 'e' nor 'E'), the second loop should be an if-else-statement (checking whether the operation is valid).
int main()
{
int numOne;
int numTwo;
int result;
string operation;
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
while (operation != "e" && operation != "E") {
if (operation == "+" || operation == "-" || operation == "*" || operation == "/") {
// your valid operation code...
} else {
cout << "Operation type invalid." << endl;
}
cout << "Please enter what operation you'd like to perform or e/E to end program: ";
cin >> operation;
}
return 0;
}
I need to use -1 to terminate but still display the summary.
Every time that I've tried and have gotten it to terminate the program, it doesn't go ahead and display the summary.
There are up to 10 trials, if you don't have enough information for 10 and you want to stop at 8, you type -1 and it goes to the summary and then terminates the program
while(i<10)
{
do
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
cin >> score[i];
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=50&& score[i] <=59)
{
cout << "Grade " << "P" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=60 && score[i] <=69)
{
cout << "Grade " << "C" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=70 && score[i] <=89)
{
cout << "Grade " << "B" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=90 && score[i] <=100)
{
cout << "Grade " << "A" << " will be assigned to this result\n";
bool test=true;
i++;
}
else
{
test=false;
cout << "Invalid Input!\n";
}
}
while(test);
}
cout << "\nSummary of the results:\n";
for(int a=0;a< 10;a++)
{
std::cout << std::fixed << std::setprecision(2) << "Result " << a+1 << " " << score[a] << " Grade " << determine_grade(score[a]) << "\n";
}
cout << "\nThe average of the results = " << calc_average(score) << "\n";
cout << "The lowest of the results = " << find_lowest(score) << "\n";
cout << "The highest of the results = " << find_highest(score) << "\n";
system("Pause");
You don't want two loops, only one. You need to combine your two conditions into one i<10 && test.
Also you have declared your test variable in the wrong places. You should declare it once at the beginning of your loop.
bool test = true;
while(i<10 && test)
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this result\n";
i++;
}
...
else
{
test=false;
cout << "Invalid Input!\n";
}
}
Try to use break; when -1 is inputted inside the while loop. Also, you can use 1 loop, instead of two as john mentioned above.
Another thing to look for is your last for loop, it goes from 0 to 9, but in case someone used -1 and only inputted 3 grades, there might be odd values for the solutions.
I built a simple calculator out of c++, it uses chars, so (+,-*,/) will works as math operators but when a user inputs an '=' it does't work.
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
#define PI 3.14159265359
#define NEWLINE '\n'
void printf(string string)
{
cout << string;
}
int main ()
{
char operation;
double a,b,c, value;
double answer = 0;
bool more = true;
cout << "Welcome to My Calculator\n\nInput a value: ";
cin >> answer;
operations:
cin >> operation;
if (operation != '=') {
if (operation == '+') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer += value;
cout << "Equals " << answer << "\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '-') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer -= value;
cout << "Equals " << answer << "\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '*') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer *= value;
cout << "Equals " << answer << "\n\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '/') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer /= value;
cout << "Equals " << answer << "\n\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '^') {
cin >> value;
cout << "\n" << answer << " " << operation << " " << value << "\n";
answer = pow(answer, value);
cout << "Equals " << answer << "\n\n";
cout << answer << " - New Operation? ";
goto operations;
}
if (operation == '=') {
cout << "\nFinal Answer = " << answer << "\n\nNew operation [yes/no]: ";
string check;
cin >> check;
if (check == "yes") {
cout << "\nInput value: ";
cin >> answer;
cout << "\n";
goto operations;
} else {
cout << "\nGoodbye for now...\n";
return 0;
}
}
} else {
cout << "Unknown Error! Program Closing...";
return 0;
}
return 0;
}
When a user uses any operation besides = it works perfectly, but if I use and equals sign it doesn't work.
Example program out put:
Welcome to My Calculator
Input a value: 4
+4
4 + 4
Equals 8
8 - New Operation? - 3
8 - 3
Equals 5
5 - New Operation? * 5
5 * 5
Equals 25
25 - New Operation? /2
25 / 2
Equals 12.5
12.5 - New Operation? ^2
12.5 ^ 2
Equals 156.25
156.25 - New Operation? =
Unknown Error! Program Closing...
if (operation != '=') {
...
if (operation == '=') {
}
}
If operation isn't equal to "=" and if it's equal to "=". I think you planned to put a closure operator or something like that in the first outer if.
your statement if (operation != '=') causes the control to be transferred to the else statement
Because you have an if (operation != '=') outside the if checking for =. You don't want to do that.