Reading a bool from cin is probably going to evaluate to true every time. Instead, ask the user for a value of "yes" or "no" in a string format, then check which input you receive.
Issues With Infinite While loop C++
-
13-07-2023 - |
Pergunta
I've tried several things suggested on stackoverflow such as break statements and incrementing a number in addition to the pre-existing condition and have had no success.
This code results in an infinite output after a doctor is selected, REGARDLESS of what is entered when the user is asked if they wish to continue.
void ClinicDriver::OpenQueue()//see comment at bottom
{
bool cont = true;
while (cont == true)
{
int QueueChoice;
for (int a = 0; a < 6; a++)
{
cout << Doctors[a] << endl;
}
cout << "Select a Doctor by number." << endl;
cin >> QueueChoice;
status[QueueChoice - 1] = true;
cout << "Do you wish to continue?" << endl;
cin >> cont;
}
}
And this code tells me I've made an access violation and blows up when trying to change part of the array.
void ClinicDriver::OpenQueue()
{
bool cont = true;
int n = 0;
while (cont == true && n < 6)
{
int QueueChoice;
for (int a = 0; a < 6; a++)
{
cout << Doctors[a] << endl;
}
cout << "Select a Doctor by number." << endl;
cin >> QueueChoice;
status[QueueChoice - 1] = true; //blows up here
cout << "Do you wish to continue?" << endl;
cin >> cont;
n++;
}
}
Any help or advice is greatly appreciated, thanks.
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow