Thread: Array of class objects

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Array of class objects

    I am trying to take input from a file and and insert them into an Array of class objects. I am having some trouble understanding this can anyone help.

    The text file contains a name, price, and quantity.

    Pleaes ignore the comments Ithose were just things I was playing around with.

    Essentially I am trying to modify my program so that the machine doesn't have built in data; it in fact will now have data programed into it via a file...

    Code:
    #include <iostream>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdio.h>
    
    #include "CashRegister.h"
    #include "dispenserType.h"
    
    using namespace std;
    
    //void showSelection(string, int);
    void showSelection();
    void sellProduct(dispenserType& product, cashRegister& pCounter);
    
    //dispenserType productlist[8];
    //struct productsinfo
    //{
    //	string name;
    //	int noi;
    //	int cost;
    //}
    
    int main()
    {
    
    	ifstream inStud;
    	
    	inStud.open("items.txt");
    	if (!inStud)
    	{
    		cout <<"**  Can't open input file Stud**"<< endl;
    		return 1;
    	}
    
    
    	Get_Inf(inStud, StudDB[])
    
    
    	//dispenserType obj[8];
    
    	//ifstream inF1;
    
    	//int i = 0;
    
    	//while(!inF1.eof())
    	//{
    	//		inF1 >> obj[i].name;
    	//		intf1 >> obj[i].numberOfItems;
    	//		inf1 >> obj[i].cost;
    	//	    i++; 
    	//}
    
    
    
    	cashRegister counter;
    	dispenserType candy (100, 50);
    	dispenserType chips (100, 65);
    	dispenserType gum (75, 45);
    	dispenserType cookies (100, 85); 
    	
    	int choice;
    		
    	//cout << "*** Welcome to Shelly's Candy Shop ***" << endl;
    		//for (int a = 0; a < 8; a ++)
    		//{
    			//showSelection(obj[a].name, a);
    		//}
    		//cout << "Press 99 to exit" << endl;
    
    	//cin >> choice;
    
    	
    	showSelection();
    	cin >> choice;
    	
    	while (choice !=9)
    	{
    		switch (choice)
    		{
    		case 1:
    			sellProduct (candy, counter);
    			break;
    		case 2:
    			sellProduct (chips, counter);
    			break;
    		case 3:
    			sellProduct (gum, counter);
    			break;
    		case 4:
    			sellProduct (cookies, counter);
    			break;
    		default:
    			cout << "Invalid selection." << endl;
    		}
    		
    		//cout << "*** Welcome to Shelly's Candy Shop ***" << endl;
    		//for (int a = 0; a < 8; a ++)
    		//{
    		//	showSelection(obj[a].name, a);
    		//}
    		//cout << "Press 99 to exit" << endl;
    
    		//cin >> choice;
    
    		showSelection();
    		cin >> choice;
    	}
    
    	return 0;
    }
    
    //void showSelection(string whatever, int a)
    //{
    	
    //	cout << "Press", a+1, "for", whatever << endl;
    //}
    
    void showSelection()
    {
    	cout <<"*** Welcome to Shelly's candy Shop ***" << endl;
    	cout <<"To select an item, enter " << endl;
    	cout <<"1 for Candy" << endl;
    	cout <<"2 for Chips" << endl;
    	cout <<"3 for Gum" << endl;
    	cout <<"4 for Cookies" << endl;
    	cout <<"9 to exit" << endl;
    }
    
    void sellProduct(dispenserType& product, cashRegister& pCounter)
    {
    	int amount;
    	int amount2;
    
    	if (product.getNoOfItems() > 0)
    	{
    		cout << "Please deposit " << product.getCost()
    			 << " cents" << endl;
    		cin >> amount;
    
    		
    		while (amount  < product.getCost())
    		{
    			cout << "Please deposit another "
    				 << product.getCost() - amount
    				 << " cents" << endl;
    			cin >> amount2;
    			amount = amount + amount2;
    		}
    
    		if (amount >= product.getCost())
    		{
    			pCounter.acceptAmount(amount);
    			product.makeSale();
    			cout << "Collect your item at the bootom and "
    				 << "enjoy." << endl;
    		}
    		else
    			cout << "the amount is not enough. "
    				 << "enjoy." << endl;
    
    		cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
    			 << endl << endl;
    
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to pass an array, just pass the array by name, you don't need [] at the end.

    I would think you should be able to put things in a for loop:
    Code:
    for (int i = 0; i < SIZE_OF_ARRAY; i++) {
        cin >> list[i].first_thing >> list[i].second_thing >> list[i].third_thing;
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I need to insert this text file into an array of class objects.

    PotatoChip 50 75
    FreshGum 100 55
    Goldfish 30 55
    Snickers 40 75
    Pretzels 50 75
    MMChocolate 60 75
    AnimalCookies 70 55
    RiceKrispies 50 55
    LifeSavers 25 55

    name/quantity/price

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read into appropriate class members, or better yet, design a member function in the class that takes a reference to the iostream and reads the data into its own member variables and call that member function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    I have no idea what you're talking about lol

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by GCNDoug View Post
    I have no idea what you're talking about lol
    Remember those private members you have in the other thread? You're not going to be able to access them except in a member function. So you're going to have to write a member function (dispenserType::readItem, maybe) that has access to those private variables.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    class myclass
    {
    public:
    	void read(ifstream& readfrom)
    	{
    		readfrom >> myvar1;
    		readfrom >> myvar2;
    		// ...
    	}
    private:
    	// Variables here
    };
    
    int main()
    {
    	// ...
    	my_array_of_classes[i].read(my_opened_file);
    	// ...
    }
    Object oriented approach.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    hmmm ok let me give it a shot. So I need to read all the data in my header implementation file?
    Last edited by GCNDoug; 03-26-2008 at 01:37 PM.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Code:
    class dispenserType
    {
    	public:
    		int getNoOfItems() const;
    		int getCost() const;
    		void makeSale();
    		dispenserType(int setNoOfItems = 50, int setCost = 50);
    		void read(ifstream& readfrom)
    		{
    			readfrom >> name;
    			readfrom >> numberOfItems;
    			readfrom >> cost;
    		}
    	private:
    		string name;
    		int numberOfItems;
    		int cost;
    };
    like this?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Something like that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Elysia does this look like the right set up for main in order to send the opened file?
    Code:
            ifstream inStud;
    	dispenserType productlist[8];
    
    	
    	inStud.open("items.txt");
    	if (!inStud)
    	{
    		cout <<"**  Can't open input file items**"<< endl;
    		return 1;
    	}
    
    	for (int i = 0; i < 8; i ++)
    		productlist[i].read(items.txt);

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No. The member function "read" takes an ifstream object as reference, not a std::string or const char*.
    Not to mention items is undefined, and undefined members can't have members either.
    String liters must be surrounded by double quotes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    So then how do I send the opened file to the function?

    And once i send it will I be able to access all of those class objects anywhere?

    Change last two lines to this?
    Code:
    	for (int i = 0; i < 8; i ++)
    		productlist[i].read(inStud);

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GCNDoug View Post
    And once i send it will I be able to access all of those class objects anywhere?
    You don't. They're private.
    The essence of a class is to be an object. You know how to operate the class, but you don't know its internal workings. You tell the class to do something and it tells you something in return, but it does not allow you to manipulate its inner workings.

    See it as a car, or whatever. There are so many things in our technological world that works as objects.

    Change last two lines to this?
    Code:
    	for (int i = 0; i < 8; i ++)
    		productlist[i].read(inStud);
    Yes, because you are now passing the ifstream object itself, which is what the class wants.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Well now I have all the data, I just need to figure out now how to use it lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handling with Array filled with a class
    By MarlonDean in forum C++ Programming
    Replies: 18
    Last Post: 06-27-2008, 10:53 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM