1

Here I set up a while loop where I input a selection number 1 or 2. If I choose 1 or 2 it couts the 1 or 2 whatever I choose; if I choose a number below 1 or above 2 it goes to the else statement which asks the user to input again. But if the user inputs the problem is that it won't loop back to the beginning if they choose the right number 1 or 2; it will just end the loop there and not go to the correct if clause. Why is this? I feel as though I attacked it right.

int menus (int selection)
{
    while ( selection > 2 ||  selection < 1)
    {
        if (selection == 1)
        {
            cout << " 1 " << endl;
        }
        if (selection == 2)
        {
        cout << " 2 " << endl;
        }
        else
            cout << "You didnt choose 2 or 3. Choose again";
        cin >> selection;
    }
    return 0;
}

8 Answers 8

3

Your loop conditions are contorted.

You only go through the loop if selection is 3, 4, 5, ... (because of selection > 2) or if selection is 0, -1, -2, ... (because of selection < 1). So, you never get into either if; you go to the else. You then unconditionally input a new selection before redoing the loop. If you now enter a valid value, the loop terminates. You might also notice that your message doesn't match what you want the user to supply.

Your code, when indented correctly, is rather similar to this:

int menus (int selection)
{
    while (selection > 2 || selection < 1)
    {
        if (selection == 1)
        {
            cout << " 1 " << endl;
        }
        else if (selection == 2)
        {
            cout << " 2 " << endl;
        }
        else
            cout << "You didn't choose 1 or 2. Choose again: ";
        cin >> selection;
    }
    return 0;
}

If you really want the user to choose either 1 or 2 and to return their choice, then you need something more like:

int menus (int selection)
{
    while (selection > 2 || selection < 1)
    {
        cout << "You didn't choose 1 or 2. Choose again: ";
        cin >> selection;
    }
    if (selection == 1)
        cout << " 1 " << endl;
    else if (selection == 2)
        cout << " 2 " << endl;
    return selection;
}

There are still issues like 'what happens on EOF?' that should be dealt with. And the printing can be streamlined, of course:

int menus (int selection)
{
    while (selection > 2 || selection < 1)
    {
        cout << "You didn't choose 1 or 2. Choose again: ";
        cin >> selection;
    }
    cout << " " << selection << " " << endl;
    return selection;
}
Sign up to request clarification or add additional context in comments.

1 Comment

So how can i attack this imk inda of confused.
2

Take a close look at your 'while' condition: it will only loop if 'selection' is bigger than two or less than 1. Then you check if the selection is 1 or 2 - what you've just ruled out. Also there is no input command in the loop.

4 Comments

And so 'selection' will never change. If the while condition evaluates to true you will have an endless loop.
It would have to change during the loop. So the input command must be inside your loop.
So i would change the selection > 2 || selection < 1 part?
First, the value of selection must change inside the loop - so the input command needs to be inside your loop. Second, yes, you need to think for a moment about the condition you're evaluating.
2

use

do{

}while( selection > 2 ||  selection < 1);

instead of while.

Comments

2

In what I can see, you are passing the selection into the function. The while loop will run only if the input is not 1 or 2.

Hence change the code to:

int menus (int selection)
{
  while (true)
 {
  if (selection == 1 || selection == 2)
  {
     cout << selection << endl;
     break;
  }
  else
  {
     cout << "You didnt choose 1 or 2. Choose again";
     cin >> selection;
  }
 }

 return 0;
}

Comments

2

Your while loop excludes values that you want to test for inside the loop!

while ( selection > 2 ||  selection < 1)
{
if (selection == 1)
{
    // Impossible!! selection isn't > 2 or < 1.
}

You could do this instead:

while (1)
{
    switch (selection) {
        case 1:
        case 2:
            cout << " " << selection << " " << endl;
            return 0;
        default:
            cout << "You didnt choose 1 or 2. Choose again";
            cin >> selection;
    }
}

I don't see why selection is a parameter to your function though. It's hard to tell if that's sensible without more code.

1 Comment

So what exactly do i out in the while loop?
1

call int menus() inside else like this:

else{
cout << "You didnt choose 2 or 3. Choose again";
cin >> selection;
menus(selection);
}

1 Comment

-1: Unnecessary recursive call to menus() function. It's better to correct the while condition.
1

If they choose a valid number, that number no longer meets the conditional requirements of your loop. You would find it easier looping on a bool that you can set if the user fails too many times, else, you can use do{ /* get the input, count it or ask them to fix it */ }while(selection == 1 || selection == 2); which will stop looping when the number becomes invalid

Comments

-2

If you enter 1 or 2, it won't even enter the loop as neither 1 or 2 are either bigger than 2 or smaller than 1, they are equal.

This should fix it:

while ( selection >= 2 ||  selection <= 1) //equals while(true)

Note that you should also use an else if statement at the second if, it doesnt work correctly the other way.

else if (selection == 2)
{
    cout << " 2 " << endl;
    break; // this leaves the loop
}

8 Comments

What exactly is else if why cant i just put it as if for all and the default else?
else if is used if you want to have several conditions in 1 statement. if you use 2 ifs only the second one gets linked to the else, the first one stands independant. you can just test it and see it. if you leave the 2 ifs as in your code, you will see that it will enter the first if and the else if you enter "1".
AFAICS, that loop (while (selection >= 2 || selection <= 1) continues indefinitely given that selection is an integer; if the values is 2 or larger, or 1 or smaller, that covers every value of integer, doesn't it?
Oh so anytime i have more than one i should just use if else basically right?
@JonathanLeffler yeah, i thought that is what he wanted. otherwise break would do it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.