tacotuesday
tacotuesday

Reputation: 135

Infinite While loop in C++

I am teaching myself c++ and am building a simple menu program. Im a complete noob to C++ so I apologize in advance for the question if it seems foolish. My code continuously calls getNum() and never exits, despite the correct menu selections. What am I doing wrong? Here is my code:

#include <iostream>
#include <string>
using namespace std;

void calc();
void pass();
string getNum(string num);

int main(int argc, const char * argv[])
{
    string num = "0";
    string menu = "Enter... \n 1 For calculator \n 2 for Passwords";
    cout << "Hello this is a sample menu program" << endl;


    while(num != "1" || num != "2")
    {
    getNum(num);
    cout << "You selected: " << num << endl;
    }

    if(num == "1"){
        calc();
    }
    else {
        pass();
    }
}
void calc() {
    cout << " You are running the calculator" << endl;
}
void pass() {
    cout << "You are running passwords" << endl;
}
string getNum(string num) {
    cout << "What would you like to do?" << endl;
    cout << "Enter... \n 1 For calculator \n 2 for Passwords" << endl;
    getline(cin, num);
    return num;
    }

Upvotes: 0

Views: 416

Answers (5)

ransey.bautista
ransey.bautista

Reputation: 51

this condition while(num != "1" || num != "2") will always return true for all inputs. for example, "1", it is false on num != "1" but will still return true on the other condition num != 2, thus it will never exit (true || false == true)

also it should be

else if(num == '2') {
        pass();
    }

correct condition should be while(!(num != "1" && num != "2")) to only allow "1" and "2" inputs

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

Change this condition

while(num != "1" || num != "2")

to

while(num != "1" && num != "2")

To write the condition correctly you should consider that the loop should not be repeated if num either equal to "1" or "2". This condition can be written as

num == "1" || num == "2"

However the loop shall be repeated if this condition is not true. So the condition for the loop will be repeated will look like

! ( num == "1" || num == "2" )

According to the mathematical logic this condition is equivalent to

num != "1" && num != "2"

It would be better to rewrite the loop like

string num;

//...

do 
{
    getNum(num);
    cout << "You selected: " << num << endl;
} while ( num != "1" && num != "2")

At least one iteration should be done in any case should not it?

Also define the function as

void getNum(string &num);

//...

void getNum( string &num) {
    cout << "What would you like to do?" << endl;
    cout << "Enter... \n 1 For calculator \n 2 for Passwords" << endl;
    getline(cin, num);
    }

Upvotes: 4

Amit
Amit

Reputation: 31

You have written while(num != "1" || num != "2")

so either num would be equal to "1" or "2" or none of these. In any condition one of these condition is true( i.e. either num is not equals to 1 , this is true or num is not equals to 2 , this is true), therefore your while loop is always true and continuously run without stop

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

Line

while(num != "1" || num != "2")

be DeMorgan laws is the same as

while(!(num == "1" && num == "2"))

So you need

 (num == "1" && num == "2")

to be true

Hence the difficulty

apart from the other bits of the code that are in error

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 362147

getNum(num);

In getNum you have a return statement that returns what the user entered. When you call getNum you don't save that return value.

num = getNum(num);

To make things clearer, I would remove the input parameter from getNum. You don't need to pass anything to it since it's job is to prompt for a number and return that number.

string getNum() {
    cout << "What would you like to do?" << endl;
    cout << "Enter... \n 1 For calculator \n 2 for Passwords" << endl;

    string num;
    getline(cin, num);
    return num;
}

Then change the call to:

num = getNum();

Another issue is your loop condition.

while(num != "1" || num != "2")

Think about what this will do if num were, say, "1". You'd want the loop to stop, right? Look what happens if we evaluate and reduce the expression bit by bit:

while(num != "1" || num != "2")
while("1" != "1" || "1" != "2")
while(false || true)
while(true)

Well that's not right. It should have evaluated to false. The || should be &&.

while(num != "1" && num != "2")

Upvotes: 1

Related Questions