Crazy values for discount

I have this function in a program that calculates the discount received by a customer for movie ticket and popcorn based on the amount bought.

It seems that I am lost as the Finalprice is 3.64756e-038.
I know it is something simple to do with the declaration of the values. Any tips will do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  int doubdisc(int numtickets, int numpopcorn){
     int disc;

    float priceTick=35*numtickets;
    float pricePop=20*numpopcorn;

if (numpopcorn=0){
const int disc=10;
}
else if (numpopcorn>0){
const int disc=20;}
float discountAmount =(pricePop+priceTick)*disc/100 ;
float finalPrice = (pricePop+priceTick) - discountAmount;

cout << "You owe R"<< finalPrice <<" and recieved a R" << discountAmount <<" discount. Thank you." <<endl;
}
On line 7 - you're assigning the value of 0 to numpopcorn - I think you wanted to compare if numpopcorn is equal to 0? You'd need to equality operator == not assignment =.

Also - lines 8 and 11 - you're creating a new constant integer variable disc here within the if/else block scope. I think you want to remove the const int on those lines, so that the code updates the disc variable that you declared on line 2. That way you'll have a valid value for disc at line 12. Is there any way a negative number could be entered for popcorn? Just wondering if you might want to check for negative values.

Topic archived. No new replies allowed.