Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello i've been trying to figure out this code. My instructor basically wants to use this operator to add to the data attribute "price" as long as it's over zero and if it is, it's acceptable to add but then if the price goes out of that range 50 and 1650, it should stay at it's original. Please help! :) This is what i've done till now, i can't figure out the rest.

C++
classdevice& classdevice::operator+(double add)
{
        double original;
        price = original;
	if(add>0)
	{
		price = price + add;
	}
	if(price < 50.0 && price > 1650.0)
        {
        price = original;
        }
}

But when i run it in my main, all the price's in the object array go to one value. What do i do? :/

What I have tried:

But when i run it in my main, all the price's in the object array go to one value. What do i do? :/
Posted
Updated 5-May-16 10:24am
v2

C++
if(price < 50.0 && price > 1650.0)

you are testing for price out of range but your test is wrong.
price can not be lower than 50 and higher than 1650 at the same time. You need to or operator.
 
Share this answer
 
Comments
Member 12505381 5-May-16 16:28pm    
THANKYOU, it worked, I was just working over the whole program for ages and i was tired, didn't realize that it needed the Or operator. Such a silly mistake. I appreciate it
It's pretty simple:
C#
double original;
price = original;

So what value do you set price to? :laugh:
What you wanted was:
C#
double original = price;

With that, it should work.
 
Share this answer
 
<br />
double original;<br />
price = original;<br />

Everything starts with original set to the default value of 0.0
Is add ever less than 0.0?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900