• C

convert char to numbers

The problem is to get an the inout and output file from the commnad line. In the input file there's phone numbers like
1 800 flowers
1 800 mattress
etc
convert the text part to numbers and ouput to a seperate both the original and numerical phone number this is what I have so far
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char*argv[])
{
 int ch;
FILE *fp;
int count=0;
fp=fopen(argv[1],"r");
while ((ch=getchar(fp))!= EOF)

switch(ch){
case 'A':
case 'B':
case 'C':
      ch=getchar(2);
      break;
case'D':
case'E':
case'F':
      ch=3;
      break;
case'G':
case 'H':
case 'I':
      ch=4;
      break;
case'J':
case'L':
case'K':
      ch=5;
      break;
case'M':
case'N':
case'O':
      ch=6;
      break;
case 'P':
case'Q':
case'R':
case 'S':
      ch=7;
      break;
case'T':
case 'U':
case 'V':
      ch=8;
      break;
case 'W':
case 'X':
case 'Y':
case 'Z':
      ch=9;
      break;



putchar(ch,stdout);
count++;
}
return(0);
}
can anyone help me further I'm stuck Trying to relearn this to go back to school in Jan
Serena2345Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rishiskCommented:
There are couple of things that you should be doing.
Instead of assigning integer (2 to 9) to the variable ch, you should be assigning the ASCII value to ch like

case 'A':
case 'B':
case 'C':
     ch='2';
     break;

The next thing is that, putchar takes only one parameter namely the char variable. So change that to

putchar (ch);

Also, you should be closing the file before the return (0)

fclose(fp);

Hope it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:

Hi Serena2345,

The first thing that you need to do is put a couple of sanity checks into the program.  If argc != 2 you probably have a parameter error and should display a message and exit.  If argc < 2 no file name was given so there's nothing to open.  And if you can't open the file you also need to let the user know.

As far as the actual problem goes, you're on the right track.  But you've made no provision for printing the original phone number and the modified number.  (You're converting the number without saving the one that you read.)  Here's the steps that will get you through this.

1)  Declare two character arrays of about 200 characters in length.  One is for the line that you're going to read in from the file, the other is for the line that you're going to build by substituting the appropriate character.
2)  Using fgets(), read a line from the file into the first array.  (When you hit EOF, exit the read loop.)
3)  Use strcpy() to copy the string to the new array.
4)  Loop through the new array and change each of the letters to the appropriate digit.  Remeber that you're replacing characters, not numbers.  You probably need to change upper case letters AND lower case letters!
5)  Print both strings.  Printing them on separate lines is probably better since the characters line up and it's easy to see what changes you made.
6)  Loop back and read another phone number from the file.

Note that copying the string before you convert the characters copies all of the punctuation, too.  This way the phone numbers that include dashes or parentheses will still have them.


Good Luck!
Kent

0
g0rathCommented:
your case statement is only for uppercase letters, which isn't always the case.

so you should use the toupper()

#include <ctype.h>

int toupper( int c );

you coulde use it like this...


switch(toupper(ch)) {
...
}

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

grg99Commented:
BTW there is no "Q" on the phone dials around here.....

0
g0rathCommented:
hehe, there is on my office phone....but true, not on all phones.
0
ExceterCommented:
It depends on the phone. See this link,
http://www.dialabc.com/motion/keypads.html

Therefore, you have to decide which keypad you will write this for. I'd be tempted to use the the ITU Standard E.161 keypad to write my app but it depends on what you are trying to do. In your case, for a learning excersize it probably doesn't make a big difference.

Cheers!
Exceter
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.