While loop is not exiting

Sep 10, 2012 at 12:44am
string cmd;
cout << "skynet@2.0: ";
cin >> cmd;
while(cmd != "Exit" || cmd != "EXIT" || cmd != "exit")
{
cout << "skynet@2.0: ";
cin >> cmd;
if(cmd == "help" || cmd == "HELP" || cmd == "Help")
help_cmd();
cout << "skynet@2.0: ";
cin >> cmd;

if (cmd == "version" || cmd == "Version" || cmd == "VERSION")
version_cmd();

if (cmd == "Date" || cmd == "date" || cmd == "DATE")
date_cmd();

else
cout << "Invaild command." << endl;
}
Sep 10, 2012 at 12:58am
post the whole code. urs is messy but it should work
Sep 10, 2012 at 1:00am
#include <string>
#include <iostream>
using namespace std;

void help_cmd();
void version_cmd();
void date_cmd();

int main()
{
cout << "Welcome to Skynet 2.0!" << endl << endl;
string cmd="";
while(cmd != "Exit" || cmd != "EXIT" || cmd != "exit")
{
cout << "skynet@2.0: ";
cin >> cmd;
if(cmd == "help" || cmd == "HELP" || cmd == "Help")
help_cmd();

if (cmd == "version" || cmd == "Version" || cmd == "VERSION")
version_cmd();

if (cmd == "Date" || cmd == "date" || cmd == "DATE")
date_cmd();

else
cout << "Invaild command." << endl;

}
cout << "Have a nice day! Goodbye.";
return 0;
}

void help_cmd()
{
cout << "help - Display list of all commands with description" << endl;
cout << "version - Displays current version " << endl;
cout << "date - Display current date or change to new date" << endl;
cout << "exit - exit out of program " << endl;
}

void version_cmd()
{
cout << "The current version is 2.0" << endl;
}

void date_cmd()
{
cout << "date";
}
Sep 10, 2012 at 1:07am
try this:

while(true)
{
getline(cin, cmd);
//other features
if(cmd == "exit")
break;
}

i dont know whats wrong with urs though it can be written better
Sep 10, 2012 at 1:07am
I'm not sure why it isn't working. It really doesn't make any sense. >_>
You should just do what the other user suggested until I, or another member, find working solution.
Last edited on Sep 10, 2012 at 1:08am
Sep 10, 2012 at 1:18am
ok thanks so much!
Sep 10, 2012 at 1:33am
while(cmd != "exit" && cmd != "EXIT" && cmd != "Exit")
I should have really noticed this, and I apologize for that. You don't want to use the || operator, you want to use the && operator. That should fix the problem.

Here is your fixed code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
#include <iostream>
using namespace std;

void help_cmd();
void version_cmd();
void date_cmd();

int main()
{
    cout << "Welcome to Skynet 2.0!" << endl << endl;
    string cmd;
    while(cmd != "exit" && cmd != "EXIT" && cmd != "Exit")
    {
        cout << "skynet@2.0: ";
        cin >> cmd;
        if(cmd == "help" || cmd == "HELP" || cmd == "Help")
            help_cmd();

        else if(cmd == "version" || cmd == "Version" || cmd == "VERSION")
            version_cmd();

        else if(cmd == "Date" || cmd == "date" || cmd == "DATE")
            date_cmd();
    }
    cout << "Have a nice day! Goodbye.";
    return 0;
}

void help_cmd()
{
    cout << "help - Display list of all commands with description" << endl;
    cout << "version - Displays current version " << endl;
    cout << "date - Display current date or change to new date" << endl;
    cout << "exit - exit out of program " << endl;
}

void version_cmd()
{
    cout << "The current version is 2.0" << endl;
}

void date_cmd()
{
    cout << "date";
}
Last edited on Sep 10, 2012 at 2:16am
Sep 10, 2012 at 2:08am
no thats wron... i tested and it works but why does || logic not work?
Sep 10, 2012 at 2:13am
i tested and it works but why does || logic not work?


Remember that || returns true if either the right or left side of it is true

And the while loop will keep looping as long as the condition is true.

So given the below:

while(cmd != "Exit" || cmd != "EXIT")

this means the loop will keep going if
cmd != "Exit" OR cmd != "EXIT".

Which means the only way for the loop to exit would be if both of those conditions are false... which means cmd would have to be both "Exit" and "EXIT" -- which is impossible.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
cmd = "Exit";

while(cmd != "Exit" || cmd != "EXIT")
while(   false      ||    true      ) // <- doesn't match "EXIT"
while(             true             ) // <- keep going

//..... another.....

cmd = "EXIT";

while(cmd != "Exit" || cmd != "EXIT")
while(    true      ||    false     ) // <- doesn't match "Exit"
while(             true             ) // <- keep going 
Sep 10, 2012 at 2:24am
oh..... i feel like an idiot. logic can be very confusing sometimes
Sep 10, 2012 at 2:30am
I really should have explained how that works. :F
Topic archived. No new replies allowed.