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

This is a discussion on C++ Made Easy: Learning to Program in C++ HELP!!! within the C++ Programming forums, part of the General Programming Boards category; Hey Guys just joined i have been using this site on and off for a few weeks now and am ...

  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
    20,981
    Recall fall through for a switch.
    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 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
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    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
    20,981
    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.
    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

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 02:05 PM

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