Thread: Text string

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Text string

    how do you read in an integer as a text string.
    Plsgive example. Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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;
    }
    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.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Cool Integer

    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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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:
    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;
    }
    For validation techniques, see the FAQ.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    More spaces

    If Iwanted more spaces between the output number (3 spaces between each number vs one, what do I add to the program??

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Ignore request

    I added spaces before %c. Thanks anyway. DW

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Reading text file into a string obj
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2008, 11:42 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM