• C

grabbing inputs

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!
nor_mirandaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avizitCommented:
(input<100)!=EOF

this condition will never be true

( input < 100 ) can never be equal to EOF
avizitCommented:
okay I will give you the general idea.  

while the number you read using scanf  != EOF
if number > 9
 display


------
basically your scanf should be inside the while loop , preferably in the while condition itself.

show what you have done and then maybe we can suggest more



avizitCommented:
err when i said

 (input<100)!=EOF  will never be true .

i was wrong , imeant the condition will always be true and hence it wil be aninfinite loop

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

teratomaCommented:
To simplify, the thing that should be compared to EOF is num :)
nor_mirandaAuthor Commented:
well i've taken out EOF... still having problem with grabbing my input from the unix redirection, pipe.

apperently the number that is being grab is not in minutes, which is mandatory for this program.
my other code to be grabbed from

int main (void)
{
  int num = 100;
  srand(time(NULL));

  while (num-- >0)
  printf("time: %d\n", rand ( ) % 10);

return (0);
}

and the updated code

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

    while(time<100)
    {
      if (time>9)
      {
          printf("While the num is %d, the time is %d mins", num, time);
      }
   time++;
  }
return 0;
}

what would be the reason that i cant grab on the redirection?
avizitCommented:
you are mostly done but you still have one error

you are reading the stdin only once .. i.e whatever the data will come from redirection you are reading once
to read it 100 times you have to put the scanf inside the while loop

move scanf from where it is to

while(time < 100){
     scanf("%d",&num);

.......

also to make your output look pretty

put a "\n" i.e newline at t he end of the print string
i.e change the printf to

printf("While the num is %d, the time is %d mins\n", num, time);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avizitCommented:
okay I am confused but the requirements

is it to ignore the input number if its less than or equal to  9 and print only when the input number is > 9

in that case our if shold look like this
 if (num>9)


if on the other hand the requirement is to ignore the first 9 lines of the input file then your current if statement si correct


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.