Basic Data types help

This is a discussion on Basic Data types help within the C Programming forums, part of the General Programming Boards category; Hey, can anyone tell how to writea program that translates an alphabetic phone number into a numeric form I tried ...

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

    Question Basic Data types help

    Hey, can anyone tell how to writea program that translates an alphabetic phone number into a numeric form
    I tried many things and I don't know what to do
    Please help me, I am clueless in this

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

    Continue

    this is what I have so far for the program. I don't know what to do next

    /*This program translates an alphabetic phone phone number into numeric form*/

    #include <stdio.h>
    int main()
    {
    char letter=0;
    int number=0:
    printf("Enter a phone number in letters: \n");
    scanf("%c, &letter);
    fflush (stdin);
    letter = getchar();

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    this will actually do the work for you

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* Call this with the actual data, it will return the phone number. */
    int alpha2number(char data[]) {
        char value[ strlen(data) ]; /* Store data here. */
        
        int x = 0; /* Keep track of where we are. */
        
        while(data[x] != '\0') {
            switch(data[x]) { /* Parse threw the data. */
                case 'a': /* All of these are 2. */
                case 'b':
                case 'c':
                    value[x] = 2;
                /* Keep it going here, im to lazzy. */
            }
            
            ++x; /* Go to next letter*/
        } /* Start all over again untill theres no more data left. */
        
        return atoi(value); /* I cheated and added the phone number to a string, here we take my string and return a number that can be used. Im lazzy, so i didnt do the math. */
    }
    what you wrote for it has the right idea, but it needs some work. think about what your doing.

    get a string, parse it with a function, take the resaults back, print it.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Talking

    o yeah, you know your homework is just going to get harder right?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    int alpha2number(char data[]) {
        char value[ strlen(data) ]; /* Store data here. */
        /* ... */
    }
    Until C99 implementations are more readily available, it might be better to post a C90 solution. Or at least comment that this code is C99 (or C++, but then that would be off-topic on this board).

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Dave_Sinkula, your completly right here, but do you know how many people have told me that before.

    im i the only one that hasnt memorized this stuff!!!!!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    > but do you know how many people have told me that before.
    I imagine everyone who doesn't have or use a C99 implementation on a regular basis yet likes to compile and run code found online for learning or testing purposes.

    viryonia:
    >scanf("%c, &letter);
    Always check the return value of input functions.

    >fflush (stdin);
    Undefined behavior, fflush does not accept input streams.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function that takes diff data types as paramter
    By nepdude in forum C Programming
    Replies: 7
    Last Post: 07-06-2007, 01:47 PM
  2. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 09:43 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 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