Basic Data types continue

This is a discussion on Basic Data types continue within the C Programming forums, part of the General Programming Boards category; Hey, how are you guys? I just want to tell you that you are a big help!!!! This is how ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Question Basic Data types continue

    Hey, how are you guys?
    I just want to tell you that you are a big help!!!!
    This is how I finished the program but I don't know if I am missing anything else
    Thank for your help

    #include <stdio.h>
    int main()
    {

    int alpha2number(char data[])
    char value[ strlen(data) ];

    int x = 0;

    printf("Please enter a phone number: \n");
    scanf("%d", &x);
    while(data[x] != '\0') {

    switch(data[x]) {

    case 'a': case 'b': case 'c':
    value[x] = 2;
    case 'd': case 'e': case 'f'
    value[x] = 3;
    case 'g': case 'h' : case 'i'
    value[x] = 4;
    case 'j': case 'k': case 'l'
    value[x] = 5;
    case 'm': case'n': case 'o'
    value[x] = 6;
    case 'p' : case 'q': case 'r': case 's'
    value[x] = 7;
    case 't': case 'u': case 'v'
    value[x] = 8;
    case 'w': case'x' : case 'y' : case 'z'
    value[x] = 9;
    }
    ++x;
    }

    return 0;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Please read this thread and edit your post accordingly.

    Code:
    case 'a': case 'b': case 'c':
    value[x] = 2;
    break;
    case 'd': case 'e': case 'f'
    value[x] = 3;
    break;
    case 'g': case 'h' : case 'i'
    value[x] = 4;
    break;
    case 'j': case 'k': case 'l'
    value[x] = 5;
    break;
    case 'm': case'n': case 'o'
    value[x] = 6;
    break;
    case 'p' : case 'q': case 'r': case 's'
    value[x] = 7;
    break;
    case 't': case 'u': case 'v'
    value[x] = 8;
    break;
    case 'w': case'x' : case 'y' : case 'z'
    value[x] = 9;
    break;
    You're going to want to put break statements in there otherwise every index in value will be 9.

    There are many other errors in your code. Have you actually tested it yet?
    Last edited by XSquared; 03-09-2003 at 07:54 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Question Basic data types fix

    Here, I think I fix it

    #include <stdio.h>
    int main()
    {

    int alpha2number(char data[]);

    char value[ strlen(data) ];

    int x = 0;

    printf("Please enter a phone number: \n");
    scanf("%d", &x);

    while(data[x] != '\0') {

    switch(data[x]) {

    case 'a': case 'b': case 'c':
    value[x] = 2;

    case 'd': case 'e': case 'f'
    value[x] = 3;

    case 'g': case 'h' : case 'i'
    value[x] = 4;

    case 'j': case 'k': case 'l'
    value[x] = 5;

    case 'm': case'n': case 'o'
    value[x] = 6;

    case 'p' : case 'q': case 'r': case 's'
    value[x] = 7;

    case 't': case 'u': case 'v'
    value[x] = 8;

    case 'w': case'x' : case 'y' : case 'z'
    value[x] = 9;
    }
    ++x;
    }

    return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Change

    I change it, but I have tried it because I don't have a compiler in my computer.

    #include <stdio.h>
    int main()
    {

    int alpha2number(char data[])
    char value[ strlen(data) ];

    int x = 0;

    printf("Please enter a phone number: \n");
    scanf("%d", &x);
    while(data[x] != '\0') {

    switch(data[x]) {

    case 'a': case 'b': case 'c':
    value[x] = 2;
    break;

    case 'd': case 'e': case 'f'
    value[x] = 3;
    break;

    case 'g': case 'h' : case 'i'
    value[x] = 4;
    break;

    case 'j': case 'k': case 'l'
    value[x] = 5;
    break;

    case 'm': case'n': case 'o'
    value[x] = 6;
    break;

    case 'p' : case 'q': case 'r': case 's'
    value[x] = 7;
    break;

    case 't': case 'u': case 'v'
    value[x] = 8;
    break;

    case 'w': case'x' : case 'y' : case 'z'
    value[x] = 9;
    break;
    }
    ++x;
    }

    return 0;
    }
    Last edited by viryonia; 03-09-2003 at 08:06 PM.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Please stop posting multiple threads on the exact same topic. Just continue with one thread, or you run the risk of having all of the threads deleted.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Threads mereged by me...

    viryonia - if you wish to continue a thread, click reply...dont start a new one please

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >I change it, but I have tried it because I don't have a compiler in my computer.
    Whenever possible, it's best to copy-and-paste the actual code to your post; retyping can introduce its own errors.
    Code:
    #include <stdio.h>
    int main()
    {
       int alpha2number(char data[]) ; /* missing semicolon */
       /* But why (attempt to) prototype a function you don't define or use? */
    
       char value[ strlen(data) ]; /* I'd avoid this and stick with C90 */
       /* You don't define 'data', so this is an error (even in C++^H^H99). */
    
       int x = 0;
    
       printf("Please enter a phone number: \n");
       fflush(stdout); /* my suggestion */
       scanf("%d", &x); /* Check the return value for success? */
       while(data[x] != '\0') {
          switch(data[x]) {
          case 'a': case 'b': case 'c': /* Got the colon here... */
             value[x] = '2'; /* Aren't you making a string? */
             break;
          case 'd': case 'e': case 'f': /* ...but here the last colon is missing. */
             value[x] = '3'; /* Put characters in a string. */
             break;
          /*...*/
    It would appear that you are taking an input string that would be in the format "ABC-DEFG" and want to transform it into its equivalent phone number string, "222-3334". Most of a solution has been posted already. Let's just say we use a function; I might do something like this.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    char *foo(char *dst, const char *src)
    {
        char *start;
        for ( start = dst; *src != '\0'; ++dst, ++src )
        {
            switch ( tolower(*src) )
            {
            case 'a': case 'b': case 'c':           *dst = '2';  break;
            case 'd': case 'e': case 'f':           *dst = '3';  break;
            case 'g': case 'h': case 'i':           *dst = '4';  break;
            case 'j': case 'k': case 'l':           *dst = '5';  break;
            case 'm': case 'n': case 'o':           *dst = '6';  break;
            case 'p': case 'q': case 'r': case 's': *dst = '7';  break;
            case 't': case 'u': case 'v':           *dst = '8';  break;
            case 'w': case 'x': case 'y': case 'z': *dst = '9';  break;
            default: *dst = *src; /* copy non-alphabetic characters as-is */
            }
        }
        *dst = '\0';
        return start;
    }
    
    int main(void)
    {
        const char number[] = "Get-Help";
        char result[32];
    
        foo(result, number);
        printf("number = \"%s\", result = \"%s\"\n", number, result);
    
        puts(foo(result, "(800) EAT-SPIT"));
    
        return 0;
    }
    
    /* my output
    number = "Get-Help", result = "438-4357"
    (800) 328-7748
    */
    Please read up on [code] [/code] tags to make future postings more readable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  3. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05:03 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21