Plz Help: Void Function / Array Trouble </3

I feel like this is a very simple fix...
But, every one of you know as well as I do..
After coding for so long, your brain gets worn out.

I'm so close, right?
Anybody mind giving a helping hand?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

struct productInfo
{
    string productName;
    string productGenre;
    float productPrice;
    string productPublisher;
    int productRating;
    int productList;
};

void showMenu();
void addProduct(productInfo productList[], int listSize);

int main()
{
    ifstream infile;
    ofstream outfile;
    
    int choice;
    int index;
    int i;
    
    string product;
    
    productInfo productList[?]; // Here is where I am having trouble...
    
    showMenu();
    
    cout << "Enter Number: ";
    cin >> choice;
    
    switch (choice)
    {
        case 1:
            addProduct(productList, ?); // "?" represents whatever I need to
            break;                      // enter for the "[?]" listed above... 
Last edited on
Now, I know a number needs to go where I am having the Error...

But, how do I properly use the number I put between the "[ # ]" ???
What does that value represent, and how do I call upon it?
Last edited on
I hope my attempt to share with you..
that I am understanding the problem, makes you ladies & gentlemen more willing to help.
Well you could read listSize and then do int productList[listSize] or just put productList[666] or any random number. If you cold show me your addProduct I could help you more so sorry if I don't understand what you actually need.
1> How many products do you expect to be added?
2> Is there a max size allowed to be added to the products array?

If you can answer number 1, then you can probably use that as your array size. If the answer of number 2 is yes, then you can guarantee an array size. You can create a const for your array size:

const size_t max_products = 20; //Or something like it.

Then when you use this in your code.

1
2
3
4
5
6
7
int main()
{
   const size_t max_products = 20;
   //...
   productInfo productList[max_products ]; //Using the constant
   //...
   addProduct(productList, max_products ); //Passing the constant 


If you cannot answer number 1, and there is no max size allowed, then you will need to use dynamic allocation and you will not be able to specify your size at compile time (which is required for arrays on the stack).
Last edited on
Topic archived. No new replies allowed.