Do While Loop Won't End

1
2
3
4
5
6
7
8
9
10
11
12
char ans;
cout << "Do you want to search for an account again?:"<<endl;
cout << "Y/N or y/n: "<<endl;
cin >> ans;

do
{

**random code**

}
while((ans !='Y')||(ans !='y'));

I just don't get it, I've tried many ways.
Last edited on
ans doesn't change in your loop. So it'll always be in a loop.

Also it should be
while ( (ans != 'Y') && (ans != 'y'));
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
char ans;
cout << "Do you want to search for an account again?:"<<endl;
cout << "Y/N or y/n: "<<endl;


do
{
cin >> ans;
**random code**

}
while ( (ans != 'Y') && (ans != 'y'));


So, it should go like that?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char ans;
cout << "Do you want to search for an account again?:"<<endl;
cout << "Y/N or y/n: "<<endl;
cin >> ans;

while ( (ans != 'Y') && (ans != 'y'))
{

**random code**

cout << "Do you want to search for an account again?:"<<endl;
cout << "Y/N or y/n: "<<endl;
cin >> ans;
}
However, a better way would be to put the question into a function

e.g
bool askSearchAgain();

then use
1
2
3
while (askSearchAgain()) {
 // random code
}
Last edited on
Thanks!
Topic archived. No new replies allowed.