crash

1
2
3
4
5
6
7
8
void drugStore( long int& m , drugStoreItems *myDrugStore[2] ) {
           unsigned int select;
               std::cout << "\t\t\tWelcome to city's biggest drug store" << std::endl;
               std::cout << "Your money is: " << m << std::endl;
               for ( unsigned short int a = 1; a < 3; a++ )
                    std::cout << "[" << a << "]" << myDrugStore[a-1]->medicine << " = $" << myDrugStore[a-1]->price << " - " << myDrugStore[a-1]->stacks << " stacks left" << std::endl;
                    std::cin >> select;
}

i cannot figure why my program is crushing.
the output is
[1]Medicine 1 = $50 - 50 stacks left
[2] // it crashes here

it should be
[1]Medicine 1 = $50 - 50 stacks left
[2]Medicine 2 = $250 - 50 stacks left 
Last edited on
We need the crashing information. Does the window just close? Does it exit with a certain code? Any messages given?

The only other thing I may be able to offer is that it MAY have to do with the parameter you are putting into the function for drugStoreItems array pointer (obviously needing to make sure that you are passing a drug store with 2 items).

Also, something small, but if you just output a-1 to the display for what number you are on, it saves you from having to make 2 additional calculations for each loop (and looks a little cleaner), but to each his own.
it says the program has stopped working.

myDrugStore[a-1]->medicine when i change this code into myDrugStore[a-1].medicine it doesnt crush
Last edited on
I have had a similar issue before. -> is a member access operator, but it is used in place of a pointer member access operator or something like that (from what I remember from a text book). Just a period is basically for a member reference.

This page will explain it way better: http://stackoverflow.com/questions/11902791/what-is-the-difference-between-and-in-c

Do you think it may be that myDrugStore is a pointer, and not an array of pointers? Because then the -> SHOULD only work on the base element (being the address of the actual pointer defined?).
Last edited on
SHOULD only work on the base element

what do you mean?
Last edited on
Because if you declared a pointer to an array, you are basically pointing to the one address location where it begins, which is essentially the address of the first element in that array: the base address or base element.
oh i see.

printing the second element of the array , which dont have a pointer yet, using the dereference arrow is bad
Topic archived. No new replies allowed.