YAKOVM
YAKOVM

Reputation: 10153

read an unknown number of lines

I need to implement in C the program ,which reads an unknown number of lines from stdin. I know that the maximum number of lines is 100. I tried to use gets ,but I don`t know when to stop the loop. Can you advise me how to implement it?

Upvotes: 2

Views: 482

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

This depends on when you want your program to stop. There are several common approaches:

  1. Never: you run an infinite loop until end-user hits ^C or otherwise terminates your program using the facilities of your operating system
  2. Until the user enters a special marker, i.e. a "keyword" QUIT, EXIT, etc. on a line by itself
  3. Until the user enters an empty line (i.e. hits Enter)

Since the max number in your case is 100, you can use it as the limit to automatically terminate the input once the max is reached.

Upvotes: 1

Related Questions