need help with atoi

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 4
Reputation: hosh is an unknown quantity at this point 
Solved Threads: 0
hosh hosh is offline Offline
Newbie Poster

need help with atoi

 
0
  #1
Oct 20th, 2009
Hi,
can anyone help me to solve this problem?
I have this code:
  1. FILE *in;
  2. char i =fgetc(in);
  3. int perm=0;
  4. perm =atoi(&i);
  5. printf("%d\n", perm);
if first character in my "in" file is 2, the output is 28;
if ------------------------------- is 3, -------------- 38, and so on...

any ideas what is wrong with this code?
thanks
Last edited by WaltP; Oct 20th, 2009 at 2:48 am. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
1
  #2
Oct 20th, 2009
Originally Posted by hosh View Post
Hi,
can anyone help me to solve this problem?
I have this code:

FILE *in;
char i =fgetc(in);
int perm=0;
perm =atoi(&i);
printf("%d\n", perm);

if first character in my "in" file is 2, the output is 28;
if ------------------------------- is 3, -------------- 38, and so on...

any ideas what is wrong with this code?
thanks
atoi() accepts a string as argument. Not a character i as it is in your code, nor a pointer to a char like you give it as in atoi(&i)
Something like:
  1. char twenty_three[] = "23";
  2. int result;
  3.  
  4. result = atoi(twenty_three);
Last edited by Aia; Oct 20th, 2009 at 1:13 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: hosh is an unknown quantity at this point 
Solved Threads: 0
hosh hosh is offline Offline
Newbie Poster
 
0
  #3
Oct 20th, 2009
.
Last edited by hosh; Oct 20th, 2009 at 2:47 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
1
  #4
Oct 20th, 2009
any ideas what is wrong with this code?
What exactly is the purpose of your code?
To me it seems like you're reading a file character by character, and then printing out only the digits?

Why doing a conversion to integer if you only want to print the digits out?
  1. char c = fgetc(in); /* read character */
  2. if( isdigit(c) ) /* if character is a digit */
  3. putchar( c ); /* print character */

Also, if you want to do a conversion to integer for only one digit, then you can use a small trick:
  1. char c = '5';
  2. int i = c - '0'; /* i now contains: 5 */
Please note that you should only apply this trick when you're sure the character variable of which you're subtracting the ASCII value of zero holds a character which is in this range:
'0' <= character <= '9'
You can check on this by using the isdigit() function:
  1. int i;
  2. char c = '9';
  3.  
  4. if( isdigit( c ) )
  5. i = c - '0'; /* i now contains: 9 */
But again, in your case it seems to me like you don't need a conversion, as it seems like you're just printing digits on your screen, nothing else.

Note: you'll need to include the ctype.h header in order to use the isdigit() function.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: hosh is an unknown quantity at this point 
Solved Threads: 0
hosh hosh is offline Offline
Newbie Poster
 
0
  #5
Oct 20th, 2009
Thank you guys.
Now it makes sense...
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC