user3373360
user3373360

Reputation: 93

Read in space delimited numbers using scanf and storing them in an array - C

I'm trying to take user input containing a bunch of space delimited 1-3 digit numbers using scanf() and storing them into an int array and printing each seperate one on a new line to test but it's not working. Here is what I have so far:

#include <stdio.h>
#include <string.h>

int main()
{
  int sourceArr[500];
  int i = 0;

  printf("\nEnter ciphertext: \n");
  while (scanf("%d", &sourceArr[i++]) == 1);
  for (int j=0;j<500;j++) {
    printf("%d\n", sourceArr[j]);
  }
}

so the user is asked to input a sequence of numbers like so:

Enter ciphertext: 
23 122 32

and I want to store 23 in sourceArr[0], 122 in sourceArr[1] and 32 in sourceArr[2] and then print each one like so:

23
122
32

But the program idles right after entering the input and won't print the numbers.

Upvotes: 0

Views: 4264

Answers (3)

SeeRam
SeeRam

Reputation: 33

scanf returns the number of items successfully scanned. Try the below code:

    #include <stdio.h>

    int main()
    {
      int sourceArr[500];
      int i = 0;
      int sc =0; //scanned items
      int n=3; // no of integers to be scanned

      printf("\nEnter ciphertext: \n");
       while(sc<n){
          sc += scanf("%d",&sourceArr[i++]);
       }
      for (int j=0;j<i;j++) {
        printf("%d\n", sourceArr[j]);
      }
    }

Upvotes: 0

VAndrei
VAndrei

Reputation: 5590

On success, scanf returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

You could change it to:

#include <stdio.h>
#include <string.h>

int main()
{
  int sourceArr[500];
  int i = 0;

  printf("\nEnter ciphertext: \n");
  while (scanf("%d", &sourceArr[i++]) == 1);
  for (int j=0;j<i-1;j++) {
    printf("%d\n", sourceArr[j]);
  }
}

Then if you enter: 1 2 3 4 5 6 x (and hit enter)

It will display as you want.

If you don't like the 'x' you could use this while line:

while (scanf("%d", &sourceArr[i++]) != EOF);

and then type something like: 12 23 345 554 (hit enter) and then (ctrl+z) in windows or (ctrl+d) in unix.

see this thread: End of File(EOF) of Standard input stream (stdin)

ctrl+z explicitly makes scanf return EOF because the console has no EOF and needs to be sent this way.

Upvotes: 1

chux
chux

Reputation: 154243

The "%d" consumes leading white-space such as ' ' and '\n', not just ' '.

So when the user enters "123 456 789" Enter, scanf() is called again waiting for more input. It will continue to wait until non-numeric data in entered, stdin is closed or rarely experiences an IO error.

Since stdin is usually buffered, stdin and scanf() do not see any input from a line until the Enter is pressed.

while (scanf("%d", &sourceArr[i++]) == 1);

If code needs to to end input with Enter, use fgets() to read a line. Then parse it using sscanf() or strtol(). The below uses "%n" to record the number of char scanned.

char buf[100];
printf("\nEnter ciphertext: \n");
if (fgets(buf, sizeof buf, stdin) == NULL)
  Handle_EOF();

char *p = buf;
int n;
while (sscanf(p, "%d%n", &sourceArr[i], &n) == 1) {
  i++;
  p += n;
}

for (int j = 0; j < i; j++) {
  printf("%d\n", sourceArr[j]);
}

Upvotes: 1

Related Questions