How would I pass an element in a struct array. I have to call the function:

new(Shoe[num], num);

where num is the number of elements in the database. I have declared the function as:

void new(Shoes Shoe[], num);

but I get the error:

cannot convert 'Shoes' to '*Shoes' for argument '1' to 'void new(Shoes*, num int&)'

Recommended Answers

All 10 Replies

Can you show more code, such as where you actually call the function? It looks like you're trying to pass a single Shoe object as the actual argument, rather than the array of Shoes

I call the function in a switch statement and the code inside the function is:

void New(Shoes Shoe[], int & num)
{
    cout << "Enter Name, Number and Date: ";
    cin >> Shoe[num].Name >> Shoe[num].Number >> Shoe[num].Size;
    num++;
}

How do you call it

New(Shoe[num], num);

You probably wany New(Shoe,num);

Yes this works but I have been asked as a task to do:

New(Shoe[num], num);

and I really don't understand this. So if anyone knows why I would be asked to do this to add a new entry to the end of the shoe struct array that would be helpful. I don't see why you can't pass the whole array and then increase num by one to add an entry to the end (this struct array is written to a .txt after)

Shoe[num]

Means you have an array of type Shoe and you're accessing the element from the array positioned at potition num. So, if you have only of element of type Shoe, and you try to access it in an array way, you'll most definitely receive an error.

Yes this works but I have been asked as a task to do:

That task just makes no sense.

To get this straight, you've been given this piece of code:

New(Shoe[num], num);

And you're supposed to make that work in a way that it adds a new element to your Shoe variable, which is an array of "Shoes" objects. (Note that this is already confusing)

In order to do that your function would need to have the following type:

void New (Shoes& Shoe, int& num)

Then you could use the implementation you had previously:

cout << "Enter Name, Number and Date: ";
cin >> Shoe[num].Name >> Shoe[num].Number >> Shoe[num].Size;
num++;

You would really have to watch out how you call the function though. if 'num' is equal to or larger than "Shoe"'s capacity you'll be writting on memory that's out of bounds.

If you're allowed to modify the function you should at least add another parameter that states the capacity of the buffer so you can compare that with 'num'.

Ok I'm not explaining things properly because I don't really understand.

I have a struct array:

struct Shoes {

    char  Name[20];  
    int Number;  
    double Shoesize;  

};

I have declared the database in int main() as:

Shoes Shoe[10];

and I need to call the function:

New(Shoe[num], num);

I would like or need to pass Shoe[num] where 'num' is the number of shoes in the database. In the 'New' function another element (another shoe containing name, number and shoe size) in the struct array should be added to the end or after position 'num'.

Ok so when I used:

void New(Shoes& Shoe, int& num)

I got the error for line 4:

Type 'Shoes' does not provide a subscript operator

void New(Shoes& Shoe, int& num)
{
    cout << "Enter Name, Number and Date: ";
    cin >> Shoe[num].Name >> Shoe[num].Number >> Shoe[num].Size;
    num++;
}

Is this because I am overloading the subscript operator? since if num is originally 7 and I use num++ then:

Shoe[7] = 8

Or am I talking rubbish? How can I fix this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.