Thread: Help with 'switch statement' please.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Help with 'switch statement' please.

    Hi, I'm writing a program, using switch statements only, and the problem is below:

    Write a program, digitsToWords.c, that prompts the user to input a character in the range '0' to'9' (i.e. a digit character). If the user enters '0' the program should print out "ZERO"; if the input is '1' the ouput should be "ONE"; and so on. Any character outside the range '0' to '9' should cause the program to output "NOT A DIGIT". Use a switch statement

    i've done most of the program, just need advice on the last part because I can't see what I've done wrong. Any time I enter a non digit, I just get 'zero' printed to the screen and not 'NOT A DIGIT', which I want. Here's what I've written so far:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int a;
        
        printf("Please enter a number between 0-9: ");
        scanf("%i", &a);
        
        switch (a) {
            
        case 1 : printf("one\n");
                break;
        case 2 : printf("two\n");
                break;
        case 3 : printf("three\n");
                break;    
        case 4 : printf("four\n");
                break;    
        case 5 : printf("five\n");
                break;
        case 6 : printf("six\n");
                break;    
        case 7 : printf("seven\n");
                break;
        case 8 : printf("eight\n");
                break;    
        case 9 : printf("nine\n");
                break;
        case 0 : printf("zero\n");
                break;
        default: printf("NOT A DIGIT!\n");
                
        }
            return 0;
    }
    Any help would be great thanks.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    What problem are you having? - the only issues I see:
    you do not loop around the switch statement so that the user can enter multiple items (seems to me it is implied from the requirements),
    you are supposed to print uppercase ONE ... ZERO.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Thanks for the reply,

    The problem I'm having is if I enter a letter or symbol I will get the message 'zero'. I want it to print 'NOT A DIGIT' and I can't see why it's not doing that.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should check return value of scanf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    ok, so it will treat all characters as integers, so how would I get the program to return 'This is not a digit' when I enter some other than 0-9?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    vart told you, check the return value of scanf. Here, read the scanf documentation for more info. What happens if the user enters "abc" instead of a number?

    Also, consider that a is an uninitialized automatic variable. Thus, it's contents are undefined at program startup, and it could have any​ value.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Andrew Webb View Post
    ok, so it will treat all characters as integers, so how would I get the program to return 'This is not a digit' when I enter some other than 0-9?
    It's not that it "treats characters as integers". It's that you've put the result into an integer. That can only always be a number.
    So trying to check if it doesn't contain a number is like trying to check if an atom doesn't contain a proton.

    Having said that, if you initialise your variable to say -1, then when the scanf fails it should be left alone, and then your switch case should hit the default. The ideal way though is to check return value of scanf.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  2. using a switch statement
    By dukebdx12 in forum C++ Programming
    Replies: 11
    Last Post: 03-26-2008, 01:58 PM
  3. Switch statement help
    By GeorgeV in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2007, 03:35 AM
  4. Using Switch statement
    By Ubha in forum C Programming
    Replies: 8
    Last Post: 12-24-2006, 12:34 AM
  5. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM