C中令人讨厌的结构错误

时间:2015-05-22 17:45:11

标签: c struct charts

我试图创建一个程序来添加,排除和打印州,城市名称,总人口,男性人口,女性人口,男性百分比和女性百分比。我继续得到这个该死的错误:

错误:(在第60行)' Chart'未申报(首次使用此功能)

#include<stdio.h>
#include<string.h>
struct Chart
{
    char uf[3]; // refers to state
    char ct[100]; // refers to city
    float pop;  // refers to population
    float h;  // refers to mens pop
    float m; // refers to womens pop
    float ph; // refers to percentage of men in the given city
    float pm; // refers to percentage of women in the given city
};
void Cad(struct Tabela vet[], int n); /* register function */
void exc(struct Tabela vet[], int n); /* exclude function*/
void list(struct Tabela vet[], float min, float max, char list); /* print function*/
int main()
{

    int n; /* number of lines to add or remove*/
    char c,list; /* c is the choice of the user (remove,add,print) and list is for the list function*/
    float max, min; /* parameters for list function*/
    struct Tabela vet[1000]; /* initializing struct */

    scanf(" %c",&c); /* User choice.*/


    while((c !='f') && (c!='F')) /* programm keep running til f or F is pressed*/
    {
        if((c=='c') || (c=='C')) /* register func */
        {
            scanf("%d",&n); /*number of lines to add*/
            Cad(vet, n);
        }
        else if((c=='e') || (c == 'E')) /*remove func*/
        {
            scanf("%d",&n); /* number of lines to remove*/
            exc(vet,n);

        }
        else if((c=='l') || (c == 'L')) /*print func*/
        {
            scanf(" %c",&list); /* print based on men, women or population choice*/
            scanf("%f %f",&min, &max); /*minimum and maximum values to be printed */
            list(vet, min, max , list); 
        }
            else
            {
                printf("Invalid command. Press 'F' to terminate.\n");
            }
            return 0;
        }
    return 0;
}
void Cad(struct Tabela vet[], int n)
{
    int i,j;
    for(i=j=0; i<n; j++)
    {
        if(Chart[j].mu ==0) /* if the position is free, it is going to be filled*/
        {
            scanf("%s",vet[j].uf); // filling positions
            scanf("%s",vet[j].ct;
            scanf("%f",&vet[j].pop);
            scanf("%f",&vet[j].h);
            scanf("%f",&vet[j].m);
            scanf("%f",&vet[j].ph);
            scanf("%f",&vet[j].pm);
            i++;
        }
    }
}
void exc(struct Tabela vet[], int n)
{
    int i,j;
    char excCt[100], excUf[3]; /*The data is erased based on the State and city name */
    for(i=0; i<n; i++)
    {
        scanf("%s",excUf);
        scanf("%s",excCt);
        for(j=0; j<1000; j++)
        {
            if(strcmp(excUf,vet[j].uf)==0)
            {
                if(strcmp(excCt,vet[j].ct)==0)
                {
                    vet[j].h = 0;
                    vet[j].m = 0;
                    vet[j].ph = 0;
                    vet[j].pm = 0;
                    break;
                }
            }
        }
    }
}
void list(struct Tabela vet[], float min, float max, char list)
{
    int i;
    if((list == 'H') || (list == 'h')) // user choice is men
    {
        for(i=0; i<1000; i++)
        {
            if((vet[i].homens>=min) && (vet[i].homens<=max)) /* if the pop of men is higher than the minimum value and lower then the maximum, the line is printed */
            {
                printf("%s ,",vet[i].uf);
                printf("%s ,",vet[i].ct);
                printf("%.0f ,",vet[i].pop);
                printf("%.0f ,",vet[i].h);
                printf("%.0f ,",vet[i].m);
                printf("%.1f ,",vet[i].ph);
                printf("%.1f",vet[i].pmu);
                printf("\n");
            }
        }
    }
    else if((list == 'M') || (list == 'm')) // user choice is women
    {
        for(i=0; i<1000; i++)
        {
            if((vet[i].mulheres>=min) && (vet[i].mulheres<=max))
            {
                printf("%s ,",vet[i].uf);
                printf("%s ,",vet[i].municipio);
                printf("%.0f ,",vet[i].pop);
                printf("%.0f ,",vet[i].homens);
                printf("%.0f ,",vet[i].mulheres);
                printf("%.1f ,",vet[i].phomem);
                printf("%.1f",vet[i].pmulher);
                printf("\n");
            }
        }
    }
    else if((list == 'P') || (list == 'p')) /* user choice is absolute population */
    {
        for(i=0; i<1000; i++)
        {
            if((vet[i].pop>=min) && (vet[i].pop<=max))
            {
                printf("%s ,",vet[i].uf);
                printf("%s ,",vet[i].ct);
                printf("%.0f ,",vet[i].pop);
                printf("%.0f ,",vet[i].h);
                printf("%.0f ,",vet[i].m);
                printf("%.1f ,",vet[i].ph);
                printf("%.1f",vet[i].pm);
                printf("\n");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

struct Chart是一种类型。你不能写一个类型。您需要声明此类型的变量,然后将其写入:

struct Chart
{
....
} chart; // <-- This is your variable name

答案 1 :(得分:1)

struct Chart
{
//something
};

创建用户定义的数据类型。数据类型不能具有。你需要有一个 type 的变量来为它添加一些值。

因此,您需要定义该类型的变量以使用它,例如

struct Chart Chart[10];

然后,您可以使用

Chart[j].mu = .....