【问题标题】:input from a file into an array of class objects c++从文件输入到类对象数组c ++
【发布时间】:2017-09-08 23:54:32
【问题描述】:

我不确定如何输入我的数据。对不起,我是一个菜鸟,我发现的唯一这样的例子太复杂了,我不明白发生了什么。我知道这甚至不是正确的,但我不知道如何设置文件的输入。我的文件数据如下所示:

986  8
432  24
132  100
123  89
329  50
503  30
783  78
822  32
233  56
322  74

到目前为止,我的程序如下所示:

#include <iostream>
#include <fstream>
using namespace std;


// This program defines a class called Inventory that has itemnumber (which 
// contains the id number of a product) and numofitem (which contains the 
// quantity on hand of the corresponding product)as private data members.
// The program will read these values from a file and store them in an 
// array of objects (of type Inventory).  It will then print these values
// to the screen.

// Example: Given the following data file:
//     986 8
//     432 24
// This program reads these values into an array of objects and prints the
// following:
//     Item number 986 has 8 items in stock
//     Item number 432 has 24 items in stock


const int NUMOFPROD = 10;   // This holds the number of products a store sells

class Inventory
{
public:

   void getId(int item);      // This puts item in the private data member 
                              // itemnumber of the object that calls it.
   void getAmount(int num);   // This puts num in the private data member
                              // numofitem of the object that calls it.
   void display();            // This prints to the screen 
                              // the value of itemnumber and numofitem of the 
                              // object that calls it.



private:

   int  itemNumber;         // This is an id number of the product
   int  numOfItem;          // This is the number of items in stock 

};


int main()
{

   ifstream infile;       // Input file to read values into array
   infile.open("inventory.dat");

   // Fill in the code that declares an array of objects of class Inventory
   // called products. The array should be of size NUMOFPROD
   Inventory products[NUMOFPROD];

   int pos;                   // loop counter
   int id;                    // variable holding the id number
   int total;                 // variable holding the total for each id number

   // Fill in the code that will read inventory numbers and number of items  
   // from a file into the array of objects. There should be calls to both  
   // getId and getAmount member functions somewhere in this code.
   // Example: products[pos].getId(id); will be somewhere in this code
   pos = 0;
   while(NUMOFPROD > pos++ && infile >> products[pos])
   {
        id = products[pos];
        products[pos].getId(id);


       //products[pos].getAmount(total);
   }
    infile.close();
   // Fill in the code to print out the values (itemNumber and numOfItem) for 
   // each object in the array products.
   // This should be done by calling the member function display within a loop
   pos = 0;
   while(NUMOFPROD > pos++)
   {
       products[pos].display();
   }

   return 0;

}


// Write the implementations for all the member functions of the class.

void getId(int item)
{
    itemNumber = item;

}   
void getAmount(int num)
{
    numOfItem = num;
}
void display()
{
    cout << itemNumber << " ";
}

【问题讨论】:

标签: c++ arrays class object


【解决方案1】:

要输入和从文件中输入,请执行以下操作:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream fin("YourTxtFile.txt");
    fin >> something //Fin acts just like your standard cin

    fin.close() //Don't forget to close the stream when you're done!
}

如果要输出到文件,请执行以下操作:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ofstream fout("YourTxtFile.txt");
    fout >> something //Fout acts just like your standard cout

    fout.close() //Don't forget to close the stream when you're done!
}

你也可以同时做这两个!

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream fin("YourTxtFile1.txt");
    fin >> something //Fin acts just like your standard cin

    fin.close() //Don't forget to close the stream when you're done!

    ofstream fout("YourTxtFile2.txt");
    fout >> something //Fout acts just like your standard cout

    fout.close() //Don't forget to close the stream when you're done!
}

只是为了让你知道,fin 和 fout 可以随心所欲地调用。

如果这篇文章回答了您的问题,请将其标记为答案。

谢谢

【讨论】:

  • 好的,谢谢,但是我如何将同一行上的两个数字输入到示例中的类对象数组中?
  • fin >> num1 >> num2;数组[ i ] .itemNumber = num1;数组[ i ] .numOfItem = num2;在一个 for 循环中。或者更好的是 fin >> array[ i ] .itemNumber >> array[ i ] .numOfItem;
  • 如果您将我的帖子标记为答案,我将不胜感激
  • 哦,我明白了,你必须做一些可以编辑私有数据的方法,否则你将永远无法更改数据
  • Ohhhh 好的 - 我看到 num1 num2 示例发生了什么。谢谢!!编辑私有变量没有问题 - 只需将信息输入程序!谢谢!!
猜你喜欢
  • 1970-01-01
  • 2010-09-21
  • 2015-01-25
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
相关资源
最近更新 更多