У меня много проблем с этим заданием для моего первого курса C ++.
Я выяснил, как заставить его правильно спросить пользователя, какую операцию он хотел бы использовать (сложение, вычитание, умножить), генерировать случайные числа в диапазоне от 0 до 9 и узнать, как попросить пользователя решить проблему и ответить, если она правильная или неправильная.
После этого момента программа должна спросить пользователя, хочет ли он продолжить (нажав y) или выйти (нажав Q), с сообщением об ошибке для пользователя при вводе любой другой буквы., но по какой-то причине эта часть не отображается при запуске программы.
Как мне заставить цикл работать правильно, позволяя мне сделать последнее приглашение, а затем повторить всю программу только при нажатии Yили выйти при нажатии Q?
Примечание: я ОЧЕНЬ новичок в программировании в целом, и это мой самый первый курс C ++, поэтому я пока не знаю, как создать этот кодболее кратко:
#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
while (true)
{
// Generate two random single-digit integers btwn 0-9
srand(time(0));
int num1 = rand() % 10;
int num2 = rand() % 10;
int operation, play, num3, guess, Y, Q;
// If num1 < num2, swap num1 with num2
if (num1 < num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
}
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
if (operation > 3 || operation < 1)
{
cout << "Your operation choice isn't valid! Please try again, using 1, 2, or 3." << endl;
cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;
}
else if (operation == 1)
{
cout << "You chose addition." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " + " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
else if (operation == 2)
{
cout << "You chose subtraction." << endl;
num3 = num1 + num2;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " - " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
else if (operation == 3)
{
cout << "You chose multiplication." << endl;
num3 = num1 * num2;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
if (guess != num3)
{
cout << "That is incorrect. Please try again." << endl;
cout << "" << endl;
cout << "What is " << num1 << " * " << num2 << " ?: " << endl;
cin >> guess;
}
else if (guess == num3)
{
cout << "That is correct!" << endl;
cout << "" << endl;
}
}
while (guess != num3)
{
int play, Y, Q;
cout << "Would you like to play again? Press Y for yes or Q for
quit" << 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;
cin >> play;
}
else
{
if (play == Y)
{
cout << "Thank you for playing! Let's play again!" << endl;
cout << "" << endl;
}
else if (play == Q)
{
cout << "Thank you for playing! See you next time!" << endl;
cout << "" << endl;
}
break;
}
}
}
return 0;
}