Thread: Jumping in C++ Chapter 5 Question 2

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    Jumping in C++ Chapter 5 Question 2

    Hello again. Thanks for the help on Question 1 in Jumping in C++ but now my program won’t work for question 2. This was my program.
    Code:
    
    
    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main()
    
    
    {
        string program;
        
    
    while(true)
        {
    cout<<"Choose a following program:\n";
    cout<<"Minecraft.\nSkype.\nSkyrim.\n";
            cin>>program;
            
    
    if(program=="Minecraft"||"Skype"||"Skyrim")
            {
                cout<<"You chose: "<<program<<".\n";
                break;
            }
            
    
            else
            {
                continue;
            }
        }
    }
    
    
    



    But whenever I don’t type in a opinion from the menu it doesn’t reprint the menu, it says "You chose:” and then whatever the user input was.


    Can you please tell me why it won;t work and how to fix it?


    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Line 22 does not compare program with the three stings "Minecraft", "Skype", and "Skyrim".

    The expression ``program=="Minecraft"||"Skype"||"Skyrim"'' is actually equivalent to (additional brackets and text added for clarity) to ``(program=="Minecraft") || ("Skype" != NULL || ("Skyrim" != NULL)''

    String literals like "Skype" are never equal to the NULL pointer, so your condition is always true.

    If you want to test if program is equal to one of "Minecraft", "Skype", or "Skyrim", the test you need to do is ``(program == "Minecraft") || (program == "Skype") || (program == "Skyrim")'' (again, I've added brackets for clarity onlu- they're not technically required unless explaining problems like this to newbies).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    Thank you so much!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++, Chapter 5 Question 1.
    By etricity in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2014, 06:55 AM
  2. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM
  3. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  4. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  5. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM

Tags for this Thread