【发布时间】:2015-04-19 23:15:20
【问题描述】:
我是一个非常新手的程序员,我正在尝试制作一个程序来读取一个包含 5 个学生姓名(仅限名字)的 txt 文件以及每个学生的四个考试成绩。我正在尝试将名称读入一个名为 students 的数组中,然后将分数读入 4 个名为 test1、test2、test3、test4 的单独数组中,然后从监视器上显示它。该文件如下所示:
史蒂夫 78 65 82 73
肖恩 87 90 79 82
安妮 92 90 89 96
卡罗尔 72 65 65 60
凯西 34 50 45 20
我很难分解阵列并组织它们。有人能帮我吗?请记住我是新手,所以我对编程知之甚少。
这是我目前的代码:
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctime>
#define over2 "\t\t"
#define over3 "\t\t\t"
#define over4 "\t\t\t\t"
#define down5 "\n\n\n\n\n"
using namespace std;
int main(int argc, char *argv[])
{
ifstream inputFile;
//Constant for max string size
const int SIZE = 5;
//Constant for the scores
string names[SIZE], fileName, line, test1[SIZE], test2[SIZE], test3[SIZE], test4[SIZE];
//input section, user enters their file name
cout << down5 << down5 << over2 << "Please enter your file name: ";
cin >> fileName;
system("CLS");
//open the file containing the responses
inputFile.open(fileName.c_str());
cout << endl;
//kicks you out if file isn't found
if (inputFile)
{
for(int i = 0; i < SIZE; i++)
{
getline(inputFile, line);
names[i] = line;
getline(inputFile, line);
test1[i] = line;
getline(inputFile, line);
test2[i] = line;
getline(inputFile, line);
test3[i] = line;
getline(inputFile, line);
test4[i] = line;
}
inputFile.close();
}
cout << down5 << over3 << "Student\tTest1\tTest2\tTest3\tTest4\n";
cout << over3 << "-------\t-----\t-----\t-----\t-----\n";
for(int i = 0; i < SIZE; i++)
{
cout << over3 << names[i] << endl;
cout << over3 << test1[i] << endl;
cout << over3 << test2[i] << endl;
cout << over3 << test3[i] << endl;
cout << over3 << test4[i] << endl;
}
return 0;
}
【问题讨论】: