简体   繁体   English

从文件读取并保存到数组C ++

[英]Read from file and save into array c++

I'm doing this program that will read from a file and store whatever is on the file to an array and the it will print that like this: 我正在执行此程序,该程序将从文件读取并将文件中的任何内容存储到数组,并且它将打印如下内容:

Item number 986 has 8 items in stock 编号986的库存为8

Item number 432 has 24 items in stock 物料号432有24件库存

Item number 132 has 100 items in stock 物料号132有100个库存

Item number 123 has 89 items in stock 物料编号123库存89

Item number 329 has 50 items in stock 物料编号329有50件库存

Item number 503 has 30 items in stock 物料编号503有30件库存

Item number 783 has 78 items in stock 物料号783有78件库存

Item number 822 has 32 items in stock 物料号822有32件库存

Item number 233 has 56 items in stock 物料号233库存56

Item number 322 has 74 items in stock 物料号322有74件库存

I don't know why I am getting 0s for all values instead of those values above. 我不知道为什么对于所有值而不是上面的那些值都得到0。 Any ideas? 有任何想法吗?

#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");

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

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


   for (int pos = 0; pos < NUMOFPROD; pos++){
        infile >> products[pos].getId(id);
        infile >> products[pos].getAmount(total);
   } // 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

   for (int pos = 0; pos < NUMOFPROD; pos++){
       products[pos].display();
   }// 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

   return 0;

}

// Write the implementations for all the member functions of the class.
void Inventory::getId(int item){
    itemNumber = item;
}

void Inventory::getAmount(int num){
    numOfItem = num;
}

void Inventory::display(){
    cout << "Item number " << itemNumber << " has " << numOfItem << " items  in stock\n";
}

Instead of this: 代替这个:

infile >> products[pos].getId(id);
infile >> products[pos].getAmount(total);

I believe you want 我相信你想要

infile >> id;
products[pos].getId(id);
infile >> total;
products[pos].getAmount(total);

Good time to rename getId to setId by the way. 顺便说一句,将getId重命名为setId的好时机。

infile >> products[pos].getId(id);

This uses the confusingly named getId function to set the value to that of id , which is zero. 这使用名称混乱的getId函数将值设置为id的值,该值为零。 It then attempts to read into the return value of that function, which should cause a compile error since there is no return value. 然后,它尝试读入该函数的返回值,这将导致编译错误,因为没有返回值。 I've no idea why your compiler accepts that code. 我不知道为什么您的编译器会接受该代码。

To read and set the value, you want 要读取和设置值,您需要

infile >> id;
products[pos].getId(id);

You might consider renaming the function to something like setId , unless your goal is to confuse readers of the code. 您可能会考虑将函数重命名为类似setId ,除非您的目标是使代码的读者迷惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM