我想从文件中提取数据并将它们存储到myWorld中,但我的for循环不起作用,程序一旦进入for循环就不会循环一次。我不确定问题是什么。到目前为止,这是我的代码。
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct Country
{
double pop1950;
double pop1970;
double pop1990;
double pop2010;
double pop2015;
string name;
};
const int MAXCOUNTRIES = 300;
struct World
{
int numCountries;
Country countries[MAXCOUNTRIES];
} myWorld;
void printPop ();
int main ()
{
printPop();
return 0;
}
void printPop()
{
ifstream inFile("population.csv");
if (!inFile.fail())
{
cout << "File has opened successfully.";
}
if (inFile.fail())
{
cout << "File has failed to open.";
exit(1);
}
for (int i = 0; i < MAXCOUNTRIES; i++)
{
inFile >> myWorld.countries[i].pop1950 >> myWorld.countries[i].pop1970 >> myWorld.countries[i].pop1990
>> myWorld.countries[i].pop2010 >> myWorld.countries[i].pop2015;
getline (cin, myWorld.countries[i].name);
cout << "loop is running" << endl;
}
inFile.close();
}
答案 0 :(得分:3)
我认为你的循环正在运行,它只是在等待用户输入:
getline (cin, myWorld.countries[i].name);