Results 1 to 9 of 9

Thread: How do I create Case statements in C++?

  1. #1
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Arrow How do I create Case statements in C++?

    I want to create a function that does something similar to this:

    C++ Code:
    int WhatToDo(char *Something)
    {
        case &Something of
        {
            1:
        }
         
    }

    Basically, if user types word, return a value that represents word the person types in.

    I think I might be leaning more towards pascal as I wrote that, but I have no idea how to do it in c++. I looked online and there was not a clear solution on how to accomplish the task. Does anyone know how to achieve something similar to that but in c++?

  2. #2
    Join Date
    Nov 2007
    Location
    46696E6C616E64
    Posts
    3,069
    Mentioned
    44 Post(s)
    Quoted
    302 Post(s)

    Default

    Code:
    switch(Something)
    {
      1: __asm { nop }
    }
    There used to be something meaningful here.

  3. #3
    Join Date
    Mar 2013
    Posts
    49
    Mentioned
    0 Post(s)
    Quoted
    17 Post(s)

    Default

    Basic
    Code:
    switch(condition) { // in this case the condition would involve an integer (matches case)
         case 1: 
            // do something
         break;
    
         case 2:
            // something else
         break;
         
         default:
            //what do do if other cases aren't met
    }
    You can also
    Code:
    switch(condition) {
        case 1: case 2: case 3: case 4:
            // do something when one of those cases are true
        break;
    
        default:
            //yeah stuff
    
    }
    Condition can be char, bool, int.
    Probably something else but i'm hungover...

  4. #4
    Join Date
    Mar 2013
    Location
    Argentina
    Posts
    758
    Mentioned
    27 Post(s)
    Quoted
    365 Post(s)

    Default

    Quote Originally Posted by JJordan View Post
    . . . . . .

  5. #5
    Join Date
    Nov 2014
    Posts
    104
    Mentioned
    12 Post(s)
    Quoted
    59 Post(s)

    Default

    In most languages, it is called a switch statement. Here's a pretty good mini tutorial. If you want any more details, don't hesitate to ask. Good luck.

  6. #6
    Join Date
    Nov 2013
    Location
    North of Hell
    Posts
    271
    Mentioned
    7 Post(s)
    Quoted
    171 Post(s)

    Default

    Don't think people realize this thread is 2 years old......................

    Let the threads rest in peace guys

  7. #7
    Join Date
    May 2014
    Posts
    633
    Mentioned
    8 Post(s)
    Quoted
    322 Post(s)

    Default

    In their defenses, most threads on this particular section are about a year or more old and I found JJordan's explanation easier to understand than the one given two years ago.

  8. #8
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    Quote Originally Posted by mynamebilalzahid View Post
    Hi,
    i would recommend you to use switch Structure and make a project on that, like "Write a program that tells week day when a user enters a number like: 1,2,3,4,5,6,7, and default value should be ("Invalid Number of Week Day")" i had also created this program when i was a student and chosen C++ as essential language.

    GOod Luck !
    You realize the OP is 2012, right?




    Skype: obscuritySRL@outlook.com

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •