Running into an issue trying to fill a vector of structs. I am completely stuck. Here the assignment question

Using the data file provided on the next page, write the C++ program to implement a vector of structs containing wines, vintage and scores. Create a file names wines.dat using the data on the next page. Load this data to your vector. Calculate the average score of the wines stored in the vector and output it. Then search the vector using a simple algorithm to return the location in the vector of the highest ranking Merlot to your main. Output the information at this location as shown below.

I am not looking for a complete solution as this is homework but what I am running into an issue with is filling the vector of structs with the input file. The input file looks like this

Peter Michael Chardonnay
97
2008
Revana Cabernet Sauvignon
97
2007
Altamura Cabernet Sauvignon
96
2007

Any help would be greatly appreciated.

Do you know how to make a struct? Where have you actually got to? I'd anticipate you're aiming for something like this:

while (notFinishedReadingFile)
{  
  fileInput >> wineVector[x].name;
  fileInput >> wineVector[x].score;
  fileInput >> wineVector[x].year;
  ++x;
}

Edited 5 Years Ago by Moschops: n/a

Yes. The problem I am running into is how to declare the vector relating to the struct. For instance lets say this is my struct;

struct wineTypes
{
    string wineName;
    int vintage;
    int score;
};

How would I take an input file like this:

Peter Michael Chardonnay 
97 
2008 
Revana Cabernet Sauvignon 
97
2007 
Altamura Cabernet Sauvignon 
96 
2007 

And put the name in
wineName

put the vintage in
vintage

and put the score in
score

Edited 3 Years Ago by Nick Evan: Fixed formatting

The code that you wrote is almost the exact code that I have been trying and for some reason it is not working properly. Heres the code I have, please tell me what I am doing wrong.

struct wineTypes
{
    string wineName;
    int vintage;
    int score;
};

void fillVector (vector<wineTypes>&, ifstream& inFile);

int main()
{
    ifstream inFile;
    vector<wineTypes> wines;
    string inputFile;




    cout << "Enter the name of the input text fil:" << endl;
    cin >> inputFile;
    cout << endl;
    inFile.open(inputFile.c_str());

    fillVector (wines, inFile);



    return 0;
}

void loadVector (vector<wineTypes>& wines, ifstream& inFile)
{
    int counter = 0;

    while (inFile)
    {
        getline(inFile, wineTypes[counter].wineName);
        inFile >> wineTypes[counter].score;
        inFile >> wineTypes[counter].vintage;
        counter++;
    }

    inFile.close();
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Please use [ CODE ] tags when you include code in your posts (which you should almost always be doing when you ask a new question). Select the code that you've pasted in, then click the very-clearly-marked "[ CODE ]" icon above the editor.

Since you haven't told us what your program -is- doing, it's hard to suggest what you should fix. Though there have been so many posts on almost identical issues dealing with ifstream input, I'll go out on a limb and suggest that you add the line inFile.ignore(); after you read the year and before you increment "counter" inside your while loop in fillVector().

This article has been dead for over six months. Start a new discussion instead.