-5

I've been searching a while for this, and couldn't find an answer. I will be appreciated if someone knows how do to that!

PROBLEM: I must code a program that will store some numbers, but I don't know how much numbers there will be! What can I do?

I was wondering if I could use timing to get things done. I mean, if 5 secs has passed and there is no input of data, then start processing these numbers. It would work, but I couldn't code this. Can someone help, please?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Normally you'd expect some kind of terminator, e.g. CR/LF or end of file. Is there some reason why you can't rely on this ? – Paul R May 31 '13 at 16:43
  • 1
    If you're writing in C++, you should probably not be using `scanf()`. If you're writing in C, you'd probably be better off using `fgets()` or `getline()` and then `sscanf()` than using `scanf()` directly. If you are using `scanf()`, then `while (scanf("%d", &x) == 1) { ...save x into dynamically allocated array... }` would do the trick. – Jonathan Leffler May 31 '13 at 16:43
  • The user will input the data 'manually', can't ask for any kinds of terminator character. I'm writing on C by the way. – Nickollas Aranha May 31 '13 at 16:49
  • When entering data into a program, you either have a 'sentinel' value (such as a non-numeric string) mark the end of the numeric input, or you type control-D (Unix -- Control-Z on Windows) to let the program know there's no more input, or you redirect the input from a file and your program gets EOF when the file is finished. – Jonathan Leffler May 31 '13 at 16:54
  • Thanks for the answer! Isn't there a way to do the 'timing' between an input and another? – Nickollas Aranha May 31 '13 at 16:56
  • An adaptation of @JonathanLeffler s loop to store the length might work well for you: `size_t length; for (length = 0; scanf("%d", &array[length]) == 1; length++) { /* resize array based on length */ }` – autistic May 31 '13 at 16:57
  • Yes; there are ways to do timing of inputs. However, it is platform specific and non-standard C, so since you've not told us anything about your platform, we can't help you. – Jonathan Leffler Jun 01 '13 at 01:20

1 Answers1

2

1) first solution:

You can ask the user to enter the number of desired element at the beginning.

2) second solution:

Keep scan numbers till you get EOF from the user. and store the input number into a linked list or a dynamically allocated array (resize your array size withe the realloc)

3) third solution

keep scan numbers with a timeout. If there is no input during the timeout then the program will consider that the user have finished input numbers and then the program stop reading from stdin. The input numbers could stored into linked list or dynamic array as indicated in the second solution. Use select() with the scanf() in order to add the timeout behaviour as indicated in this answer

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • Hey, thanks for the answer! 1) Can't do that, the input data won't have the amount of numbers. 2) EOF isn't only returned while reading a FILE *f? Thanks in advance! – Nickollas Aranha May 31 '13 at 16:45
  • 1
    (1) Users are bad at counting; programs that ask for 'how many' are not nice to use -- for all that every programming exercise seems to do that. Maybe this is a relic from the days of Fortran 66 before you could rely on detecting EOF. (2) Why a linked list? Why not a dynamically allocated array? – Jonathan Leffler May 31 '13 at 16:45
  • @JonathanLeffler is a good solution too. I will update my answer with that thank you – MOHAMED May 31 '13 at 16:48
  • Yes, @JonathanLeffler. I like the second solution, too. I note the loop in your question-comment doesn't need to detect `EOF`; It handles all of `scanf`s failures in this case. I like that, even more. – autistic May 31 '13 at 16:49
  • 1
    @NickollasAranha the user can input EOF via stdin with `CTRL+D` for Linux and `CTRL+Z` for Windows – MOHAMED May 31 '13 at 16:50
  • @NickollasAranha I added a 3rd solution in the answer. the third solution uses the timeout behaviour – MOHAMED May 31 '13 at 16:58
  • I'm not sure about the third solution. Is there any logical proof that `stdin` won't be unbuffered as opposed to line-buffered? This affects the behaviour of `scanf`. – autistic May 31 '13 at 17:02
  • @MOHAMED thanks for the answer! I'll try to code using the select() function! – Nickollas Aranha May 31 '13 at 17:02