Hi, Im trying to make a switchcase calculator but I don't know how to make my other cases loop indefinitely while setting one of my cases as a breakpoint when prompted. Any suggestions?
Do you want the entire program to keep looping until the user wants to quit or do you want to keep looping inside the case with the same operation but different numbers?
All the case statements that end in return 0; cause the program to end before you have a chance to read the answer. All thereturn 0; s need to be changed tobreak;s. In order to keep the program running until 'x' is typed you will need somehing like this:
1 2 3 4 5 6
bool cont{ true };
do
{
} while (cont);
Put lines 13 to 54inside the "{}" of the do while loop. This will allow to program to loop until you type 'x'. You will need to add the line cont = false; to case 'x' to exit the do while loop. This works for the whole program, but the same concept will work inside the case statements.
Whether you havereturn 0; or break; you will exit the program or exit the switch
respectively. What I use to pause the program is: std::this_thread::sleep_for(std::chrono::seconds(5)); this will require the header files "chrono" and "thread".
I have used case '-' as an example:
1 2 3 4 5 6 7 8 9
case'-':
std::cout << "\n enter your numbers" << std::endl;
std::cout << "\n number 1: ";
std::cin >> number1;
std::cout << "\n number 2: ";
std::cin >> number2;
std::cout << "\n This is the answer: " << number1 << " - " << number2 << " = " << number1 - number2 << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
break;