0

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.

user3373360
  • 93
  • 1
  • 11

3 Answers3

1

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.

Community
  • 1
  • 1
VAndrei
  • 5,420
  • 18
  • 43
  • Okay this works for single digit numbers but the numbers the user will be entering can be anywhere from 1-3 digits eg: 1, 23, 213 *in between 0 - 255* so how could I get this to work exactly? – user3373360 Oct 09 '14 at 16:47
  • @VAndrei: Is this the only way to solve this issue? I mean entering x at the end of the input doesn't really look good right? – Gopi Oct 09 '14 at 17:00
  • No actually it works for all numbers. Not just single digits. @pala this is not the only solution. This is just the least "invasive" :) – VAndrei Oct 09 '14 at 17:56
  • 1
    @VAndrei can we make scanf () return EOF by giving inputs through stdin – Gopi Oct 09 '14 at 17:58
1

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]);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

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]);
      }
    }
SeeRam
  • 33
  • 2