function to read an int from input

This is a discussion on function to read an int from input within the C Programming forums, part of the General Programming Boards category; i got a homework problem. i'm learning on my own but still you'd probably consider it homework. anyway i'm not ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    function to read an int from input

    i got a homework problem. i'm learning on my own but still you'd probably consider it homework. anyway i'm not understanding this programming exercise... what is it asking me to do exactly?
    write a function that skips over input until encountering a digit. store that digit and subsequent digits into a string until encountering a nondigit. place that character back into input and convert the string to an int. the function should use a pointer argument to supply the number value. return EOF if the function encounters end of file, otherwise return 1. use getc() and ungetc(). in short, the function should read the int from input such as this: be22again.
    so what i need to know is
    1) what input are they talking about? just stdin? or do i pass a stream pointer (though it doesn't say so)?
    2) under what condition does the function return 1? it returns EOF if that's encountered, but otherwise how does it know when to stop looking? does it search ALL input from stdin until it finds an int and then return 1 when the int terminates? but what if the user doesn't simulate EOF?
    3) am i totally wrong? i just want to know what exactly i'm supposed to do...
    Last edited by Salem; 06-08-2004 at 12:08 AM. Reason: Tag

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ack, change that code tag to a quote tag quick!

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    This might help you:
    Code:
    int ch;
    while ( (ch=getc())  != '\n' && ch != EOF )
    {
      if ( isdigit(ch) ) /* Put the digit into the string */
      else /* If a digit hasn't been inputted yet continue on with the loop, 
    otherwise put the character back (ungetc()) and then stop the loop */
    }

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    woops... it's late i didn't even think to use a quote tag... anyway that helps, so there are no arguments passed to getc()?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so there are no arguments passed to getc()?
    No, getc takes a FILE * as the argument. So Thantos's corrected code would be:
    Code:
    int ch;
    while ( (ch=getc(stdin))  != '\n' && ch != EOF )
    {
      if ( isdigit(ch) ) /* Put the digit into the string */
      else /* If a digit hasn't been inputted yet continue on with the loop, 
    otherwise put the character back (ungetc()) and then stop the loop */
    }
    Of course, getc(stdin) is equivalent to getchar() and both are equivalent to fgetc(stdin) except that if the former two are macros they may evaluate the stream more than once.
    My best code is written with the delete key.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps sorry for forgetting the stream parameter.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Opps sorry for forgetting the stream parameter.
    I forget too sometimes and blame it on too many ways to do the same thing.
    My best code is written with the delete key.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Don't forget that most of those ways have only minor differences in them (Like a couple extra or missing characters in their names). Yes that does sound like a good thing to blame!

  9. #9
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    hey, i don't know nothin' about no macros, at least not yet, but i think i understand what i need to do now thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

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