Infinite loops issue

when i compile and run this, It continually loops and never ends, can someone explain why?

string answer = "yes";
while(answer[0] != 'y' || answer[0] != 'Y')
{
system("clear");
geography(plains, forest, river, mountain);
cout<<"Plains: "<<plains<<endl
<<"Forest: "<<forest<<endl
<<"River: "<<river<<endl
<<"Mountain: "<<mountain<<endl;
cout<<"Settle here?"<<endl;
cin>>answer;
}
Your while condition is faulty.
If answer[0] == 'Y', then answer[0] != 'y' will be true.
If answer[0] == 'y', then answer[0] != 'Y' will be true.
What you want is:
 
while (answer[0] == 'y' || answer[0] == 'Y')


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I changed the variable answer to "no" and used && instead of or


1
2
3
4
5
6
7
8
9
10
11
12
string answer = "no";
while(answer[0] != 'y' && answer[0] != 'Y')
{
system("clear");
geography(plains, forest, river, mountain);
cout<<"Plains: "<<plains<<endl
<<"Forest: "<<forest<<endl
<<"River: "<<river<<endl
<<"Mountain: "<<mountain<<endl;
cout<<"Settle here?"<<endl;
cin>>answer;
}
Last edited on
Topic archived. No new replies allowed.