Thread: Array-Switch-Default

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    29

    Array-Switch-Default

    1) Prompting the user to enter a phone number.
    2) Read the characters, if its alphabets and lower case, change it to upper case. if not, leave it.
    3) Read and store the characters in the first array O[i]
    4) for statement for i
    5) Switch-case loop. Here, I am trying to convert the entered alphabets into phone numbers and save them in a second array. However, if the input character was not an alphabet (none of the cases) i'm setting a default case to keep the same character but save it in the second array.
    6) Print the 2nd array.

    My outcome should be:
    Enter Phone Number: 1-800-COL-LecT
    Numeric Form: 1-800-265-5328

    Code:
    #include <stdio.h> 
    #include <ctype.h>
     
     
    int main()
    {
        #define N 15
    
    
        char O[N];
        char S[N];
        char ch; 
        int i=0;
        
        printf("Enter phone number: ");
    
    
        while ( (ch=toupper(getchar()) ) != '\n' && i<N)
        {
            O[i] = ch;
            i++;
        }
        printf("Numeric Form      : ");
         
        for(i=0;i<N;i++)
        {
            
            switch (O[i])
            {
                
                case '.' :
                case ',' :
                case '@' :               
                    S[i] = '1';
                    break;
                case 'A' :
                case 'B' :
                case 'C' :
                    S[i] = '2';
                    break;
                case 'D' :
                case 'E' :
                case 'F' :
                    S[i] = '3';
                    break;
                case 'G' :
                case 'H' :
                case 'I' :
                    S[i] = '4';
                    break;
                case 'J' :
                case 'K' :
                case 'L' :
                    S[i] = '5';
                    break;
                case 'M' : 
                case 'N' :
                case 'O' :
                    S[i] = '6';
                    break;
                case 'P' :
                case 'Q' :
                case 'R' :
                case 'S' :
                    S[i] = '7';
                    break;
                case 'T' :
                case 'U' :
                case 'V' :
                    S[i] = '8';
                    break;
                case 'W' :
                case 'X' :
                case 'Y' :
                case 'Z' :
                    S[i] = '9';
                    break;
                default:
                  O[i] = S[i];
                     
    
    
                    printf("%c", S[i]);
            }
             
            i++;
           }
     
    return(0);
    }
    Last edited by Gkendoyan; 10-24-2014 at 09:24 AM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    then default should be S[i] = O[i];
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    Ok i fixed that, thank you! But now what's happening is that its only reading the first character. and if the first character is a lower case alphabet, its transfering to upper case printing that. if i put in a number, its printing back the first number.

    Where's the mistake?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You need to look at 2 things carefully..

    1.) look at your printf statement and where it's located.

    2.) you have an extra i++ that is iterating through your loop twice.

    EDIT: Also, your while loop is getting the newline character in the buffer and exiting.

    Code:
    while ( (ch=toupper(getchar()) ) != '\n' && i<N)
    Code:
     while ( (ch=toupper(getchar()) ) != '\n' && i<N)
        {
            getchar(); // This will flush the newline character
            O[i] = ch;
            i++;
        }
    Last edited by camel-man; 10-24-2014 at 10:16 AM.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by camel-man View Post
    then default should be S[i] = O[i];
    Quote Originally Posted by Gkendoyan View Post
    Ok i fixed that, thank you! But now what's happening is that its only reading the first character. and if the first character is a lower case alphabet, its transfering to upper case printing that. if i put in a number, its printing back the first number.

    Where's the mistake?
    The mistake is that you picked horrible variable names. They don't describe anything. Honestly, I would have guessed O is your "output" array, and S is your "string" or "alphabet" array, so I didn't notice that mistake right away. If, instead you had array names like original and converted, not only would it make your code more clear, it would be much easier to spot the mistake you made of reversing the assignment -- in fact, with clean, well formatted code and descriptive names, it's much less likely you'll even even make mistakes in the first place.

    As for the double increment problem, it would be good if you you started learning to use a debugger. A debugger allows you to step through your program line by line and examine all the variables at each moment. IT's one of the most important tools for a programmer. For smaller programs, sometimes you can just add a few printf statements to see what is going on, and remove them when you have the problem(s) fixed. Putting the following at the end of the for loop would have caught the problem:
    Code:
    printf("Converting %c in input, to %c in array position %d\n", O[i], S[i], i);
    Also, you have a potential problem with your input. You don't null terminate it, so it's not a proper C-string. Your code assumes that the user always enters N characters, but it will break if anybody enters less than N (by converting and printing garbage characters at the end of the input array). You have two options:
    1. Store the length in a separate variable, and use that to limit your for loop instead of N.
    2. Null-terminate the input array. This has the advantage of making it easier to work with and print later, as there are a lot of standard functions for working with strings. You can use strlen to get it's length at any time, and you can still use a variable to track the length so you don't have to call strlen every time. Note that you have to save one spot in the array for the null terminator, so your input loop would be
    Code:
    while ( (ch=toupper(getchar()) ) != '\n' && ch != EOF && i<N-1) {
        original[i++] = ch;
    }
    original[i] = '\0';  // add null terminator
    original_length = i;  // store the length in a variable for easy use later
    ...
    // easy print:
    printf("The original number is: %s\n", original);  // no looping through char by char
    A few things to note about the above
    1. Check for EOF as well as '\n'. I may redirect input from a file or pipe it from another program, which may send EOF to signal end of input. You should always handle EOF when dealing with input.
    2. i < N-1 to save space for the null terminator. Define N as 16 if you want room for 15 characters of input + 1 for the null terminator.
    3. You can combine the increment and assignment by putting i++ in the array index.
    4. If you similarly put a null terminator at the end of the converted string, you can use the same techniques and standard functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C: Default case in switch not working
    By LunaLux in forum C Programming
    Replies: 5
    Last Post: 04-24-2011, 08:46 AM
  2. switch () {default: <something>; case ...
    By Kennedy in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 12:40 PM
  3. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  4. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM
  5. Returning from function - switch selects default
    By rkooij in forum C Programming
    Replies: 24
    Last Post: 03-17-2006, 07:27 AM