| | |
need help with atoi
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
Hi,
can anyone help me to solve this problem?
I have this code:
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
can anyone help me to solve this problem?
I have this code:
c Syntax (Toggle Plain Text)
FILE *in; char i =fgetc(in); int perm=0; perm =atoi(&i); printf("%d\n", perm);
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????
1
#2 Oct 20th, 2009
•
•
•
•
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
i as it is in your code, nor a pointer to a char like you give it as in atoi(&i) Something like:
c Syntax (Toggle Plain Text)
char twenty_three[] = "23"; int result; result = atoi(twenty_three);
Last edited by Aia; Oct 20th, 2009 at 1:13 am.
1
#4 Oct 20th, 2009
•
•
•
•
any ideas what is wrong with this 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?
C Syntax (Toggle Plain Text)
char c = fgetc(in); /* read character */ if( isdigit(c) ) /* if character is a digit */ 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:
C Syntax (Toggle Plain Text)
char c = '5'; int i = c - '0'; /* i now contains: 5 */
'0' <= character <= '9' You can check on this by using the isdigit() function:
C Syntax (Toggle Plain Text)
int i; char c = '9'; if( isdigit( c ) ) i = c - '0'; /* i now contains: 9 */
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."
![]() |
Similar Threads
- Problem using atoi for my argv argument (C)
- atoi function (C)
- atoi(string.substr(x,x)) Possible? (C++)
- Help with atoi (C++)
- help me about atoi (C++)
- Please help with atoi() (C)
Other Threads in the C Forum
- Previous Thread: Array of Pointers to structures...
- Next Thread: Best Windows IDE for C
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o incrementoperators initialization kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue multi mysql number odf open opensource owf pattern pdf performance pointer pointers posix probleminc process program programming pyramidusingturboccodes read recursion recv repetition research scanf scheduling scripting segmentationfault send shape socket socketprograming stack standard string strings systemcall testautomation unix user voidmain() wab win32api windows.h






