0

I need to put in an array some data from show.

I have this function:

addShow(string name, string theatre, string day, string hour, int totalTickets, float price)

I have a dinamic array which maximum length is the maximum shows that can be done.

arrayShows = new int[totalShows];

Now, I need to put in this array the data of each show, how can I do it?

I thought something like this:

count = 0;
arrayShows[count]= name;
arrayShows[count+1]= theatre;
arrayShows[count+2]= day;
arrayShows[count+3]= hour;
arrayShows[count+4]= totalTickets;
arrayShows[count+5]= price;

count += 6;

But as you can see if the maximum shows is 5, this is not correct because I can only put some data from the first show, but the other 4 shows won't be stored.

Could I do a bidimensional dinamic array to do solve this problem?

3
  • 3
    You probably want an array of structures or several arrays. Commented Nov 18, 2017 at 16:58
  • Change string name to const string& name to avoid copying the strings Commented Nov 18, 2017 at 16:59
  • Don't use a floating-point number for the price. Store the cents as an int. Commented Nov 18, 2017 at 17:19

3 Answers 3

2

What you need is to create either structure or class which represents a show. It is not that difficult:

struct Show {
    Show() = default;

    Show(const std::string& name, const std::string& theater,
        const std::string& day, const std::string& hour,
        int totalTickets, float price)
        : name(name)
        , theater(theater)
        , day(day)
        , hour(hour)
        , totalTickets(totalTickets)
        , price(price)
    {}

    std::string name;
    std::string theater;
    std::string day;
    std::string hour;
    int totalTickets;
    float price;
};

Then, avoid using dynamic arrays and use std::vector instead:

std::vector<Show> shows(totalShows);
shows[0] = Show(name, theater, day, hour, totalTickets, price);

Or just add new elements on the fly:

shows.emplace_back(name, theater, day, hour, totalTickets, price);

There are, of course, other approaches like using std::tuple, but you do not need to make things more complicated then they are.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to store different types in array you can use C++17 std::any or std::variant. std::any:

std::vector<std::any> arrayShows;
arrayShows.push_back(name);
arrayShows.push_back(theatre);
arrayShows.push_back(day);
arrayShows.push_back(hour);
arrayShows.push_back(totalTickets);
arrayShows.push_back(price);

std::variant:

std::vector<std::variant<int, float, std::string>> arrayShows;
arrayShows.push_back(name);
arrayShows.push_back(theatre);
arrayShows.push_back(day);
arrayShows.push_back(hour);
arrayShows.push_back(totalTickets);
arrayShows.push_back(price);

Or if you cannot use C++17 you could implement your own solution using type erasure.

Comments

0

Create class for your data and then make array of the objects of class that will do

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.