【发布时间】:2016-09-21 02:47:37
【问题描述】:
我在从 C++ 问题中读取文件时遇到问题。请在下面找到我的代码并告诉我您的想法。我不断收到“文件打开失败!”
问题:
编写一个程序,生成一个条形图,显示过去 100 年中中西部小镇 Prairieville 以 20 年为间隔的人口增长情况。程序应从文件中读取 1900、1920、1940、1960、1980 和 2000 年的人口数据(四舍五入到最接近的 1000 人)。对于每一年,它应该显示日期和一个由每 1000 人一个星号组成的栏。例如,让我们使用 3000、7000、10000、25000、29000 和 30000。
以下是图表如何开始的示例:
大草原人口增长
(每个*代表1000人)
1900 ***
1920 *******
1940 **********
// main.cpp
// Population Chart
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int year,population;
ifstream inputFile;
inputFile.open("People.txt");
//if (inputFile.fail())
if(!inputFile)
{
cout << "File open failure!";
}
cout << "PRAIRIEVILLE POPULATION GROWTH\n" <<endl;
cout << "(each * represents 1000 people)\n" <<endl;
while (inputFile >> population)
{
for (year =1900 ; year<=2020; year += 20)
{
cout<< year;
for (int i = 1; i <= population/1000; i++)
{
cout<<"*";
}
cout<< endl;
}
}
inputFile.close();
return 0;
}
【问题讨论】:
-
您是否尝试将完整的系统路径添加到您的文件中?
-
您能否就完整系统路径的含义提出建议?