Question:
Storing integers using scanf?
2012-12-03 01:36:28 UTC
hey, beginner in C here...

How do I store not-specified amount of integers into a field? For example:

input: 2 9 7 2 6 \n

(= I don't know how many integers will user input, but I want to store them all individually and end the loop when the user hits enter).

Can anyone help me, please?
Four answers:
Merc
2012-12-03 04:33:30 UTC
The manual page says scanf() usually returns the numbers of items read. So you could make your loop similar to:



while (scanf(...) == 1) {

...

}



As for storing the integers: if you know nothing about dynamic memory allocation, you can use a "large" fixed array. So your code would look something along of:



#define MAX_ITEMS 50



int list[MAX_ITEMS];

int list_size = 0;



while(scanf("%d", &num) == 1) {

if (list_size == MAX_ITEMS) {

exit("list too big")

}

list[list_size++] = num;

}



(Code not checked: expect bugs.)



==



@Martin wrote: "Thanks a lot, but this still doesn't work, because the loop ends only if I type a letter"



You need to signal end of input (to the whole program) by pressing Ctrl+D (after ENTER; on Linux) or Ctrl+Z (after ENTER, probably; on Windows/DOS).



But if you need to feed more input to the program afterwards then this technique isn't suitable for you (Sorry for not noticing that this might indeed have been your case). Probably the simplest solution is to use a "sentry value": stop reading the list if you see a special value like -1 or 999.



Other solutions:



(1) Read the whole line (by using fgets() or getline() or whatever) and then parse it yourself, as @jplatt39 suggested.



(2) Re-organize your program so it reads the list as its last input. Or read the list via the command-line, as @jplatt39 suggested, and the other input via scanf().



(3) Use getc() till a digit (or newline) it met and then ungetc() it if it's a digit so the next scanf() can see it.



As you see, scanf() isn't suitable for "complicated" input. For "complicated" input programmers use a "regular expressions" library, or they write a lexer (and/or parser), or they use a library for a common format like YAML or XML or JSON.
Cody
2012-12-03 01:58:08 UTC
Ok do you know about for loops? its been a little while since used C but i think this is what you will want, ask the user how many number they will enter and then assign that to a variable (i chose x but use what makes sense for you).

for(i = 0, i < x, i ++1)

{scanf statment}

this will run the loop how ever many times as the user chose numbers to put in.
2016-12-13 14:31:41 UTC
The scanf function we could you settle for enter from widely utilized in, which for us is frequently the keyboard. The scanf function can do lots of different issues, in spite of if this is frequently unreliable except used interior the least complicated procedures. this is unreliable because of the fact it does not cope with human errors o.k.. yet for easy classes this is sturdy adequate and easy-to-use. the least complicated utility of scanf sounds like this: scanf("%d", &b); this methodology will examine in an integer value that the person enters on the keyboard (%d is for integers, as is printf, so b must be declared as an int) and place that value into b. The scanf function makes use of a similar placeholders as printf: int makes use of %d drift makes use of %f char makes use of %c character strings (pronounced later) use %s you need to placed & in front of the variable utilized in scanf. the clarification why will grow to be sparkling as quickly as you be taught approximately assistance. this is easy to forget approximately the & sign, and once you forget approximately it your application will especially much consistently crash once you run it. The & operator is used to furnish handle of the variable.
jplatt39
2012-12-03 04:11:42 UTC
Given the input you list you can getstring() (If you are crazy. It's one of C's' least safe functions). Use strtok() to break the string up and while you have input from the line convert each with atoi(). Not efficient but you can do it. You can also enter them into the command line with something like buffer=(int*)malloc(sizeof(int)*(argc-1); for (i=1;i


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...