How do i access a member in a structer that has an array?

[Im not sure how to access my members that have arrays. Ive tried for loops and every other way I know how but soon as i add the members with arrays to the function, it shows up blank
What am i doing wrong?

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

// Stuct of Store
struct Store
{
std::string name,
descripition;
double price;
std::string color[5];
int sizes[5];
};


int main()
{
Store Skates; // I have initialized each member
Skates.name = { "Luxe Skates" };
Skates.descripition = { " PVC Upper, heel and sole Metal Speed" };
Skates.price = 100.00;
Skates.color[0] = "Pink";
Skates.color[1] = "Purple";
Skates.color[2] = "Red";
Skates.color[3] = "Blue";
Skates.color[4] = "Green";

showProducts(Skates, Choice);// Function Call


}
// function to show first product chosen and choose a color and size- which are both arrays
void showProducts(Store s, int choice)
{
cout <<"You chose " << s.name << "! ";
cout <<"Price: $" << s.price;
cout << "Which color would you like? Pink, Purple, Red, Blue, Green? ";
for(int a = 0; a < 5; a++)
{
cin >> choice;
choice = s.color[a];
}



Last edited on
Please use code tags to format your code! (use "Edit" to edit your post)

You define a struct called Store. And, in your main() function, you create an instance of Store and assign values to its members. Fair enough! But I don't see where function showProducts() is called. Or what the type STodd, which goes into that function as a parameter, is supposed to be. Where does that value come from?

Last but not least: What is the exact error or problem you are facing?!
Last edited on
The problem i have have is i guess accessing the color and size arrays( I didn't add sizes here but its also an array with 5 elements). I ve initialized them. What I want to do is have the user pick which product they want ( using switch statements in main) and in main i wanna call each function but once i add the members "Sizes[5] and "colors[5] to the function with a for loop it does not show up in the show product function. Idk if my question is clear let me know!
Last edited on
Please use code tags to format your code! (use "Edit" to edit your post and put [code] and [/code] around your source code.)
Last edited on
Even after your edit, your code still is wrong/incomplete.

Why the struct is called Store, but the type of the parameter that goes into showProducts() is STodd, which was never declared anywhere, as far as I can tell? Similarly, where does the variable Choice come from? I see it being passed into showProducts(), but it was never declared or initialized before.


1
2
3
4
5
for(int a = 0; a < 5; a++)
{
    cin >> choice;
    choice = s.color[a];
}

This loop reads an integer from the stdin (e.g. keyboard) and stores it into choice, five times. But, after each input, it immediately overrides choice with the color at the index that equals the current loop counter a.

So, effectively (after all 5 loops), this loop ends up doing nothing, except for:
choice = s.color[4]

Is that really what you want?
Last edited on
Sorry i changed it here to make it easier to read but forgot to change it
Indeed, it is good to provide the shortest possible code that shows your problem.

But: That code still must be self-contained and consistent.
Last edited on
And no i dont want that.. So I initialized the color[5] with five diff colors. i want the user to be able to choose one of the 5 colors. Sorry i changed it here to make it easier to read but forgot to change it
Last edited on
He'll never learn to edit his OP at this rate.
Last edited on
Then maybe you want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int chooseColor(const Store &s)
{
    int choice;

    // Print all available colors
    for(int a = 0; a < 5; a++)
    {
        std::cout << a << ": " << s.color[a] << std::endl;
    }

    // Read and return user's choice
    std::cin >> choice;
    return choice;
}


...or, if you want to return the color as a string (rather than its index in the array):
1
2
3
4
5
6
7
const std::string &chooseColor(const Store &s)
{
    /* ... */

    std::cin >> choice;
    return s.color[choice];
}


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

BTW: Consider replacing...
1
2
3
4
5
struct Store
{
    /* ... */
    std::string color[5];
};

...with:
1
2
3
4
5
struct Store
{
    /* ... */
    std:vector<std::string> colors;
};


std::vector can store a variable number of elements. Also, you wouldn't have to assume that there are exactly 5 elements. You could call vector.size() to get the actual number of elements in the vector!

https://www.cplusplus.com/reference/vector/vector/
https://www.cplusplus.com/reference/vector/vector/operator[]/

1
2
3
4
5
// Print all available colors
for(size_t i = 0; i < s.color.size(); i++)
{
     std::cout << i << ": " << s.color[i] << std::endl;
}
Last edited on
Thanks !! Ill try that! I was thinking I would need a separate function for colors and sizes... Il be back if I have another question
Topic archived. No new replies allowed.