how do you read in an integer as a text string.
Plsgive example. Thanks
how do you read in an integer as a text string.
Plsgive example. Thanks
There is a different function that works the same as atoi() except it does error checking. It would probably be a bit better to use that one - I just cant think of the name of the function off hand.Code:#include <stdlib.h> int main() { char buff[32]; int number; fgets(buff,32,stdin); number = atoi(buff); printf("You entered in the number: %d\n",number); return 0; }
>I just cant think of the name of the function off hand.
You're probably thinking of strtol.
>how do you read in an integer as a text string.
You always read integers as text strings. Any input is a sequence of characters that are converted by functions such as scanf into the appropriate type. Can you be more specific as to what you want?
My best code is written with the delete key.
I want to read in a 5 digit integer, and print it as follows with spaces between each number.
Ex: Read 12345,
and print 1 2 3 4 5.
I heard some smart programmer in my class say read it in as a text string. This is a beginning class and we haven't discussed this. Any ideas.
Thanks.
DW
>Ex: Read 12345,
>and print 1 2 3 4 5.
Okay, any user input will be text, it's your program that converts that text into a meaningful value of another type. So your only problem here is making sure that you read an integer and not anything else. The problem becomes one of validation and not conversion. If you want to assume valid input then it's as simple as this:
For validation techniques, see the FAQ.Code:#include <ctype.h> #include <stdio.h> int main ( void ) { char buffer[BUFSIZ]; printf ( "Enter a number: " ); fflush ( stdout ); if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) { int i; for ( i = 0; isdigit ( buffer[i] ); i++ ) printf ( "%c ", buffer[i] ); printf ( "\n" ); } return 0; }
My best code is written with the delete key.
If Iwanted more spaces between the output number (3 spaces between each number vs one, what do I add to the program??
I added spaces before %c. Thanks anyway. DW