Thread: Newbie question, trouble entering value into switch from scanf function

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Newbie question, trouble entering value into switch from scanf function

    I'm having trouble inputing a value into the switch control from scanf. I'm completely lost as to what the problem maybe.

    Code:
    #include <stdio.h>
    
    int code = 0, hours = 0;
    float salary = 0, wage = 0, base = 0;
    
    
    int main()
       {
    
       while ( code != 6 )
          {
    
          printf("Enter number 1-6(6 to exit): ");
          scanf("%d", &code);
    /*---------Test code below------*/
          printf("%d", code);
    
          switch (code)
             {
    
             case '1':
                printf("Enter weekly salary: ");
                scanf("%lf", &salary);
                printf("Well there you, you make %.2f a week\n", salary);
                break;
    /*
             case '2':
                printf("Enter hourly wage: ");
                scanf("%lf", &wage);
                printf("Enter hours worked in week: ");
                scanf("%d", &hours);
    
                if (hours > 40)
                   salary = ( (hours - 40) * 1.5 + 40 ) * wage;
                else
                   salary = (float) hours * wage;
    
                printf("You made %.2f this week\n", salary);
                break;
    
             default:
                break;
    */
             }
    
          }
    
    
       return 0;
       }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    case 1: /* ... */
    case 2: /* ... */
    (No single-quotes.)

    But avoid scanf, visit the FAQ, don't drink and code^H^H^H^Hdrive.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thank you!

    Hmmm, I wonder why it somewhat worked with getchar but not with scanf.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by yougene
    Hmmm, I wonder why it somewhat worked with getchar but not with scanf.
    Because there is a difference between character and integer input. If you ask for a character, scanf will (try to) give you the character. If you ask for an integer scanf will (try to) give you the integer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    crystal clear

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    12
    if you dont want to change int code = 0 to char code = '0' or case '1' to case 1: write:
    case '1'-'0': ...
    case '2'-'0': ...
    ...
    its funny

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    vangmor, the next time you give advice, make it correct advice. A switch statement might compile like that, but it won't work. I'm surprised it doesn't warn you that the case value isn't constant.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by citizen
    vangmor, the next time you give advice, make it correct advice. A switch statement might compile like that, but it won't work. I'm surprised it doesn't warn you that the case value isn't constant.
    Probably because they are constants (or a very likely to be).

    [edit]http://groups.google.com/group/comp....bb9e5babcb6829

    Not that I see any advantage in writing case '1'-'0': vs case 1:.
    Last edited by Dave_Sinkula; 05-25-2006 at 01:31 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Newbie function trouble
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2008, 04:33 AM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Having trouble with scanf function
    By cw3od in forum C Programming
    Replies: 2
    Last Post: 11-28-2001, 03:58 PM