0

I am not sure how to input my data. Sorry I am a noob and the only examples I've found for something like this are way too complicated and I can't understand what is going on. I know this is not even close to being right but I have no idea how to set up the input from the file. My file data looks like this:

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

And my program so far looks like this:

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

1 Answer 1

1

to input and from a file, do this:

#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!
}

if you want to output to a file, do this:

#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!
}

you can also do both at the same time!

#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!
}

Just to let you know, fin and fout can be called whatever you want.

If this post answers your question, please mark it as the answer.

Thanks

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, thank you, but how would I input two numbers on the same line into an array of class objects like in the example?
fin >> num1 >> num2; array[ i ] .itemNumber = num1; array[ i ] .numOfItem = num2; in a for loop. Or even better, fin >> array[ i ] .itemNumber >> array[ i ] .numOfItem;
I would appreciate it if you marked my post as an answer
Oh I see, you will have to make some methods that can edit the private data, otherwise you will never be able to change the data
Ohhhh ok- I see what's going on with the num1 num2 example. Thank you!! Editing the private variables is no problem- just getting the info into the program! Thanks!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.