Hello yet again!
I seem to be having a problem with a logical OR inside a while loop.
Basically, I've got it taking user input for a string, and I want them to type 'exit' to... well, exit.
Here's a shorter non-working version of my code:
[Code=cpp]
#include <iostream>
#include <string>
int main()
{
std::string str( " " );
std::cout << "Enter some text. \"exit\" to quit. ";
while( str != "exit" || str != "Exit" )
{
std::cin >> str;
std::cout << "You entered: " << str << "\n";
}
return 0;
}
[/Code]
Ok, I understand why it doesn't work if I type in: Exit. ( because of the short-circuit effect. )
But, shouldn't it stop the loop if you type in: exit?
This has been driving me crazy for hours, and everytime I search the site for similar problems,
It searches for one, or two of the words I enter, and tells me the others give to broad a search. Heh.
Anyway, I appreciate the help, and if you can point me in the direction of a better
way to implement this loop, I would appreciate that also.
Is there a better way to take a single exiting word without having to check for
every possible letter combination?
( exit, Exit, eXit, ExIt, etc. )
I guess I could put a 'continue' in there, or maybe a break. What do ya think?
Ok, I'm outs... Lates! ( Oh, and thanks again! )
-Soneji
I seem to be having a problem with a logical OR inside a while loop.
Basically, I've got it taking user input for a string, and I want them to type 'exit' to... well, exit.
Here's a shorter non-working version of my code:
[Code=cpp]
#include <iostream>
#include <string>
int main()
{
std::string str( " " );
std::cout << "Enter some text. \"exit\" to quit. ";
while( str != "exit" || str != "Exit" )
{
std::cin >> str;
std::cout << "You entered: " << str << "\n";
}
return 0;
}
[/Code]
Ok, I understand why it doesn't work if I type in: Exit. ( because of the short-circuit effect. )
But, shouldn't it stop the loop if you type in: exit?
This has been driving me crazy for hours, and everytime I search the site for similar problems,
It searches for one, or two of the words I enter, and tells me the others give to broad a search. Heh.
Anyway, I appreciate the help, and if you can point me in the direction of a better
way to implement this loop, I would appreciate that also.
Is there a better way to take a single exiting word without having to check for
every possible letter combination?
( exit, Exit, eXit, ExIt, etc. )
I guess I could put a 'continue' in there, or maybe a break. What do ya think?
Ok, I'm outs... Lates! ( Oh, and thanks again! )
-Soneji
Comment