Thread: Why is this if statement failing?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16

    Why is this if statement failing?

    It seems this code keeps somewhat falling through. Once it gets to the if statement, the if statement no matter what you enter always returns a true value, and follows through. My aim is a simple is this correct question with the user entering Y or N (y or n is also acceptable) but no matter what I type in the if condition seems to return true? If someone could give me some pointers, I would be very grateful!

    Code:
    int starting_menu()
    {
        int Menu_Selection;
       string Menu_Confirm;
       cout << "Welcome to " << Program_Header << " Version: " << Program_Version << endl;
       cout << " " << endl;
       cout << "Please select from the following options to continue." << endl;
       cout << " " << endl;
       cout << "1. (Enter Selection one here)" << endl;
       cout << "2. (Enter Selection two here)" << endl;
       cout << "3. (Enter Selection three here)" << endl;
       cout << "Selection: ";
       cin >> Menu_Selection;
       cin.ignore();
       cout << "You selected: " << Menu_Selection << endl;
       cout << "Is this correct? (Y/N): " << endl;
       cin >> Menu_Confirm;
       cin.ignore();
    
       if ( Menu_Confirm == "Y" || "y" )
         {
          switch ( Menu_Selection )
            {
              case 1:
              cout << "Case 1 Goes Here." << endl;
              break;
    
              case 2:
              cout << "Case 2 Goes Here." << endl;
              break;
    
              case 3:
              cout << "Case 3 Goes Here." << endl;
              break;
            }
         }
       else if ( Menu_Confirm == "N" || "n" )
         {
          cout << "Rebooting the Main Menu." << endl;
          cin.get();
          cout << "Menu System Sucessfully Reloaded." << endl;
          starting_menu();
         }
         else
         {
             cout << "You have not entered a valid input, please try again." << endl;
             cout << "You Selected: " << Menu_Selection << endl;
             cout << "Is this correct? (Y/N): ";
             cin >> Menu_Confirm;
             cin.ignore();
         }
    }
    Happy coding!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    Menu_Confirm == "Y" || "y"
    should be:
    Code:
    Menu_Confirm == "Y" || Menu_Confirm == "y"
    Otherwise, "y" by itself will always evaluate to true, hence the entire expression will always be true.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Tamworth, NSW, Australia
    Posts
    16
    Thank you SO much!!!
    +1 respect :3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateWindowEx() failing with 64-bit
    By domokato in forum Windows Programming
    Replies: 7
    Last Post: 07-02-2009, 07:30 PM
  2. ListView_InsertItem is failing!
    By Joelito in forum Windows Programming
    Replies: 5
    Last Post: 08-22-2006, 07:09 PM
  3. Failing to initialize COM
    By andyhunter in forum Windows Programming
    Replies: 8
    Last Post: 12-22-2004, 03:43 PM
  4. failing hardware
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 10:14 AM
  5. execl failing
    By carrythe0 in forum C Programming
    Replies: 1
    Last Post: 10-01-2001, 12:25 PM