0

I am doing a assignment which requires the input of a list of numbers and get the output when I press Enter on the keyboard. This is the code I am trying the use to get the list of numbers when I enter, but it doesn't work:

#include <stdio.h>

int main(){
    int arra[100];
    int i ;
    int j = -1;
    while (scanf("%d",&i) != 1){ 
        arra[++j] = i;
    }
    printf("\n");
    int k;
    for(k = 0; k < j; k++){
        printf("%d",arra[k]);
    }
    return 0;
}

I want to print the elements of arra.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

6

First off, massive kudos for testing the return value from scanf, most people just blindly assume it works. It's just a shame you're using it in the wrong way :-)

You want the loop to continue as long as the return value is 1, meaning that you managed to scan an integer. That means it should be:

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

That also means that any non-numeric input will cause scan failure and hence the while loop will exit. So, if you enter:

3 1 4 1 5 9 stop

you should successfully see the numeric values from the array.

The only other thing is to clean up your j handling since the k loop will stop early. This can be done with:

for (k = 0; k <= j; k++) {

Alternatively, leave that loop alone and just change how you initialise and modify j:

int j = 0;
:
arra[j++] = i;

I tend to find the second choice more C-like since j is then a count of the elements in the array rather than the maximum index.


And, of course, you're open to a buffer overflow attack at the moment since you assume nobody will enter more than a hundred numbers. So, don't use this as a homework solution (what you have is good enough with the slight bug fixes) but I'd tend to write it as something like:

#include<stdio.h>

#define SZ 100

int main (void){
  int arra[SZ], i, nxt = 0;

  while ((nxt < SZ) && (scanf ("%d", &(arra[nxt])) == 1))
    nxt++;
  printf ("\n");

  for (i = 0; i < nxt; i++)
    printf ("%d ", arra[i]);

  return 0;
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Why `k<=j`? it should be `k – Gopi Nov 24 '14 at 06:10
  • @Gopi, it's because OP was starting `j` at -1 and pre-incrementing, hence it ended up at the highest index (count-1) rather than the count itself. – paxdiablo Nov 24 '14 at 06:14
  • ok cool!! Your code looks good – Gopi Nov 24 '14 at 06:15
  • but I want to get output after i press ENTER in keyboard, not when I put the character –  Nov 24 '14 at 06:22
  • @hellocomputer: If you want to know when the user hits return, you can't use `scanf()`; it doesn't care in the slightest about newlines. You'd need to read a line of input, probably with `fgets()`, and then use `sscanf()` to parse that line iteratively. That's a little harder, but not very much harder. You do need to read [How to use `sscanf()` in loops](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops/3975254#3975254) to find out how to know where to resume the scan. – Jonathan Leffler Nov 24 '14 at 06:25
  • thank you,but how can i implement sscanf() in my code? –  Nov 24 '14 at 06:31
  • @hellocomputer: see http://powerfield-software.com/?p=65 – paxdiablo Nov 24 '14 at 06:36
0

Check the below code:

#include<stdio.h>

    int main(){
        int arra [100];
        int i ;
        int k;
        int j = 0; /* index from 0 */
        printf("Keep entering numbers and press q once done \n");
        while (scanf("%d",&i) == 1){ /* scan for integers */
         arra[j++] = i;
        }
        printf("\n");

        for(k = 0; k < j; k++){
            printf("%d",arra[k]);
        }
        return 0;
    }
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • when I press ENTER in keyboard it still get input –  Nov 24 '14 at 06:23
  • @hellocomputer `scanf()` fails when you enter a character instead of a integer. So enter q at the end after entering all your numbers – Gopi Nov 24 '14 at 06:27
  • thank you, so which can I use to do –  Nov 24 '14 at 06:37
  • @hellocomputer I didn't get your question `so which can I use to do?` – Gopi Nov 24 '14 at 06:43
  • I am sorry, I mean which function or method can I use to implement my code –  Nov 24 '14 at 08:18
  • @hellocomputer You can do it as I have done else if you want `\n` as your last input then you need to use `fgets` followed by `sscanf()` – Gopi Nov 24 '14 at 08:29