简体   繁体   English

预期的','或';' 在“ {”令牌{(C ++)之前

[英]expected ‘,’ or ‘;’ before ‘{’ token { (C++)

So I'm getting this error when i compile my code (expected ',' or ';' before '{' token {) I know there may be many of these errors out there on stackoverflow, but can't seem to find a solution: 因此,当我编译代码(预期在'{'令牌{之前)时会出现此错误,我知道在stackoverflow上可能存在许多此类错误,但似乎找不到一个解:

I'm new to c++. 我是C ++的新手。 Here is the code: I have to read data from a text file (data.txt) and display it: 这是代码:我必须从文本文件(data.txt)中读取数据并显示它:

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

int main(){

FILE *fptr;
char country[5][20];
int population[5];
int landMass[5];
int option;
int i;
int countryOption;
int gdp[5];
int populationDensity[5];
int gdpHead[5];

//open file for reading
fptr = fopen("data.txt", "r");      

//Error checking
if (fptr == NULL) {                
   printf("Unable to open data.txt");
   return(1);
}

//input from user
printf("Hi welome to the country database!");
getchar();
system("cls");
printf("Select a country for information:\n");
printf("1)Canada\n");
printf("2)Italy\n");
printf("3)China\n");
printf("4)USA\n");
printf("5)Russia\n");
printf("6)All\n");
printf("Type in the option you want:");
scanf("%d", &option);
system("cls");

//reads data from data.txt and assigns to variables
for (i = 1; i <= 5; i++) {
    fscanf(fptr, "%s %d %d %d", country[i], &population[i], &landMass[i], &gdp[i]);
    populationDensity[i] = (population[i]/landMass[i]);
    gdpHead[i] = ((gdp[i]*1000000)/population[i]); 

    if (option == 6) {
        printf("Here is info on all the countries in our database:\n");
        printf("Country: %s\n", country[i]);     
        printf("Population: %d\n", population[i]);
        printf("LandMass: %d\n", landMass[i]);
        printf("GDP: %d\n", gdp[i]);
        printf("Population density: %d\n", populationDensity[i]);
        printf("Population density: %d\n\n\n", gdpHead[i]);  
    }
}       

void countrySelection(int countryOption)
{
     printf("Here is some info on the country you chose:\n");
     printf("Country: %s\n", country[countryOption]);     
     printf("Population: %d\n", population[countryOption]);
     printf("LandMass: %d\n", landMass[countryOption]);
     printf("GDP: %d\n", gdp[countryOption]);
     printf("Population density: %d\n", populationDensity[countryOption]);
     printf("Population density: %d\n\n", gdpHead[countryOption]);  
}

//function that prints the info
if (option < 6) {
   countrySelection(option);
}                   

fclose(fptr);
system("pause");
return(0);

}

The data.txt looks like this: data.txt看起来像这样:

Canada
42000000
9984670
1821000 
Italy
60920000
301230
2013000
China
1351000000
9706961
8227000   
USA
313900000
9826675
15680000
Russia
143000000
17098246
2015000  

Any one have a clue as to what the problem is??? 任何人都有什么问题的线索吗???

You are defining void countrySelection(int countryOption) inside the main function, which is not allowed in c++. 您正在主函数中定义void countrySelection(int countryOption) ,这在c ++中是不允许的。

Move the function above the main function and it should compile. 将功能移到主要功能上方,即可进行编译。 Also you have to define the variables used in countrySelection as global variables, otherwise the function has no access to them. 另外,您还必须将countrySelection使用的变量定义为全局变量,否则该函数将无法访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM