4

I have to input values whose frequency i don't know...

For example first input: 1 32 54 65 6

second input: 2 4 5

What i first thought was, scan the values, if new line '\n' then break the loop, but that didn't go so well, so instead i said i use characters, then i typecast to get the number but the problem with this also came that it scan one character by one and if its its a negative value its also a problem;

Something like this

#include <stdio.h>

int main(){
int myarray[20];
int i=0, data;

while(1){
      scanf("%d", &data);
      if (data == '\n') break; 
      myarray[i]=data;
}

return 0;
}

but then, scanf jumps all the special characters and look for only ints... is there a way to scan ints to an array and when there is a newline it stops?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
user3564573
  • 680
  • 1
  • 12
  • 24

2 Answers2

6

My advice, go for fgets().

  1. Read the whole line from the input
  2. Tokenize using space [] [or your preferred delimiter] [using strtok()]
  3. Allocate memory to store the integer
  4. Convert the string input to integer [maybe strtol()] and store each integer.

Optionally, you may want to add some validation and error checking.

Read more about fgets() here.

also, don't forget to get rid of the trailing \n stored in the read buffer by fgets()

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I see fgets get a string, if i change the data into string using say strtol or atoi am not quite sure if its going to recognize negative values?? – user3564573 Nov 26 '14 at 11:13
  • @user3564573 if you notice, the return type is `long int` for `strtol()` and I see no reason not to hadle negative numbers. as for delimiter, you cannot use `-` sign as a delimiter. – Sourav Ghosh Nov 26 '14 at 11:15
  • @user3564573 Read the manual page for `strtol()` if you're not familiar with it. Hint: it handles negative values just fine. – unwind Nov 26 '14 at 11:16
  • Thanks, let me try it out – user3564573 Nov 26 '14 at 11:17
  • Note that `strtok_r` is not a standard function in C, but a common extension in many compilers. – chux - Reinstate Monica Nov 26 '14 at 15:38
  • A limitation with this approach is that the maximum line length needs to be known ahead of time (unless code uses `fgets()` multiple times on the same line - somewhat complicated) and OP implies an unknown count of numbers per line. IMO such a limitation is not bad, but code should report when a line is read that is longer than expected. – chux - Reinstate Monica Nov 26 '14 at 15:46
  • @chux Thank you sir for mentioning the `extension` regarding `strtok_r()`. Also, by mentioning `some validation and error checking`, I implied the read buffer length overrun, nevertheless, thanks for elaboration. – Sourav Ghosh Nov 26 '14 at 16:04
  • The link to the documentation should be as follows -> https://linux.die.net/man/3/fgets – Jean-Victor Côté May 22 '17 at 20:14
  • @Jean-VictorCôté emmm...did you miss `Read more about fgets() here.` part? – Sourav Ghosh May 23 '17 at 02:17
0

Recommend using fgets() as suggested by Sourav Ghosh

Otherwise code can search for '\n' before reading each int

#include <ctype.h>
#include <stdio.h>
#define N (20)

int main(void) {

  int myarray[N];
  int i = 0;

  while (1) {
    int ch;
    while (isspace(ch = fgetc(stdin)) && ch != '\n')
      ;
    if (ch == '\n' || ch == EOF)
      break;
    ungetc(ch, stdin);

    int data;
    if (scanf("%d", &data) != 1)
      break; // Bad data
    if (i < N)
      myarray[i++] = data;
  }

  return 0;
}
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256