Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C &引用;“之前的分析错误”&引用;代币;分配给变量[]时_C_Arrays_String_Loops_While Loop - Fatal编程技术网

C &引用;“之前的分析错误”&引用;代币;分配给变量[]时

C &引用;“之前的分析错误”&引用;代币;分配给变量[]时,c,arrays,string,loops,while-loop,C,Arrays,String,Loops,While Loop,我的C程序如下。它试图从一个文件中读取一组值,并创建一个表来生成一些浮点数的平均值 int main(void) { int lake, beach, samples, count, sum; float e_coli, ave; char recc[20], lakename[20], beachname[20]; FILE *in; sum = 0; count = 0; in = fopen("july15.data&qu

我的C程序如下。它试图从一个文件中读取一组值,并创建一个表来生成一些浮点数的平均值

int main(void)
{

    int lake, beach, samples, count, sum;
    float e_coli, ave;
    char recc[20], lakename[20], beachname[20];
    FILE *in;
    sum = 0;
    count = 0;
    in = fopen("july15.data", "r");
    printf("Lake   |   Beach   | Avg E Coli Level |  Reccomendation |\n");
    while (1)
    {
        fscanf(in, "%d, %d, %d", &lake, &beach, &samples);
        count = samples;
        while (count < samples)
        {
            fscanf(in, "%f", e_coli);
            sum = sum + samples;
        }
        ave = sum / samples;
        if (lake == 1)
            lakename[] = "Ontario";
        else if (lake == 2)
            lakename[] = "Erie";
        else if (lake == 3)
            lakename[] = "Huron";
        else if (lake == 4)
            lakename[] = "Muskoka";
        else if (lake == 5)
            lakename[] = "Simcoe";
        if (beach == 100)
            beachname[] = "Kew Beach";
        else if (beach == 101)
            beachname[] = "Sunnyside Beach";
        else if (beach == 102)
            beachname[] = "Sandbanks";
        else if (beach == 201)
            beachname[] = "Port Dover";
        else if (beach == 202)
            beachname[] = "Port Burwell";
        else if (beach == 203)
            beachname[] = "Crystal Beach";
        else if (beach == 301)
            beachname[] = "Goderich";
        else if (beach == 302)
            beachname[] = "Sauble Beach";
        else if (beach == 303)
            beachname[] = "Kincardine";
        else if (beach == 401)
            beachname[] = "Muskoka Beach";
        else if (beach == 501)
            beachname[] = "Sibbald Point";
        if (samples < 3)
            recc[] = "INSUFFICENT DATA";
        else if (samples > 3 && ave < 50)
            recc[] = "OPEN";
        else if (samples > 3 && ave > 50)
            recc[] = "CLOSED";
        printf("%s,      %s,      %f,      %s\n", lakename, beachname, ave, recc);
    }
    return(0);
}
int main(无效)
{
int湖、海滩、样品、计数、总和;
漂浮大肠杆菌,ave;
char recc[20],lakename[20],beachname[20];
文件*in;
总和=0;
计数=0;
in=fopen(“2015年7月数据”,“r”);
printf(“湖泊|海滩|平均大肠杆菌水平|再淹没|\n”);
而(1)
{
fscanf(在、%d、%d、%d、、湖泊、海滩和样本中);
计数=样本;
while(计数<样本)
{
fscanf(在,“%f”,大肠杆菌中);
总和=总和+样本;
}
ave=总和/样本数;
如果(湖==1)
lakename[]=“安大略省”;
否则如果(湖==2)
lakename[]=“伊利”;
否则如果(湖==3)
拉克内梅[]=“休伦”;
否则如果(湖==4)
lakename[]=“Muskoka”;
如果(湖==5),则为else
lakename[]=“Simcoe”;
若(泳滩==100)
beachname[]=“丘湾海滩”;
如有其他情况(泳滩==101)
beachname[]=“阳光海滩”;
若有其他情况(泳滩==102)
beachname[]=“沙洲”;
如有其他情况(泳滩==201)
beachname[]=“多佛港”;
如有其他情况(泳滩==202)
beachname[]=“伯威尔港”;
如有其他情况(泳滩==203)
beachname[]=“水晶海滩”;
如有其他情况(泳滩==301)
beachname[]=“Goderich”;
若有其他情况(海滩==302)
beachname[]=“索布尔海滩”;
如有其他情况(泳滩==303)
beachname[]=“Kincardine”;
若有其他情况(海滩==401)
beachname[]=“Muskoka海滩”;
如有其他情况(泳滩==501)
beachname[]=“Sibbald点”;
如果(样本数<3)
recc[]=“数据不足”;
否则如果(样本数>3且平均值<50)
recc[]=“打开”;
否则,如果(样本数>3且平均值>50)
recc[]=“已关闭”;
printf(“%s,%s,%f,%s\n”,lakename,beachname,ave,recc);
}
返回(0);
}
不幸的是,它在第22、32和54行生成“]”标记之前的错误
parse error
。实际上,这些是定义字符串的第一个区域。因此,第22行是
lakename[]=“安大略省”
第32行是
beachname[]=“丘湾海滩”
,第54行是
recc[]=“数据不足”


我尝试将空格数插入
[]
(即
lakename[9]=“Ontario”
)中,但随后出现错误
“赋值从指针生成整数而不强制转换”

数组在C中不可赋值

有效的分配/初始化为

char lakename[] = "Ontario";
你得到的错误

lakename[9] = "Ontario"
因为
lakename[9]
只能容纳一个字符


无论如何,还有其他选项可以复制字符串,比如
strcpy()
和格式为
lakename[]=“安大略”的语句
beachname[]=“丘湾海滩”不是有效的C。请改为使用类似于strcncpy(beachname,“Kew Beach”,sizeof(beachname)-1的内容上面的某个地方做一个
beachname[sizeof(beachname)-1]=0用于安全。或者,让变量
char*
,然后将它们指定为指针。在这种情况下,这更合适。

假设您的输入数据看起来像下面的示例,此解决方案应该可以工作

1 101 5 5.2 10.9 20.1 30.56 80.6
1 301 4 50.0 30.3 40.2 20.4
代码解决方案

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void){
    int lake, beach, samples;
    char recc[20], lakename[20], beachname[20];

    FILE *in;
    in = fopen ("july15.data", "r");
    if(in == NULL) {
        exit(EXIT_FAILURE); 
    }

    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    
    printf ("Lake   |   Beach   | Avg E Coli Level |  Reccomendation |\n");

    while ((read = getline(&line, &len, in)) != -1) {
        // Count number of spaces
        double sum = 0;
        int tokens = 0;
        int i = 0;
        for (; i < len; i++) {
            if (line[i] == ' ') {
                tokens++;
            }
        }
        // Add one extra token
        tokens = tokens + 1;
        if (tokens < 4) {
            continue;
        }

        i = 0;
        // Convert tokens to floats
        double *values = malloc(sizeof(double) * tokens);
        char *token = strtok(line, " ");  
        while (token != NULL) {
            values[i++] = atof(token);
            token = strtok(NULL, " ");
        }

        lake = values[0];
        beach = values[1];
        samples = values[2];
        for (i = 3; i < tokens; i++) {
            sum = sum + values[i];
        }
        free(values);

        double ave = sum / samples;
        
        if (lake == 1)
            strcpy(lakename, "Ontario");
        else if (lake == 2)
            strcpy(lakename, "Erie");
        else if (lake == 3)
            strcpy(lakename, "Huron");
        else if (lake == 4)
            strcpy(lakename, "Muskoka");
        else if (lake == 5)
            strcpy(lakename, "Simcoe");

        if (beach == 100)
            strcpy(beachname, "Kew Beach");
        else if (beach == 101)
            strcpy(beachname, "Sunnyside Beach");
        else if (beach == 102)
            strcpy(beachname, "Sandbanks");
        else if (beach == 201)
            strcpy(beachname, "Port Dover");
        else if (beach == 202)
            strcpy(beachname, "Port Burwell");
        else if (beach == 203)
            strcpy(beachname, "Crystal Beach");
        else if (beach == 301)
            strcpy(beachname, "Goderich");
        else if (beach == 302)
            strcpy(beachname, "Sauble Beach");
        else if (beach == 303)
            strcpy(beachname, "Kincardine");
        else if (beach == 401)
            strcpy(beachname, "Muskoka Beach");
        else if (beach == 501)
            strcpy(beachname, "Sibbald Point");
        
        if (samples < 3)
            strcpy(recc, "INSUFFICENT DATA");
        else if (samples > 3 && ave < 50)
            strcpy(recc, "OPEN");
        else if (samples > 3 && ave > 50)
            strcpy(recc, "CLOSED");

        printf("%s,      %s,      %f,      %s\n", lakename, beachname, ave, recc);
    }

    free(line);
    fclose(in);

    return(0);
}
定义GNU源
#包括
#包括
#包括
内部主(空){
int湖、海滩、样品;
char recc[20],lakename[20],beachname[20];
文件*in;
in=fopen(“2015年7月数据”,“r”);
if(in==NULL){
退出(退出失败);
}
char*line=NULL;
尺寸长度=0;
阅读;
printf(“湖泊|海滩|平均大肠杆菌水平|再淹没|\n”);
while((read=getline(&line,&len,in))!=-1){
//计算空间数
双和=0;
int标记=0;
int i=0;
对于(;i#include <stdio.h>

int main (void){
    int lake, beach, samples, count, sum;
    float e_coli, ave;
    char *recc;
    char *lakename;
    char *beachname;
    FILE *in;

    sum = 0;
    count = 0;
    in = fopen ("july15.data", "r");
    printf ("Lake   |   Beach   | Avg E Coli Level |  Reccomendation |\n");

    int iterator;
    char lake_names[6][10]={
        {"Ontario"},
        {"Erie"},
        {"Huron"},
        {"Muskoka"},
        {"Simcoe"},
        {"No lake"},
    };

    int beach_code[12] ={100,101,102,201,202,203,301,302,303,401,501,0};

    char beach_names[12][20]={
        {"Kew Beach"},
        {"Sunnyside Beach"},
        {"Sandbanks"},
        {"Port Dover"},
        {"Port Burwell"},
        {"Crystal Beach"},
        {"Goderich"},
        {"Sauble Beach"},
        {"ncardine"},
        {"Muskoka Beach"},
        {"Sibbald Point"},
        {"No beach"},
    };

    char reccs[3][20]={
        {"INSUFFICENT DATA"},
        {"OPEN"},
        {"CLOSED"},
    };




    while (fscanf (in, "%d, %d, %d", &lake, &beach, &samples) !=  EOF)
    {
        recc=&reccs[0][0];
        lakename=&lake_names[5][0];
        beachname=&beach_names[11][0];

        count = samples;

        while (count < samples)
        {
            fscanf (in, "%f",&e_coli);
            sum = sum + samples;
        }

        ave = sum / samples;

        for(iterator=1;iterator<5;iterator++)
        {
            if(lake==iterator)
                lakename=&lake_names[iterator][0];
        }

        for(iterator=0;iterator<11;iterator++)
        {
            if(beach==beach_code[iterator])
                beachname=&beach_names[iterator][0];
        }

        if (samples < 3)
            recc = &reccs[0][0];
        else if (samples > 3 && ave < 50)
            recc = &reccs[1][0];
        else if (samples > 3 && ave > 50)
            recc = &reccs[2][0];
        printf("%s,      %s,      %f,      %s\n", lakename, beachname, ave, recc);
    }
    return(0);
}