C++ Reading a txt file into an array of structs problem

Discussion in 'OT Technology' started by Swerve, Mar 18, 2008.

  1. Swerve

    Swerve OT Supporter

    Joined:
    Feb 20, 2005
    Messages:
    3,175
    Likes Received:
    3
    Hi!

    I've wrote a program which placed the contents of three array's into a txt file.

    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("myfile2.txt");//create and open a file called 'myfile'through a stream 'mystream'.
    
        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();//close the stream 'mystream' and it's associated file ('myfile').
    
    
            system ("pause");
    
            return 0;
    }
    
    The 3 arrays held:-

    char string (food's name)
    double (food's price)
    bool (is food's classed as 'luxury')

    (A total of 10 different items, image of the txt file is attached).

    Now the next step is reading the txt file into an array of structs, and this is where I'm having difficulty.

    What I have so far:-

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    const int MAXCHARS = 20;
    const int MAXITEMS = 10;
    
    struct mystruct
    {
        char itemname[MAXCHARS];
        double price;
        bool luxury;
    };
    
    int main()
    {
        mystruct mystructname;
    
        mystruct thestructsarrayname[MAXITEMS];
    
        ifstream mystream("myfile2.txt");
    
        if(!mystream)
        {
            cout << "phail " << endl;
        }
    
        for(int i = 1;i < 10;i++)
        {
            cout >> thestructsarrayname[i].price;
        }    
    
        mystream.close();
    
        system ("pause");
    
        return 0;
    }
    
    I've been trying to do this for two days solid now (seriously) and whilst I know it's easy to some people, I'm finding it hard to figure out as I've never done it before, and whenever I ask people for help they either say something I don't understand, or go wayy over my head saying use vectors, or overload this and that.

    If anyone could PLEASE just help me out with reading from the txt file so that the first food item goes into the first struct I would be EXTREMLY grateful. I appreciate just giving someone the answers isn't helpful, but being stuck with one issue for this long is not helpful either. I know my code may not be 'optimised', or have other issues I'm not aware of, but that's because I'm new to all this and trying to teach myself.

    Sorry for the long post, but I've become really frustrated with this, because I know that once I do have a working program, I'll be able to go through it slowly with the debugger step by step, and that way I'll get it into my head, it's just the way my brain works.

    Many many thanks to anyone who can help me with this.


    Swerve :)

    P.S. I've added a screen shot of the txt file, because I don't understand how it knows which bit to read.

    [​IMG]
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You have an ifstream that opens your file:

    ifstream mystream("myfile2.txt");

    but you never attempt to read data from it. Instead you attempt to read from cout:

    cout >> thestructsarrayname.price;
     
  3. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    The input stream reader reads sequentially along the input. Its stops when it encounters whitespace ( a tab, space, or newline). Skinjob is right, you need to use mystream >> <variable name> instead of cout >> <variable name>.

    But, you also need to deal with the other data items on each line of input. So, you'll want to do something like this.

    Code:
    for(int i = 1;i < 10;i++)
        {
            mystream >> thestructsarrayname[i].itemname >> thestructsarrayname[i].price >> thestructsarrayname[i].luxury;
        }    
    You have to deal with every element of the input file. The program doesn't inherently know that the second data item on each line is the price; instead, you have to write your input code to do something with every token returned from the stream extraction operator >>
     
  4. CodeX

    CodeX Guest

    ROFL at "cout >>", thats like the most geeky form of irony ever
     
  5. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    When you think about it, that's not much different than putting punctuation outside your quotations.
     
  6. CodeX

    CodeX Guest

    the comma belonged to the sentence outside of the quotation, and not to the contents of the quotation... I believe what I wrote is grammatically correct... please feel free to post official English language guidelines or some other gay source to prove me wrong
     
  7. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    http://owl.english.purdue.edu/handouts/grammar/g_quote.html
     
  8. CodeX

    CodeX Guest

  9. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Put commas and periods within closing quotation marks, except when a parenthetical reference follows the quotation.
     
  10. CodeX

    CodeX Guest

    Yeah, fine.

    You win and lose at the same time, congrats
     

Share This Page