I will be getting three lines of input. The first line will give me 2 integers and the third line will give me 1 integer. But the second line can give me any number of integers ranging between 1 to 100. For example, the input could be:
2 1
5 6 1 9 2
10
or could be:
10 4
5 6
9
I can read the second line of integer input into an integer array for a fixed number of integers, but cannot do so for a varying number of integers. I suppose, in this case, I should use a while loop which will break when scanf()
finds a newline. How do I code that?
I am actually a newbie in programming and am unaware of most of the functions. The only string functions I know of are
strlen()
andstrcmp()
. And my i\o function knowledge is limited toprintf()
andscanf()
.Anyhow, I solved my problem in this way:
This works! The value of
num
had to be equal to the valuei
because my program needed it.Read the line into a buffer (@John Coleman) using using
fgets()
orgetline()
.Parse the string looking for whitespace that may contain a
'\n'
, exit loop if found. Then callstrtol()
orsscanf()
to read the 1 number. Check that function's return value for errors too.Repeat above steps.