Thread: Switch Statement Help

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Switch Statement Help

    // Please tell me why doesn't my switch statement work when I plug in characters?

    Code:
    #include <stdio.h>
    
    main() 
    {
           int grade; 
           
           printf("\nEnter your score\n");
           scanf("%d",&grade);
           
           if ( grade >= 70) {
           
           printf("\nyou passed the test");
           }
           
           else {
                printf("\nYou failed the test");
           
           }
            
           printf("\n\nEnter grade\n\n");
           scanf("%d",&grade);
           
           switch (grade)
          
           {
           
           case 'a': printf("\nYou did excllent on the test");
           break;
           
           case 'b': printf("\nYou did good on the test");
           break;
           
           case 'c': printf("\nYou did ok on the test");
           break;
           
           case 'd': printf("\nYou did poorly on the test");
           break;
           
           case 'e': printf("\nYou failed the test");
           break;
          
           default: printf("\nYou need to take the test");
           break;
           
           }
            getch();
           
           }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,347
    Because you are reading as int, not as char.
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, if you want a letter, read a letter, not a number:
    Code:
           printf("\n\nEnter grade\n\n");
           scanf("%d",&grade);
           
           switch (grade)
          
           {
           
           case 'a':
    Other than that, you need to actually explain what is happening and how that differs from what you want to happen.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    When I enter a letter my switch statement goes to my default statement but not the correct one.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Learn to answer questions the nice and SMART WAY GENIUS

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    "What we got here is... failure to communicate."

    edit:
    Quote Originally Posted by IMMORTALX View Post
    Learn to answer questions the nice and SMART WAY GENIUS
    When seeking help, try not to be a piece of crap.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    I know you cant communicate well get those skills

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,347
    You really should not pick a fight with people who have helped you by pointing out what needed to be fixed for your program to work, especially when it directly answers your question.

    *thread closed*
    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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by IMMORTALX View Post
    // Please tell me why doesn't my switch statement work when I plug in characters?
    Code:
    #include <stdio.h>
    
    int main( void ) 
    {      int  score;
           char grade; 
           
           printf("\nEnter your number score\n");
           scanf("%d",&score);
           
           if ( score >= 70)        
             printf("\nyou passed the test");
           else 
            printf("\nYou failed the test");
           
            printf("\n\nEnter letter grade\n\n");
           scanf(" %c",&grade);
           
           switch (grade) {
             case 'a': 
                 printf("\nYou did excllent on the test");
                 break;
             case 'b': 
                 printf("\nYou did good on the test");
                 break;
             case 'c': 
                 printf("\nYou did ok on the test");
                 break;
             case 'd': 
                 printf("\nYou did poorly on the test");
                 break;
             case 'e': 
                 printf("\nYou failed the test");
                 break;
             default: 
                 printf("\nYou need to take the test");
                 break;
        }
        getchar();
        return 0; 
     }
    There... try that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement
    By Programmer_P in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2010, 03:11 AM
  2. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  3. Switch statement
    By BSmith4740 in forum C Programming
    Replies: 29
    Last Post: 02-28-2008, 08:28 PM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM
  5. The Switch Statement
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:44 AM