【问题标题】:expected ‘,’ or ‘;’ before ‘{’ token { (C++)'{' token { (C++) 之前的预期',' 或';'
【发布时间】:2013-10-01 23:58:30
【问题描述】:

所以我在编译我的代码时遇到了这个错误(在 '{' token { 之前应该是 ',' 或 ';' 我知道在 stackoverflow 上可能有很多这样的错误,但似乎看不到寻找解决方案:

我是 C++ 新手。这是代码:我必须从文本文件(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);

}

data.txt 如下所示:

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

谁知道问题出在哪里???

【问题讨论】:

  • 将您的代码缩减为SSCCE
  • main() 的右括号似乎不见了。
  • 我也收到此错误:在 '{' token 之前不允许函数定义
  • 见上文,你少了一个括号。
  • 它就在最后,或者我在这里遗漏了什么?

标签: c++ c compiler-errors token curly-brackets


【解决方案1】:

您在 main 函数中定义 void countrySelection(int countryOption),这在 c++ 中是不允许的。

将函数移到主函数之上,它应该可以编译。 此外,您必须将countrySelection 中使用的变量定义为全局变量,否则函数无法访问它们。

【讨论】:

  • 我将它们定义为全局,但是每当我运行程序并通过选择其中一个选项来调用函数时...(加拿大、美国、俄罗斯)我没有得到任何输出
  • 您的加载循环有错误的迭代,将其更改为“int i = 0; i
猜你喜欢
  • 1970-01-01
  • 2012-04-22
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
相关资源
最近更新 更多