我是C++的新手,试着学会如此宽恕我犯的错误。我有一个文件,其中包含以下格式的数据:如何从文件循环输入并存储到数组变量中?
“String”,“String”,带有100个条目的字符和数字。 即“比利乔尔A 96蒂姆麦肯B70”。
我想将这些条目存储在一个类的数组中(也许我的意思是实例或对象,我对此如此不清楚)。
这是我的不良尝试: 它不好的原因它没有得到下一组学生信息......我怎么会想出一个循环来处理这个问题?所以我可以得到所有学生的名字?必须不要让100个变量输入事物infile。
#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]'数组来存储信息。 – user1336087
'myfile >> a >> b << c << d; ''我认为'''在'c'和'd'之前是一个拼写错误。 – drescherjm
'学生myStudent;'应该是'学生myStudent [100];' – drescherjm