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
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
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();
this will actually do the work for you
what you wrote for it has the right idea, but it needs some work. think about what your doing.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. */ }
get a string, parse it with a function, take the resaults back, print it.
o yeah, you know your homework is just going to get harder right?
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).Code:int alpha2number(char data[]) { char value[ strlen(data) ]; /* Store data here. */ /* ... */ }
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!!!!!
> 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.