Grabbing input

This is a discussion on Grabbing input within the C Programming forums, part of the General Programming Boards category; Need help in grabbing my input from another program file, UNIX redirector operator. How do I go about doing it? ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Unhappy Grabbing input

    Need help in grabbing my input from another program file, UNIX redirector operator.
    How do I go about doing it?

    Iím supposed to get 100 random input and have an input point of 9, input > point display msg. If input<point go next input no msg. I have to use loop to grab input and use to compare input with the point.

    Here is what I've come out with...

    int main(void)
    {
    int num, input=0;
    scanf("%d",&num);

    while((input<100)!=EOF)
    {
    if (input>9)
    {
    print("The num is %d while the input is %d ", num, input);
    }
    input++;
    }


    return 0;
    }

    Thanks for the help in advance!

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well first up, we should have a look at what your code might have looked like, had you chosen to use code tags:

    Code:
    int main(void)
    {
        int num, input = 0;
        scanf("%d", &num);
    
        while ((input < 100) != EOF) {
            if (input > 9) {
                print("The num is %d while the input is %d ", num, input);
            }
            input++;
        }
    
    
        return 0;
    }
    I notice you are using the 'print' function to display the values of the variables. If you decide to use printf() instead, be sure to #include <stdio.h>

    Now about your while loop. When will the value stored in the variable input ever equal EOF? Also, if you want your call to scanf() to be repeated, perhaps it should be in the loop too.

    ~/
    Last edited by kermit; 09-03-2004 at 04:15 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >When will the value stored in the variable input ever equal EOF?
    That really isn't a concern since the value stored in input isn't being tested against EOF, the boolean value 0 or 1 is because those are the only possible results from a relational comparison (ie. input < 100). Of course, because EOF is defined as a negative quantity and 0 and 1 hold the interesting position of not being considered negative, we now have a problem.

    >scanf("%d", &num);
    If you must use scanf, at least try to use it safely and check the return value:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int num, input = 0;
    
      if ( scanf("%d", &num) == 1 ) {
        while (input < 100) {
          if (input > 9) {
            printf("The num is %d while the input is %d\n", num, input);
          }
          input++;
        }
      }
    
      return 0;
    }
    My best code is written with the delete key.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Prelude
    >When will the value stored in the variable input ever equal EOF?
    That really isn't a concern since the value stored in input isn't being tested against EOF, the boolean value 0 or 1 is because those are the only possible results from a relational comparison (ie. input < 100). Of course, because EOF is defined as a negative quantity and 0 and 1 hold the interesting position of not being considered negative, we now have a problem.
    Good point - for my part I was hinting that the variable input had nothing to do with EOF, and if the OP was to incorporate a test for EOF, it would more appropriate to do so in a context where EOF could actually occur. I stumbled over the boolean test while I was thinking about the fact that input is not the variable for testing against EOF.. if that makes any sense...

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for my part I was hinting that the variable input had nothing to do with EOF
    And it's poorly named to boot. I would have swapped the use of num and input because num makes more sense as a counter and input as a value taken from the user.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    i've amended my program to

    Code:
    int main(void)
    {
      int num, input=0;
      scanf("%d",&num);
    
       while(input<100)
       {
         if (input>9)
         {
           printf("The num is %d while the input is %d ", num, input);
         }
         input++;
        }
    
    return 0;
    }
    it gives me the different kind of ouput...

    tried using perlude's code... it works okie.. but it should display several output not stop until 2 outputs (maybe ard 20).how do i do that? and what does it mean by....

    >>> if ( scanf("%d", &num) == 1 )

    could you explain it to me... thank you!

  7. #7
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by miryellis
    and what does it mean by....

    >>> if ( scanf("%d", &num) == 1 )

    could you explain it to me... thank you!
    scanf() (like many other library functions) has a return value - this return value should always be checked. In Prelude's example she was checking the return value by comparing it to 1, which corresponds to the format specifier in the code. If the call to scanf() looked like this:
    Code:
    if ( scanf("%d%d%s", &num) == 3 )
    then you would check for 3 items.

    The man page for scanf() is helpful..

    RETURN VALUE
    These functions return the number of input items assigned, which can be
    fewer than provided for, or even zero, in the event of a matching fail-
    ure. Zero indicates that, while there was input available, no conver-
    sions were assigned; typically this is due to an invalid input charac-
    ter, such as an alphabetic character for a `%d' conversion. The value
    EOF is returned if an input failure occurs before any conversion such
    as an end-of-file occurs. If an error or end-of-file occurs after con-
    version has begun, the number of conversions which were successfully
    completed is returned.
    As to your other query...

    tried using perlude's code... it works okie.. but it should display several output not stop until 2 outputs (maybe ard 20).how do i do that?
    If you want repeated calls to scanf (not sure if this is what you want - you are hard to understand) then you need to put the call inside a loop.

    ~/
    Last edited by kermit; 09-05-2004 at 09:50 AM.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it gives me the different kind of ouput...
    My best attempt at deciphering your requirements is that you want to read 100 values from the user (that's quite a lot for anyone to type in though) and if the value is greater than 9, print the message:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int input;
      int n;
    
      for ( n = 0; n < 100; n++ ) {
        /* Always check the return value of input functions */
        if ( scanf ( "%d", &input ) != 1 )
          break;
        if ( input > 9 )
          printf ( "n is %d, input is %d\n", n, input );
      }
    
      return 0;
    }
    If this isn't what you want then you'll need to give us an example of what output you expect with what input.

    >could you explain it to me...
    scanf returns the number of items successfully converted. Generally, if that number does not match with the number of format flags then an error has occurred. You should always check the return value of input functions because users have a tendency to input things that you don't expect.
    My best code is written with the delete key.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kermi3
    They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.
    I'm not so sure I like [code] tags now. It appears that the use of [code] tags changes string literals! I find this very troubling. I must look into this further.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    ^^^
    Hilarious

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 11:04 PM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-30-2007, 12:16 AM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  4. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 02:50 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 04:07 AM

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