Newbie While Loop problem

I'm going through a C++ book right now and it has exercises at the end each chapter. Not sure where I went wrong. Even if I enter the the correct choices, and prints what's in the while loop twice.

Exercise:Write a menu program that lets the user select from a list of options, and if the input is not one
of the options, reprint the list
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string option;
	
	cout << " Please enter your choice from the menu: " << endl;
	cout << ">First choice >Second choice >Third choice" << endl;
	cin >> option;
	
	while(option != "First choice" || option != "Second choice" || option != "Third Choice")
	{
		cout << "That choice is not on the list. Choose one from the list" << endl;
		cout << ">First choice >Second choice >Third choice" << endl;
		cin >> option;
	}
	
	if(option == "First choice")
	{
		cout << "First choice is a great choice." << endl;
	}
	else if(option == "Second choice")
	{
		cout << "Second choice is a great choice." << endl;
	}
	else if(option == "Third Choice")
	{
		cout << "Third choice is a great choice." <<endl;
	}
}
Last edited on
There are two separate problems here. First, the || operators here:
 
    while (option != "First choice" || option != "Second choice" || option != "Third Choice")
should actually be &&, like this:
 
    while (option != "First choice" && option != "Second choice" && option != "Third Choice")

The second problem, is that you ask the user to enter not just a single letter or number, but a whole phrase, such as "Second choice", however when you do this:
1
2
    string option;
    cin >> option;

the user types in "Second choice" but the variable option will contain just the first word, "Second"

If you want to get the whole of the input as a string, you need to use getline, like this:
 
    getline(cin, option);

However, it's tedious for the user to have to enter such a lot of text, where they might mistype something, for example by adding an extra space or something seemingly insignificant. Usually its better to keep the user input short and to the point, such as a single character. char option;
Last edited on
cool I understand the getline(cin, option); part, but wouldn't using && make it to where it has to be all of the options, and not just one of them ?
Last edited on
No, since you're using the ! operator.
If you read it out loud,
while (option != "First choice" && option != "Second choice" && option != "Third Choice")
means "while the option isn't 'First choice' and the option isn't 'Second choice' and the option isn't 'Third choice'".
It's the same as saying "while the option isn't any of 'First choice', 'Second choice', or 'Third choice'".
(You can also write it like that: while (!(option == "First choice" || option == "Second choice" || option == "Third Choice")))

If you switch it up to || instead, then you get "while the option isn't 'First choice' or the option isn't 'Second choice' or the option isn't 'Third choice'", which makes no sense, because regardless of what the option actually is, it's always going to be "not" one of those choices, so the condition will always be true.

Somehow I feel like none of that makes any sense whatsoever.
Heh, ok I understand you now thank you guy's so much.
Topic archived. No new replies allowed.