Я работаю над созданием меню с проверкой ошибок, и это то, что я придумал, но я не могу заставить его работать.Почему мой цикл while-while не зацикливается?
#include <iostream>
using namespace std;
int main()
{
char option; // user's entered option will be saved in this variable
int error1 = 0;
//Displaying Options for the menu
cout << "There are three packages available." << endl;
cout << "Monthly Price - Price per MB for overages (excluding C)" << endl;
cout << "A) $15 - $.06 for each MB after 200 MB." << endl;
cout << "B) $25 - $.02 for each MB after 2,000 MB (approx. 2 GB)." << endl;
cout << "C) $50 - Unlimited data." << endl;
do //do-while loop starts here
{
//Prompting user to enter an option according to menu
cout << "Please enter which plan you currently have : ";
cin >> option; // taking option value as input and saving in variable "option"
if(option == 'A' || option == 'a') // Checking if user selected option 1
{
cout << "You chose a" << endl;
error1 = 1;
}
else if(option == 'B' || option == 'b') // Checking if user selected option 2
{
cout << "You chose b" << endl;
error1 = 1;
}
else if(option == 'C' || option == 'c') // Checking if user selected option 3
{
cout << "You chose c" << endl;
error1 = 1;
}
else //if user has entered invalid choice
{
//Displaying error message
error1 = 0;
cout << "Invalid Option entered";
}
}
while(error1 = 0); //condition of do-while loop
return 0;
}
При вводе неправильного значения на выходе будет введена Неверная опция; однако он не будет возвращаться к началу и снова запрашивает пользователя для ввода.
Почему это делается?
изменение while (error1 = 0); в while (error1 == 0); –
Это просто опечатка, которую вы назначили вместо проверки на равенство. – shuttle87
Поднимите уровни предупреждения на свой компилятор. –