I am trying to go above and beyond for this program by creating a loop with multiple exits. If the user enters Y ( loop is restarted ), if user enters N ( program is term'd ), if user enters P ( a prompt will ask to enter new precision and reenter the calculation loop. I have used a goto statement to get the program to work but I would like to know how to get it work without the goto. Can anyone assist me with this? Any suggestions would be greatly appreciated.
cout << "\rEnter Expression : ";
cin >> operand1 >> math >> operand2; // read expression for calculation
// switch starts here
switch (math)
{
case '+': // addition
result = operand1 + operand2;
break; // exits switch
case '-': // subtraction
result = operand1 - operand2;
break;
case '*': // multiplication
result = operand1 * operand2;
break;
case '/': // division
result = operand1 / operand2;
if (operand2 == 0 ) // if user enters a 0 to divide by
{
cout << "ERROR: Division by zero error, denominator cannot be '0'!!!";
result = 0;
}
else break; // end if... else, restart loop
case '^': // power (x, y)
result = pow(operand1, operand2);
break;
case 'x': // improper mulitiplication operator
cout << "ERROR! 'x' is an invalid operator!!!";
break;
// Result of Calculation
cout << "\nResult : " << setprecision(precision) << result << endl;
cout << "\r\nEnter another expression <Y/N> or change precision <P> ? ";
cin >> answer; // read Y/N or P
} while ((answer != 'N') && (answer != 'n')
&& (answer != 'p') && (answer != 'P')); // end do while
if (answer == 'p' && 'P') // if statement to decide if program exits or prompts
{
cout << "Current precision is " << precision << " decimal places\n"
<< "Please enter the desired display precision : ";
cin >> precision; // read new precision
goto expression; // reenter loop
} else
cout << "GoodBye!!!"; // If users enters 'n' or 'N' exit program.
<> code block!
Use functions and loop them with a single while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
while(1)
{
// Display My Simple Calulator
//'expression' (without the dowhile) and return 'result' from it
//u could put most of the rest into another function (another switch ?)
cout << "\r\nEnter another expression <Y/N> or change precision <P> ? ";
cin >> answer;
switch(answer)
{
//...
//return value
}
//loop again or break depending on return
}