3

I am able to ask a user for an input and insert it to a linked list. So the following will get 1 integer from user:

  printf("Enter an integer: ");
  scanf("%d",&value);
  insert(value); // insert value to linked list

But I want to user to be able to enter many integers (as many as they want). Example: Enter an integer: 5 6 7 8 9 and add 5 to insert then, add 6 to insert and so on.

I read this post "reading two integers in one line using C#" and the suggested answer was to use an array of strings but I don't want to do that. I want every number entered by the user to be entered in a linked list.

main function :

int main(){
   printf("Enter integer(s) : ");
   scanf("%d",&num);
   insert(num);
   return 0;
}

thank you

Community
  • 1
  • 1
Rain Man
  • 1,163
  • 2
  • 16
  • 49
  • 4
    You could just loop on your `printf`, `scanf`, `insert` cycle for a fixed number of times, or until the user enters something else when done. It depends upon your more specific needs. – lurker Feb 17 '15 at 21:06

3 Answers3

4

One way to do this is by first scanning in an integer to identify the number of integers to be read, and then reading that many integers and storing them into your list.

int i, size;
int x;
scanf("%d", &size);
for(i=0; i < size; i++){
    scanf("%d", &x);
    insert(x);
}

Example input would be as such:

4
10 99 44 21
TheHorse
  • 234
  • 2
  • 9
  • great, never thought of that :) – Rain Man Feb 17 '15 at 21:27
  • User input of `"4 10 99 44 21"` would get the same response as well as `"4 10"` `"99 44 21"`. There is not detection for end-of -line. – chux - Reinstate Monica Feb 17 '15 at 21:33
  • @chux Yes that is correct. Additionally it may be safer to first scan all the integers into an array of length `size`, loop through the array, inserting each element concurrently and finally freeing the memory. – TheHorse Feb 17 '15 at 21:36
  • @chux yes, you are right, how can we fix this issue? – Rain Man Feb 17 '15 at 21:36
  • @Dave Sorry what exactly is the issue? – TheHorse Feb 17 '15 at 21:44
  • @SovMoose if the user input a wrong size `3 20 10` then enter `10 30 40` the last two numbers entered will not be added to the list, is it possible to prevent that? – Rain Man Feb 17 '15 at 21:48
  • 1
    @Dave `scanf()` is difficult to use to read _lines_ of user input. Most format specifiers treat space and `'\n'` equally so the concept of _line_ vs space separator is lost. Better to read the _line_ using `fgets()` or `getline()` (*nix) and then parse/scan the line using `sccanf()`, `strtol()`, etc. There are hundred of SO posts on this. (try searching `[c] scanf vs fget`) – chux - Reinstate Monica Feb 17 '15 at 21:53
1

You could use formatter in scanf which takes everything by the time user hits enter

char array[256];
scanf("%[^\n]",array)

Then use

int num;
while(*array !='\0') // while content on array is not equal to end of string
{
  if(isspace(*array)) // need to check because sometimes when atoi is  returned, 
                         // we will move only one location size of char,
                         //and convert blank space into integer
   *array++;

else{
   num=atoi(*array);   // function atoi transform everything to blank space
   insert(num);
   *array++;   // then move to the next location in array

 }
}
1

Why don't you add a simple while/for loop for this

printf("total numbers to input? ");
scanf("%d",&i);
printf("\nEnter integer(s) : ");
while(i--){
   scanf("%d",&num);
   insert(num);
}
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225