If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Having problems with objects and classes

hi, I'm having problems with the code below. Can someone help me? When I run the program, I am unable to get the output of GetMissile, GetLaser,.. Also, instead of displaying the correct 'prices' for, for example You.MissilePrice(), I get numbers that don't make sense, such as -858993460

Re: Having problems with objects and classes

Originally Posted by larry burns

hi, I'm having problems with the code below. Can someone help me? When I run the program, I am unable to get the output of GetMissile, GetLaser,.. Also, instead of displaying the correct 'prices' for, for example You.MissilePrice(), I get numbers that don't make sense, such as -858993460

Did you run the program using your debugger? If you did, then you would see the obvious mistake if you single-stepped through the code:

You make the same mistake in all of the other switch() statements. Do you see the problem? How does switch/case work in C++? Isn't there a "break" statement that goes along with all of this? Again, if you did single step through the program, you would see that the case statements just flow right into each other, which is not correct.

Second, the Price member is uninitialized when you create your objects. This means that it can have any random value on object creation. What if none of those case statements are executed? What happens to the Price member? It never gets set to anything. Initialize your member variables:

Now Price is set to 0 when an Aircraft is created instead of Price being any garbage value.

But the bottom line is that the debugger would reveal all of this to you. That's why it is imperative that you know how to use the debugger for your compiler suite, so that you don't need to ask questions and wait (either patiently or impatiently) for answers on a programming board.