0

So I was thinking that instead of inputting
1
2
3
with enter at the end of each input.
which use{ scanf("%i",&a) in a loop}
I wish to to input somthing like
1 2 3
which uses { scanf("%i %i %i",&a,&b,&c)}
the problem is if I declare sth like

int num;
scanf("%i",num);
info[num];
using loop I can input num times until the array is full
so the inputs look like
1
2
3
...
num-1
num

but if I want my inputs to be

1 2 3 4 5 6 .... num-1 num

I can't just scanf("%i %i ... num times",&info[1],&info[2]...&info[num]);
any suggestion on what can i do?

1

It can be done, and in fact, will be done as part of some of the last psets. But since this is a teaching environment, and one of the things this class teaches is how to find out how research questions like these, I'll point you in the right direction. ;-)

Like all programming problems, it's best broken down into parts. In this case, you want to:

  1. Read in a string containing a bunch of values on one line,
  2. extract each number, still in string form, from the string,
  3. convert each individual string to a number form,
  4. store the numbers.

The following items will help you with each step. The first problem is that scanf doesn't normally read in spaces. http://www.c4learn.com/c-programming/c-reading-string-with-spaces-scanf/ will explain how to read in a string with spaces.

For extracting the numbers in string form, functions like strtok() and it's cousins will be useful.

Of course, by now you should be familiar with atoi() for the conversion from string to int.

Storing the numbers is a matter of programming as needed for your program.

That's all the pieces and parts. If not, Google can provide a wealth of information! You should be able to put the puzzle together now. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .