C++ . Simple fstream into an array of structs needed

Discussion in 'Programming' started by pictureboarduk, Mar 18, 2008.

  1. #1
    Hi,

    For the last couple days I've been struggling with reading a txt file into an array of structs, and despite asking people, am still stuck.

    I created the txt file with this code:-

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    const int MAXCHARS = 20;
    const int MAXITEMS = 10;
    
    int main()
    {
    	char ItemName[MAXITEMS][MAXCHARS] = { "Caviar", "Sprouts", "Salmon", "Eggs", "Truffles", 
    		"Quail", "Champagne", "Bread", "Brioche", "Apples"};
    	double Price [MAXITEMS] = {12.90, 0.80, 6.50, 0.75, 7.29, 5.55, 21.90, 0.80, 1.20, 1.10};
    	bool Luxury [MAXITEMS] = {false, false, true, false, true, true, false, false, true, false};
    
    
    	ofstream mystream("ShoppingList.txt");
    
    	if(!mystream)//if the above fails, cout the text
    	{
    		cout << "phail " << endl;
    	}
    
    	for(int i = 0;i<MAXITEMS;i++)
    	{
    		mystream << ItemName[i] << " " << Price[i] << " " << Luxury[i] << endl;
    	}
    
    		mystream.close();
    
    
    		system ("pause");
    
    		return 0;
    }
    Code (markup):
    Which produced:-

    [​IMG]

    What I'm wanting done is :-

    This was for a test I had last week, but am still not able to do it. the program MUST use SIMPLE code, no overloading, vectors or anything like that. For example this:-

    
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    struct items
    {
    	char itemname[10];
    	double price;
    	bool luxury;
    };
    
    int main()
    {
    	items structname;
    	char arrayname[10];
    	ifstream myfile("myfile2.txt");
    
    	if(!myfile)
    	{
    		cout << "phail " << endl;
    	}
    
    	myfile >> structname.itemname;
    	myfile >> structname.price;
    	myfile >> structname.luxury;
    
    
    	myfile.close();
    	system ("pause");
    	return 0;
    }
    
    Code (markup):
    is what I have tried, it works for one entering the first item into one struct, but I don't know how to create an array of structs for all the items, I hope this shows you how I want it done with just these 'things', like I say, nothing more like vectors, overloading etc.

    The reason I want this doing is so that I can use it as a template to learn from as I don't get the concept, and until I can see a working program, seems I still won't.

    All I can offer is $5 :eek: , so I'm looking for a charitable person.

    Many thanks.
     
    pictureboarduk, Mar 18, 2008 IP
  2. Bronto

    Bronto Peon

    Messages:
    308
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    Done.

    PM sent.
     
    Bronto, Mar 18, 2008 IP
  3. pictureboarduk

    pictureboarduk Well-Known Member

    Messages:
    551
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    140
    As Seller:
    100% - 9
    As Buyer:
    100% - 0
    #3
    Thanks, offer taken.
     
    pictureboarduk, Mar 18, 2008 IP
  4. Bronto

    Bronto Peon

    Messages:
    308
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    Payment received.

    Cheers!
     
    Bronto, Mar 18, 2008 IP