Циклы C++: как заставить программу повторять сообщение "Ошибка" более одного раза в любое время, когда пользователь вводит неверный ввод
Всем привет,
Я почти закончил с этим заданием, но все еще испытываю проблемы с получением кода для непрерывного отображения сообщений об ошибках до тех пор, пока не будет введен правильный ввод.
Например, если пользователь вводит "4" для операции (которая должна быть между 1-3), он правильно отображает: "ваш выбор операции недействителен! Пожалуйста, попробуйте еще раз, используя 1, 2 или 3." Однако если пользователь вводит другой недопустимый номер для операции (например, 5), он не повторяет сообщение об ошибке, а просто продолжает движение вперед.
Кто-нибудь может помочь мне понять, как заставить каждое сообщение об ошибке повторяться до тех пор, пока для каждого запроса не будут введены допустимые числа или символы?
Примечание: Я очень новичок в кодировании, поэтому, пожалуйста, используйте простые объяснения или примеры в коде, если это возможно. СПАСИБО!!
Что я уже пробовал:
#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. */