【发布时间】:2018-02-09 21:42:48
【问题描述】:
我需要创建一个struct 数组,其中struct 是学生(数据类型为string FirstName、string LastName、int testScore 和char Grade)。我已经弄清楚了函数原型的逻辑,并且我已经学习了一些基本的文件 i/o。我希望结构数组中有 20 个学生,并且信息将从 .txt 文件中读取。这是我遇到麻烦的地方。这是我的基本代码。
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct studentType {
string firstName;
string lastName;
int testScore;
char Grade;
};
int main()
{
studentType students[20];
int i;
ifstream inputFile;
inputFile.open("testScores.txt");
for (i = 0; i < 20; i++)
inputFile >> students->testScore;
cout << "The test scores entered are: ";
for (i = 0; i < 20; i++)
cout << " " << students->testScore;
return 0;
}
【问题讨论】:
-
我还添加了一个参考文件“testScore.txt”。当我输出时,我从文本文件中获取第一个值 20 次。这是取 testScore.txt 的第一个值并存储到 studentType 的所有 testScore 成员中吗?
-
您是否在调试器中逐步完成了此操作?这里的实际问题是什么?它在哪一行表现出意外?
-
访问单个数组元素使用方括号
[]