read int with scanf until new line

十年热恋 提交于 2021-02-04 16:20:07

问题


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


回答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);
}



回答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.




回答3:


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



来源:https://stackoverflow.com/questions/31522555/read-int-with-scanf-until-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!