1

I want to read some numbers from the console. The numbers will appear in this way -

5 1 2 3 4 5
4 5 6 7 8
6 2 3 4 5 6 7
..............
EOF

The starting number represents how many number will appear in that line, i.e., the first number of the first line is 5, so there will be 5 more numbers on this line. The end of input will be denoted by the EOF (end-of-file).

I thought of reading the whole line as a string and then convert them to numbers but I want to know is there any other way to do this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178

2 Answers2

3

The 'standard' answer is scanf(). The trouble with the standard answer is that it won't allow you to check that there are the correct number of numbers on a line. So, your idea of reading a line and then converting it piecemeal is much better for error detection.

Take a look at How to use sscanf() in loops; the answer there shows you the basics of what you should do. It isn't an exact duplicate. You will read a first value identifying how many entries are on the line, followed by code to read that many entries into an array, with appropriate diagnostics if the data doesn't match the format claimed for it.

If you are in charge of the data format, you should consider dropping the count field - let the computer count how many values are on the line. Computers are good at counting, and it removes a source of errors to be detected and handled (so it makes the programming easier). (If you do change the input format, your question becomes a duplicate of the linked question.)

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

Have a look at the documentation of scanf:

int count, current, i;
while(scanf("%d", &count) > 0)
{
   for(i = 0; i < count; i++)
   {
       scanf("%d", &current);
       // store current
   }
}
  • You missed out testing the return value of scanf inside the for-loop. –  May 28 '11 at 14:45
  • You also have no way of knowing whether you hit end of line when you are supposed to do so. `scanf()` will eat newlines that occur early and ignore newlines that occur late. Given the data `1 1 3 / 1 2` (where the slash is the line break), the first line will be 'OK' (even though it contains 2 values, not 1), and the 3 will be interpreted as the count for the 'second' line, which only has 2 values on it. This is why `scanf()` is seldom the correct function to use. (I've used `scanf()` more on SO than I have in my previous 25 years of programming!) – Jonathan Leffler May 28 '11 at 14:52
  • @Neil Butterworth: I didn't add it as it wasn't mentioned in the question, but OP can do that if needed, providing he reads the `scanf` documentation. –  May 28 '11 at 14:53
  • @Jonathan Leffler: The question seemed like it was worded like a programming assignment where only valid data would be inputted, therefore one wouldn't have to worry about the situation you mentioned. OP should be aware of it, though. –  May 28 '11 at 14:56