【问题标题】:How to loop input from file and store into array variables?如何循环输入文件并存储到数组变量中?
【发布时间】: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 &gt;&gt; a &gt;&gt; b &lt;&lt; c &lt;&lt; d; 我认为&lt;&lt; 之前的cd 是一个错字。
  • Student myStudent; 应该是Student myStudent[100];
  • 您的文件是否总是正好有 100 名学生?
  • 对不起,这两个都是错别字,我不知道一旦我发布了如何编辑这篇文章。

标签: c++ arrays class loops input


【解决方案1】:

您只输入 1 个学生的数据,然后循环 100 次。如果您想输入 100 个学生的数据并存储每个学生的数据,那么您应该这样做

for (int i = 0; i < 100; i++) {
    myfile >> a >> b >> c >> d;
    myStudent[i].myStudentInfo.set_firstname(a);
    myStudent[i].myStudentInfo.set_lastname(b);
    /// the rest of variables...etc                     
}

而不是

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                     
}

【讨论】:

  • 也许我搞砸了,但我试图这样做。它在文本文件中为所有 100 个学生分配了相同的学生姓名与不同的学生姓名。
  • Student myStudent[100]; 替换为@drescherjm 所说的Student myStudent;
【解决方案2】:

你在正确的轨道上继承我的想法我会做什么。但重点是你需要在 main 中创建一个向量或学生数组。

class Student{
private:
  int grade;
  char grade_letter;
  string firstname;
  string lastname;
public: 
      Studnet();
      void set_firstname(string x);
      void set_lastname(string x); 
      void set_letter(char x); 
      void set_grade(int x);          
};


int main() { 
    Student x;
    std::vector<Student> list(100);

    sting input;

    ifstream myfile("input.txt");
    if (myfile.is_open()) {
        while( getline(myfile, input)) {

            // divide input variable into parts
            // use set functions to set student x's values
            // push student x into vector of students list using "list.push_back(x);"

        }
        myfile.close();
    }
    return 0;
}

【讨论】:

  • 样本输入数据没有换行符。
猜你喜欢
  • 2017-07-05
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 2012-11-16
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
相关资源
最近更新 更多