read int with scanf until new line

14,441

Solution 1

scanf is not line oriented by design. It is aimed for blank separated inputs, where blanks are any combinations of spaces, tabs, \r or \n.

If you want to use line oriented input, you should use fgets to get a line in a string and then strtok and sscanf to parse the string. Your code could become :

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

/* maximum size of an input line : adapt to your needs */
#define SIZE 256

int main() {

    int i, numberOfNumbs=0,total=0,value, valsRead;
    float average;
    char line[SIZE], *val;
    char delims[] = " \t\r\n";

    if (fgets(line, SIZE, stdin) == NULL) { /* EOF */
        fprintf(stderr, "No input\n");
        return 1;
    }
    if (line[strlen(line) - 2] != '\n')  {/* line too long */
        fprintf(stderr, "Line too long\n");
        return 2;
    }
    val = strtok(line, delims);
    valsRead = sscanf(val, "%d",&value);

    while(valsRead>0)
    {
        numberOfNumbs++;
        total +=value;
        printf("Read %d\n", value);
        val = strtok(NULL, delims);
        valsRead = (val == NULL) ? 0 : sscanf(val, "%d",&value);
    }

    average=(float)total/(float)numberOfNumbs;
    printf("You read %d values. Average = %f.",numberOfNumbs, average);

    return (0);
}

Solution 2

For what you want to do for entry from the keyboard I would suggest reading in a string with

char string[100];

scanf("%s",string)

then you can work through the string looking for numbers with, for example, the command

sscanf(string,"%d",&integer)

the tricky thing here is that after you have got your first integer you need to find out where the first space is after your number.. say it is n characters after the start then you could repeat with

sscanf(string+n,"%d",&integer)

and test the value of sscanf to see if something was correctly read in.

This is not a full answer, but suggestion of a different strategy.

I do this sort of thing reading in from a file, but here you have to be careful because for a file end of line characters can either be ascii code 10, 13 or both 10 and 13 for unix, mac and msdos format files (and sorry I can't remember which is which.

Solution 3

if you want multiple values, then better use array, also if want values until you enter new line '\n' is scanf("%[^\n]", string);

Share:
14,441
bli77
Author by

bli77

Updated on June 04, 2022

Comments

  • bli77
    bli77 almost 2 years

    I'm new to the c language and used java before, so I'm not so familiar with some things... I want to read an indefinite number of integer until there's a new line. I know, that new line is ⁄n and I already have a code, so the integer are read until you type in a letter, but it doesn't stop, if there's a new line.

    #include <stdio.h>
    
    int main() {
    
    int i, numberOfNumbs=0,total=0,value, valsRead;
    float average;
    
    valsRead = scanf("%d",&value);
    
    while(valsRead>0)
    {
    numberOfNumbs++;
    total +=value;
    printf("Read %d\n", value);
    valsRead = scanf("%d",&value);
    }
    
    average=(float)total/(float)numberOfNumbs;
    printf("You read %d values. Average = %f.",numberOfNumbs, average);
    
    return (0);
    }
    

    The input should be something like this: 23 4 114 2 34 3224 3 2 ⁄n

    Thanks in advance