Thread: Phone Number Translator in C

  1. #16
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    This is what happens when I run it.
    It should be :
    Enter Phone Number: CAlLatT
    Numeric Form: 2255288
    Attached Images Attached Images Phone Number Translator in C-screen-shot-2014-10-20-8-25-17-am-png 

  2. #17
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    the variable 'i' needs to be reset back to 0 before the while loop. Right now the while loop is being skipped over completely because the for loop is incrementing 'i' to the end of the string.

  3. #18
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    so should I add curly braces before the while loop and after the i++?

  4. #19
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Try and see what happens

  5. #20
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    Nothing Changes

  6. #21
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    Does it have to do with the braces? Or its totally something else?

  7. #22
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    post your current code

  8. #23
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    Code:
    #include <stdio.h>
    int main()
    {
        char N[20];
        int i=0;
        printf("Enter phone number: ");
        scanf("%s" ,N);
        printf("Numeric Form:");
        
        for(i=0; i<=strlen(N); i++){
                if(N[i]>=97&&N[i]<=122)
                N[i]=N[i]-32;
      } 
        {
        while (N[i] != '\0')
            switch (N[i])
            {
                case '.' :
                case ',' :
                case '@' :
                    printf("1");
                    break;
                case 'A' :
                case 'B' :
                case 'C' :
                    printf("2");
                    break;
                case 'D' :
                case 'E' :
                case 'F' :
                    printf("3");
                    break;
                case 'G' :
                case 'H' :
                case 'I' :
                    printf("N4");
                    break;
                case 'J' :
                case 'K' :
                case 'L' :
                    printf("5");
                    break;
                case 'M' : 
                case 'N' :
                case 'O' :
                    printf("6");
                    break;
                case 'P' :
                case 'Q' :
                case 'R' :
                case 'S' :
                    printf("7");
                    break;
                case 'T' :
                case 'U' :
                case 'V' :
                    printf("8");
                    break;
                case 'W' :
                case 'X' :
                case 'Y' :
                case 'Z' :
                    printf("9");
                    break;
                    
                    printf("%c", N[i]);
            }
            
            i++;
        }
    return(0);
    }

  9. #24
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Refer back to Post#17

    also change your curly braces to be after the while loop condition instead of behind it.

  10. #25
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    So i have a mistake somewhere with the braces?

  11. #26
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    {
        while (N[i] != '\0')
    should be

    Code:
        while (N[i] != '\0'){

  12. #27
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    I tired, nothing changes

  13. #28
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    did you reset i back to 0 before the while loop like post #17 suggested??

  14. #29
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    well i changed the braces, which would make the i go back to 0 and then to the while loop (like post 26) but it didn't work.

  15. #30
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    show me how changing the braces will make the i go back to 0. It won't. The 'i' is coming out of the for loop and it's value is whatever the strnlen(N) is. therefore it is not 0. you need to manually set it to 0 before the while loop and after the for loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's my phone number going?
    By Dino in forum C Programming
    Replies: 1
    Last Post: 01-16-2010, 08:33 PM
  2. phone number code
    By willmhmd in forum C Programming
    Replies: 5
    Last Post: 10-19-2005, 07:33 PM
  3. strtok() a phone number
    By Sure in forum C Programming
    Replies: 3
    Last Post: 06-27-2005, 06:46 PM
  4. Phone number
    By beachchutney in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 04:18 PM