gpt4 book ai didi

c++ - 在 ‘,’ token { (C++)

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:30 25 4
gpt4 key购买 nike

所以我在编译我的代码时遇到了这个错误(预期的','或';'在'{' 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

有人知道问题出在哪里吗???

最佳答案

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

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

关于c++ - 在 ‘,’ token { (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com