【发布时间】:2016-07-01 18:53:46
【问题描述】:
我是 C++ 新手,正在努力学习,所以请原谅我犯的任何错误。我有一个文件,其中包含以下格式的数据,
"String" 、 "String" 、 Character 和 Number 100 个条目。 即“比利乔尔 A 96 蒂姆麦肯 B 70”。
我想将这些条目存储在一个类数组中(也许我的意思是实例或对象,我是新手,所以不确定)。
这是我的错误尝试: 它没有得到下一组学生信息的原因是......我将如何想出一个循环来处理这个问题?所以我可以得到所有学生的名字吗?必须远离不做100个变量来输入文件。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Student{
private:
int grade;
char grade_letter;
public:
struct Student_info(){
void set_firstname();
void set_lastname();
string get_firstname();
string get_lastname();
};
}myStudent_info;
/// Set / get code below but left out.
int main()
{
Student myStudent[100];
ifstream myfile("input.txt");
if (myfile.is_open())
{
string a, b;
char c;
int d;
myfile >> a >> b >> c >> d;
for (int i = 0; i < 100; i++) {
myStudent[i].myStudentInfo.set_firstname(a);
myStudent[i].myStudentInfo.set_lastname(b);
/// the rest of variables...etc
}
myfile.close();
}
//Exit
cout << endl;
system("pause");
return 0;
}
【问题讨论】:
-
创建一个
Student myStudent[100]数组来存储信息。 -
myfile >> a >> b << c << d;我认为<<之前的c和d是一个错字。 -
Student myStudent;应该是Student myStudent[100]; -
您的文件是否总是正好有 100 名学生?
-
对不起,这两个都是错别字,我不知道一旦我发布了如何编辑这篇文章。
标签: c++ arrays class loops input