Loops and conditionals



I have been at this for a while and I just cant seem to figure out why this is not able to exit the loop once I input STOP. The value for more changes in the loop. I would appreciate any help, many thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main(){

std::string STOP, n, more="yes";

while(more=="yes"){

std::cout<< " please enter a word" << std::endl;
std::cin >> n;

if(n==STOP){
    more="no";
}

else{
  std::cout << n << std::endl;
}

}

return 0;
}


The idea is that more would be yes to begin with to enter the loop. After this the question is asked and whatever is inputted would be repeated and the question asked again however if what is input is the word STOP this would exit the loop and it ends.
Last edited on
Line 6 declares a VARIABLE called STOP, with default value "". i.e. the empty string.

So line 13 will only be true if you enter an empty string on line 11!

Calling a variable STOP does not give it the value "STOP".
Last edited on
YES! I have just adjusted the value of STOP to STOP and now it does exit the loop. Thank you. Not sure if having 3 variables to do this is the most efficient way though.
Topic archived. No new replies allowed.