writing a read_line function similar to (scanf or gets)

This is a discussion on writing a read_line function similar to (scanf or gets) within the C Programming forums, part of the General Programming Boards category; Code: /*i am writing a character input function that has the following properties *it ignores all white space character before ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195

    writing a read_line function similar to (scanf or gets)

    Code:
    /*i am writing a character input function that has the following  properties
    *it ignores all white space character before starting to collect input.  
    *it stops reading after a white space character has been entered  *when the the '\n' character is entered it stores the character and   then adds the    
    * null character.  
    it leaves all other character that could not be entered and continues reading the   * them   during its next call.
     */ 
     #include  <stdio.h>  
    #include   <stdlib.h>
    
    #define MAX 100   
    
    char name[MAX + 1]; 
    int age; 
     int read_line( char str[], int n) 
    { int ch, i; 
     
    while ( (ch == getchar()) != '\n' && ch != '\t && ch != EOF)
      if ( i < n && ch != ' ')
     {    if ( str[0] == ' ' ) i--;  
          str[i++] = ch;
     }
    str[i] = '\n'; 
    str[i + 1] = '\0';
      
    printf("%s", str); 
     return i;
     }    
    
     int main(void)
     {     
     printf("\nThis program asks for you age and your name"); 
    
         printf("\nEnter you names: ");  
       read_line(name, MAX+ 2);    
    
       printf("\nEnter your age: ");
        age == getchar(); 
    
      printf(" Your name is: %s and your age %d", name, age); 
     return 0;
     }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195
    this code ignores the first character and but it skips the next white space characters that that appear after it has started reading input. i do not know how to discard input after it is used once; and to ignore the other character that cannot enter the array.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195
    it took me about four days to figure out how to ignore the whitespaces before the collection of input

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    i is used uninitialised.

    You are missing a closing inverted comma in the expression in your while loop.

    I don't understand why you are checking the first element of 'str' for whitespace, when nothing can possibly be assigned to that element yet.

    I believe you wish to use assignment (=) instead of equivalence (==) to assign a value to age. Also, only a single character will be read into 'age' and it will be stored as a character, not as a number like you want it to be. How about you use your new beaut' (once it's done ) read_line() function to read the age into a string and then print it from there?

    A flag variable would be a good way to skip only the leading whitespace, or even just reading the string in and then left-shifting it if there is leading whitespace.

    That aside, your code is not a bad attempt at all. Work on your indentation a little more though.
    Last edited by DeadPlanet; 01-02-2012 at 09:12 AM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195

    thanks

    but i thought when u dont initialise a variable , it is initialised as 0??

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195
    thanks for the encouragement @deadplanet i knew this code was pathetic.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by Nyah Check View Post
    but i thought when u dont initialise a variable , it is initialised as 0??
    No, it's initialized to whatever value is in that area of memory at the time. Unless you use calloc(), you have to manually set or memset() the variable to zero.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Global variables are set to zero by C, if they have no other initial value. Local variables are not set to zero, unless you declare them to have that value.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    195
    but in thought getchar() return s an interger value of the input thats why i used it

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by Nyah Check View Post
    but in thought getchar() return s an interger value of the input thats why i used it
    So let me then ask you what integer value would you expect it to return if you enter the character 'W' (as an example)? The getchar function returns an integer value based on a table associating characters with integers and the character '4' does not map to integer 4 but more likely the integer value 52.

    You can convert that to integer value 4 by subtracting the char '0' but still this only lets you deal with integer values in the range from 0 through 9, nothing higher:
    Code:
    char cValue = '4';
    int iValue = cValue - '0';  /* iValue should have value of 4 */
    To deal with a proper numeric value such as an age it's better to use something like scanf with a %d format specifier. Or you can read the data in as a character string and convert using functions such as sscanf, strtol or atof.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function similar to gotoxy()
    By juice in forum C Programming
    Replies: 4
    Last Post: 12-25-2011, 01:42 PM
  2. function similar to execvp()
    By Elkvis in forum Linux Programming
    Replies: 10
    Last Post: 09-22-2011, 12:25 PM
  3. Replies: 3
    Last Post: 08-14-2011, 01:38 PM
  4. function similar to strpos() ?
    By willc0de4food in forum C Programming
    Replies: 18
    Last Post: 10-08-2005, 05:13 PM
  5. Replies: 0
    Last Post: 04-27-2003, 07:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21