While loop with OR conditions, beginner

This is a discussion on While loop with OR conditions, beginner within the C++ Programming forums, part of the General Programming Boards category; I am just beginning and going through Jumping Into C++ . I am working #2 in chapter 5 which states... ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Near Houston
    Posts
    2

    While loop with OR conditions, beginner

    I am just beginning and going through Jumping Into C++. I am working #2 in chapter 5 which states... "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"
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        string input = "unknown";
        while ( input != "1" || input != "2" || input != "3" || input != "4" || input != "5")
        {
            cout << "file (1)\t edit (2)\t view (3)\t search (4)\t project (5)\n";
            cout << "Select a function: ";
            cin >> input;
        }
    }
    No matter what I enter the loop never breaks. I am pretty sure my "while" line is not right but I don't know. I know this is elementary stuff but I want to understand the easy stuff before it gets really hairy.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Suppose input == "1". Then input != "1" is false, so we check if input != "2". Well, it is true that input != "2", so we continue looping. Problem is, when input == "1", you want to stop looping. So, under what conditions does (input != "1" || input != "2" || input != "3" || input != "4" || input != "5") ever evaluate to false? Well, when (input == "1" && input == "2" && input == "3" && input == "4" && input == "5"). Unfortunately, input can only hold one of these values at any one time.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Location
    Near Houston
    Posts
    2
    Let me put my logic hat on and ponder a bit.

    Thanks for the reply. I will re-tool a little.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You could simplify it a little:
    Code:
    while( input[0]>'0' && input[0] <'6')
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by manasij7479 View Post
    You could simplify it a little:
    Code:
    while( input[0]>'0' && input[0] <'6')
    That would be wrong though. It should be:
    Code:
    while (input.length() != 1 || input[0] < '1' || input[0] > '5')
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9
    If you want a bit more structure, you can do something like:

    Code:
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    int main() 
    { 
       int choice = 0;
       do
       {
          cout << "file (1)\t edit (2)\t view (3)\t search (4)\t project (5)\t quit (6)\n"; 
          cout << "Select a function: "; 
          cin >> choice; 
          switch( choice )
          {
             case 1: cout << "file" << endl;
             case 2: cout << "edit" << endl;
             case 3: cout << "view" << endl;
             case 4: cout << "search" << endl;
             case 5: cout << "project" << endl;
          }
       } 
       while( choice != 6 ) //loop stops on 6
    }


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. combined conditions in For loop
    By sjmp in forum C Programming
    Replies: 5
    Last Post: 08-24-2012, 04:35 AM
  2. Beginner Problem with Loop
    By ToweyLeake in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2011, 10:12 AM
  3. beginner loop help
    By helloalyssa in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2010, 09:08 PM
  4. Multiple conditions for the while loop?
    By Olidivera in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2005, 03:47 AM
  5. While Loop Conditions?
    By ted_smith in forum C Programming
    Replies: 8
    Last Post: 01-17-2005, 09:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21