Switch Statements

This is a discussion on Switch Statements within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm a beginner and lost on my assignment already--stuck on the switch statements. I've looked at the tutorials already ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    14

    Switch Statements

    Hi,

    I'm a beginner and lost on my assignment already--stuck on the switch statements. I've looked at the tutorials already but I still can't find my answer so I'm here. My assignment is a quiz so I can't really ask specific questions. I'm just confused about some parts. I know that in the if/else statement, you can have eqns like y=x+3 and then enter in an x value in the if statement to calculate what y is. I've been trying to do the same for the switch statement but I can't.

    Also, I want to add all the cases together, like case 1 = case 1+case2+case3, then case 2 = case 2 +case3

    with the cases being numbers of course. any help is appreciated, thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    I've looked at the tutorials already
    As in Lesson 5: switch case?

    I know that in the if/else statement, you can have eqns like y=x+3 and then enter in an x value in the if statement to calculate what y is. I've been trying to do the same for the switch statement but I can't.
    You might want to give an example of what you mean in terms of if/else.

    Also, I want to add all the cases together, like case 1 = case 1+case2+case3, then case 2 = case 2 +case3
    What do you mean by "add"? One feature (or 'misfeature', depending on your point of view) is that of 'fall-through', as in once a case is reached, control moves to the following case in the absence of say, a break.
    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 2006
    Posts
    835
    I'm not clear on your first question. If I understand the second one, you can do
    Code:
    switch (n) {
    case 1:
    case 2:
    case 3:
      // do something
      break;
    case 4:
    case 5:
      // do something else
      break;
    }
    You're just using switch's fallthrough.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    14
    Yes, tutorial 5.

    Example:

    if(x=5)
    y=x+3;
    printf("y = %f\n",y);

    I don't know how to insert equations into the switch statement. Is that even allowed in the switch statement? To calculate an eqn?

    I tried the the fall through one (I did it without break; ), but it didn't add the numbers together. All it did was list them together.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I think you want "x == 5" (one equals is assignment).

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    14
    oh yes. bad mistake.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    I presume you actually mean:
    Code:
    if (x == 5)
    {
        y = x + 3;
        printf("y = %f\n", y);
    }
    In a switch, this might be:
    Code:
    switch (x)
    {
    case 5:
        y = x + 3;
        printf("y = %f\n", y);
        break;
    }
    Or, if you are declaring a variable within a case:
    Code:
    switch (x)
    {
    case 5:
        {
            double y = x + 3;
            printf("y = %f\n", y);
        }
        break;
    }
    I note that in C++, we usually use output streams like std::cout instead of C-style output functions like printf().

    I tried the the fall through one (I did it without break; ), but it didn't add the numbers together. All it did was list them together.
    Add what numbers together?
    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

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    14
    I've only seen printf(), never std::cout, have no idea why.

    Add the results from each case. Like if I calculate:

    y (case 1) to be = 5
    y (case 2) = 4
    y (case 3) = 3

    Then when I do the switch statement and call on case 1, without using break, it would give me y = 12. But instead, all it did was give me something like:

    y = 5
    y = 4
    y = 3

    rather than y = 12.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Perhaps you are looking for something like this:
    Code:
    int y = 0;
    switch (x)
    {
    case 1: y += 5;
    case 2: y += 4;
    case 3: y += 3;
    }
    But honestly, if you are not cautious and prudent in your use of fall through it can get more confusing than it is worth.
    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

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > I've only seen printf(), never std::cout, have no idea why.
    Well, this is the C++ forum, and std::cout is the C++ counterpart to printf() (you can still use printf() but it's discouraged).
    I think what you're trying to do is to add something to y in each case. In that case, you would want something like "y += 5;" in case 1, "y += 4;" in case 2, etc. You also need to initialize y to 0 before the switch statement.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    14
    Sorry but I have another question. If I use y in the switch statement, then I'd identify it in the beginning as:

    double y;

    right?

    Because for some reason, all my ys are zeros now.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    Because for some reason, all my ys are zeros now.
    What is your current code?
    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

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    14
    Nvm, got it. Thanks for the help.
    Last edited by lazyturtle; 05-02-2007 at 03:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  3. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM
  4. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM

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