簡體   English   中英

從文件讀取並保存到數組C ++

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

我正在執行此程序,該程序將從文件讀取並將文件中的任何內容存儲到數組,並且它將打印如下內容:

編號986的庫存為8

物料號432有24件庫存

物料號132有100個庫存

物料編號123庫存89

物料編號329有50件庫存

物料編號503有30件庫存

物料號783有78件庫存

物料號822有32件庫存

物料號233庫存56

物料號322有74件庫存

我不知道為什么對於所有值而不是上面的那些值都得到0。 有任何想法嗎?

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

代替這個:

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

我相信你想要

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

順便說一句,將getId重命名為setId的好時機。

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

這使用名稱混亂的getId函數將值設置為id的值,該值為零。 然后,它嘗試讀入該函數的返回值,這將導致編譯錯誤,因為沒有返回值。 我不知道為什么您的編譯器會接受該代碼。

要讀取和設置值,您需要

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

您可能會考慮將函數重命名為類似setId ,除非您的目標是使代碼的讀者迷惑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM