Thread: C++ Made Easy: Learning to Program in C++ HELP!!!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Post C++ Made Easy: Learning to Program in C++ HELP!!!

    Hey Guys just joined i have been using this site on and off for a few weeks now and am slowly workign through the c++ section but have come across a problem maybe i am reading it wrong or something but have been stuck for a few days.

    Lesson 5: Switch case -> Test yourself -> question 4.

    Code:
    int x=0;
    switch(x)
    {
      case 1: cout<<"One";
      case 0: cout<<"Zero";
      case 2: cout<<"Hello World";
    }
    A. One
    B. Zero
    C. Hello World
    D. ZeroHello World
    No matter how many times i re-read this section i keep getting B as the answer as int x =0 and so i read that as case 0 now i know this is a very basic question but help would be great THANKS.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall fall through for a switch.
    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 2010
    Posts
    31

    Lightbulb

    You would think it to be answer B, but in fact this switch() statement is missing something: breaks. Without a "break" keyword at the end of each "case" in a switch statement, the compiler will read top-to-bottom from the start point. As a rule, always include break; at the end of each case.

    I make the mistake sometimes of leaving out break when coding, especially if I get distracted before I reach the end of defining a case. You might consider typing break as soon as you start defining a case, so that you can't forget. Developing good habits like this will save you hours upon hours of frustration in the long run.

    Code:
    switch (x) {
    
    case 0: //Your code. Comes later! break;
    }
    Within switch() statements, you should consider a case marker as being like an <a> anchor in HTML. It only tells the program where to jump to, it does not tell the program to stop when it finds another anchor. In your example, the program jumps to case 0, and then reads until it finds a break; or the switch() statement ends.
    Last edited by frog; 09-28-2010 at 01:23 PM. Reason: additional info

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Yeah, somewhat confusing.

    If you don't specify a "break" after some case block then program flow continues linearly, hence it "falls through" to the subsequent cases.

    Perhaps the C language designers should have made breaking the default behavior and required falling through to be explicit.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    so i did read the code right x=0 so it goes case 0 then continued on and did the next case as well what im now getting confused with is the switch(x) part what does this actually mean?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Y0ung Talent
    what im now getting confused with is the switch(x) part what does this actually mean?
    Ah. That means that you simply skipped or forgot something along the way, e.g., this tutorial on switch.
    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

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    ok thanks i understand it now thanks for all the help.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Perhaps the C language designers should have made breaking the default behavior and required falling through to be explicit.
    Have you ever seen "Duff's device"?
    Duff's device from FOLDOC
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    Have you ever seen "Duff's device"?
    Duff's device from FOLDOC
    NICE!!!
    Talk about abusing the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy Bash program project for learning
    By theKbStockpiler in forum Linux Programming
    Replies: 0
    Last Post: 09-15-2010, 09:05 PM
  2. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  3. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  4. made a litte filecounting and byte tallying program ...
    By geekoftheweek in forum C Programming
    Replies: 6
    Last Post: 10-21-2008, 06:16 AM
  5. Will someone try this program I made?
    By SyntaxBubble in forum Windows Programming
    Replies: 6
    Last Post: 11-04-2001, 03:05 PM