0

I have a school assignment in which I have to create a Wine Inventory System where the user can add multiple different wines without a finite number I assume.

I need to create vectors but I'm not sure. I don't know what to try.

#include <string>
#include <iostream>
#include <vector>
using namespace std;

struct Wine1
{   //struct for Wine pssibly needs Vector
    string name;
    string year;
    string place;
    string price;
} wine;
void printwine(Wine1 wine);


int main()
{
    string str; //input for data
    cout << "Please enter the data of the First wine: " << endl;
    cout << "Enter name: ";
    getline(cin, wine.name);
    cout << endl << "Enter year: ";
    getline(cin, wine.year);
    cout << endl << "enter country of creation: ";
    getline(cin, wine.place);
    cout << endl << "enter price: ";
    getline(cin, wine.price);
    cout << endl;

    cout << "your entered data: " << endl;
    printwine(wine);
    cout << endl;
    printwine2(wine2);
    cout << endl;
    printwine3(wine3);
}
void printwine(Wine1 wine)
{ //data the user typed as output
    cout << "Wine1" << endl;
    cout << "the name is: " << wine.name << endl;
    cout << "it's year is: " << wine.year << endl;;
    cout << "its country of creation is: " << wine.place << endl;;
    cout << "it's price is: " << wine.price << endl;
}

It should output the name of the wine, the year, it's country, and it's price, for each wine which was added.

JeJo
  • 30,635
  • 6
  • 49
  • 88

3 Answers3

2

A good starting should be using vector of Wine1.

std::vector<Wine1> wineVec;
wineVec.reserve(/*size*/) // reserve the memory if you know the number of wines beforehand

The printwine function should now take std::vector<Wine1>(preferably const-reference as the data is read-only) and iterate through the vector to print the attributes of the Wine1.

Something like:

#include <string>
#include <iostream>
#include <vector>

void printwine(const std::vector<Wine1>& vecWine)
{ 
    for (const auto& wine : vecWine)
    {
        // do printing each: wine.name, wine.year,... so on
    }
}


int main()
{
    std::vector<Wine1> vecWine;
    int wineNumber = 2;
    vecWine.reserve(wineNumber);

    std::string name, year, place, price;
    for (int i = 0; i < wineNumber; ++i)
    {
        // get the user input for name, year, place, and price
        std::cin >> name >> year >> place >> price;
        vecWine.emplace_back(Wine1{ name, year, place, price });
    }
    printwine(vecWine);
}

That said, you should read more about std::vector to get to know more, how it works.

Also, good to read about, how to overload operator>> and operator<<, so that you could even write code, much simpler.

Following is an incomplete code, which I leave you to complete after covering the topics which I mentioned.

void printwine(const std::vector<Wine1>& vecWine)
{
    for (const auto& wine : vecWine)
    {
        std::cout << wine << '\n'; 
    }
}

int main()
{
    std::vector<Wine1> vecWine(wineNumber);
    for (Wine1& wine : vecWine)
    {
        std::cin >> wine;
    }
    printwine(vecWine);
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
1

You probably want something like this:

#include <string>
#include <iostream>
#include <vector>
using namespace std;

struct Wine1
{   //struct for Wine pssibly needs Vector
    string name;
    string year;
    string place;
    string price;
};

void printwine(Wine1 wine);

int main()
{
    vector<Wine1> wineinventory;

    // read 3 wines

    for (int i = 3; i < 10; i++)
    {
      Wine1 wine;
      string str; //input for data
      cout << "Please enter the data of the First wine: " << endl;
      cout << "Enter name: ";
      getline(cin, wine.name);
      cout << endl << "Enter year: ";
      getline(cin, wine.year);
      cout << endl << "enter country of creation: ";
      getline(cin, wine.place);
      cout << endl << "enter price: ";
      getline(cin, wine.price);
      cout << endl;

      cout << "your entered data: " << endl;
      printwine(wine);

      wineinventory.push_back(wine);  // store in vectore
    }    

    // print all wines in the vector

    for (int i = 0; i < wineinventory.size(); i++)
    {
      cout << "Wine " << i << ":" endl;
      printwine(wineinventory[i]);
    }
}

Disclaimer: this is untested code, I'm not even sure if it compiles, but you should get the idea.

There is still much room for improvement.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 2
    A little typo: `printwine(wineinventory[i]);` I guess you first wanted to implement a `for range` ! – Damien Jul 18 '19 at 11:54
1

I believe there is a misunderstanding: you do not intend your struct Wine1 to contain a vector but instead, you want a vector of Wine1's.

I suggest a data structure similar to the following:

struct Wine {
   string name;
   string year;
   string place;
   string price;
};


void printwinelist(vector<Wine>& list){
    for(Wine& w : list){
        printwine(w);
    }
}
vector<Wine> winelist;

The main method has to be rewritten accordingly, to append additional objects to the vector.

While I could rewrite your code accordingly I suspect, that a better next step for you would be to read up on some of the concepts used, such as vectors.

Hagadol
  • 362
  • 1
  • 3
  • 16