I am very new to C++.
This question is relating to my Coursework, I am not seeking a full solution but a step in the right direction.
I can not find anything online for this (may be searching the wrong terms?).
I have created a simple calculator but now need it to store the last result to be called if needed. So if the user wanted 3+3, I would like the answer 6 to be stored, the user can then retrieve the result if needed.
int op;
float numA, numB, result;
int main()
{
cout << "Calculator V1\n By Jolene Stanley\n\n";
while (true)
{
cout << "1. Add\n" <<
"2. Subtract\n" <<
"3. Divide\n" <<
"4. Multiply\n" <<
"5. Square Root\n" <<
"6. Power\n" <<
"7. Exit\n\n" <<
"Please choose an option ";
//Creating an exit option for the user
cin >> op;
if (op == 7)
break;
if (op == 5)
{
cout << "\nPlease enter a number to Square Root: ";
cin >> numA;
result = sqrt(numA);
cout << "\nThe Square Root of " << numA << " is: " << result << "\n" << endl;
}
//Obtaining user input
cout << "\nPlease enter the first number: ";
cin >> numA;
cout << "Please enter the second number: ";
cin >> numB;
//Switch statement provides a result depending on input and outputs to the screen
switch (op)
{
case 1:
result = numA + numB;
cout << "The answer is: " << result << "\n\n";
break;
case 2:
result = numA - numB;
cout << "The answer is: " << result << "\n\n";
break;
case 3:
result = numA / numB;
cout << "The answer is: " << result << "\n\n";
break;
case 4:
result = numA*numB;
cout << "The answer is: " << result << "\n\n";
break;
case 5:
result = sqrt(numA);
cout << "The answer is: " << result << "\n\n";
break;
case 6:
result = pow(numA, numB);
cout << "The answer is: " << result << "\n\n";
break;
}
}
return 0;
}
I have edited the code so the result variable is used.
This worked, I just need to get it so it doesn't prompt the user to enter first and second number still (Same issue with the Square root!).
Your initial response helped me think about the issue, thank you!
int op;
float numA, numB, result;
int main()
{
cout << "Calculator V1\n By Jolene Stanley\n\n";
while (true)
{
cout << "1. Add\n" <<
"2. Subtract\n" <<
"3. Divide\n" <<
"4. Multiply\n" <<
"5. Square Root\n" <<
"6. Power\n" <<
"7. Recall last result\n" <<
"8. Exit\n\n" <<
"Please choose an option ";
//Creating an exit option for the user
cin >> op;
if (op == 8)
break;
if (op == 5)
{
cout << "\nPlease enter a number to Square Root: ";
cin >> numA;
result = sqrt(numA);
cout << "\nThe Square Root of " << numA << " is: " << result << "\n" << endl;
}
//Obtaining user input
cout << "\nPlease enter the first number: ";
cin >> numA;
cout << "Please enter the second number: ";
cin >> numB;
//Switch statement provides a result depending on input and outputs to the screen
switch (op)
{
case 1:
result = numA + numB;
cout << "The answer is: " << result << "\n\n";
break;
case 2:
result = numA - numB;
cout << "The answer is: " << result << "\n\n";
break;
case 3:
result = numA / numB;
cout << "The answer is: " << result << "\n\n";
break;
case 4:
result = numA*numB;
cout << "The answer is: " << result << "\n\n";
break;
case 5:
result = sqrt(numA);
cout << "The answer is: " << result << "\n\n";
break;
case 6:
result = pow(numA, numB);
cout << "The answer is: " << result << "\n\n";
break;
case 7:
cout << "The last result was: " << result << "\n\n";
break;
}
}
return 0;
}