Switch statement problems

This is a discussion on Switch statement problems within the C++ Programming forums, part of the General Programming Boards category; I am writing a program which incorprates a switch statement. I am having problem withs with the menu function. say ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Switch statement problems

    I am writing a program which incorprates a switch statement. I am having problem withs with the menu function.

    say you are prompting the user to input some value. Let's call this this case one. Inside the menu function on the program how do you tell the computer to go get case one.

    For instance I am trying.

    printf("Input leg1:",case 1);

    This isnt working at all. Any ideas.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,794
    You read the value into a variable, then use the switch on the variable.
    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
    Feb 2008
    Posts
    93
    Code:
    float leg1 = -1.0, leg2 = -1.0, hypotenuse = -1.0, area = 0;
     
    
      case '1' :
     44                         {
     45                                 printf("Enter leg1: ");
     46                                 scanf("%f", &leg1);
     47                                 while(leg1 <= 0)
     48                                 {
     49                                         printf("Please enter a positive number: ");
     50                                         scanf("%f", &leg1);
     51                                 }
     52                         }
     53                         break;
    
    
    Menu Printing Function;
    219 void printMenu()
    220 
    221 {
    222 
    223 
    224   printf("Input value for leg1:",case '1');
    225   printf("Input value for leg2:");
    226   printf("Input value for hypotenuse:");
    227   printf("Calculate leg1:");
    228   printf("Calculate leg2:");
    229   printf("Calculate hypotenuse:");
    230   printf("Calculate area of right triangle:");
    214 }
    215
    What do you mean by that?

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    In C please? I don't understand what you said.

  5. #5
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    Code:
    int input;
    printf("Enter your choice (0 - exit, 1 - do something usefull):");
    flush(stdout);
    if( scanf("&#37;d",&input) == 1)
    {
       switch(input)
       {
          case 0: return 0;
          case 1:
             /* do something useful */
             break;
          default:
             /* wrong input - do something stupid */
       }
    }
    else
    {
       /* wrong input - clear the stdin and try again */
    }
    To be or not to be == true

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    flush(stdout);
    fflush(), of course.

    BTW -- "usefull" has one 'L' . . . .

    Code:
    /* wrong input - clear the stdin and try again */
    Here's one way to do that:
    Code:
    while(getchar() != '\n');
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. Switch statement problems
    By ComDriver in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2005, 10:31 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

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