• 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?
 
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);


0
 
avizitCommented:
(input<100)!=EOF

this condition will never be true

( input < 100 ) can never be equal to EOF
0
 
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



0
Live Q & A: Securing Your Wi-Fi for Summer Travel

Traveling this summer? Join us on June 18, 2018 for a live stream to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn 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

0
 
teratomaCommented:
To simplify, the thing that should be compared to EOF is num :)
0
 
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?
0
 
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


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.