Solved

grabbing inputs

Posted on 2004-09-03
7
256 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:nor_miranda
  • 5
7 Comments
 
LVL 11

Expert Comment

by:avizit
Comment Utility
(input<100)!=EOF

this condition will never be true

( input < 100 ) can never be equal to EOF
0
 
LVL 11

Expert Comment

by:avizit
Comment Utility
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
 
LVL 11

Expert Comment

by:avizit
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:teratoma
Comment Utility
To simplify, the thing that should be compared to EOF is num :)
0
 

Author Comment

by:nor_miranda
Comment Utility
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
 
LVL 11

Accepted Solution

by:
avizit earned 100 total points
Comment Utility
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
 
LVL 11

Expert Comment

by:avizit
Comment Utility
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now