if else help

I'm writing a program for school project where the input is
item1
10
item 2
20
do you have a club card (Y/N)
tax rate
8.25
base price 30.0

the problem I am having starts at line 45 when I start to implement the if else statement to calculate the discount (discount is buy 1 get half off lesser priced item)
. The math looks OK to me but for some reason it will print out 30.0 for price after discount when it should be 25. The code is below. Ant info on how i can make this work would really help.

#include<iostream>
using namespace std;

int main() {
int item1, item2, discounts, totalDiscount, salePrice;
double rate, basePrice, clubCard;
char y, n, Y, N;

y = 'Y';
n = 'N';
Y = 'y';
N = 'n';

cout << " Enter the price of the first item: ";
cin >> item1;
cout << " Enter price of second item: ";
cin >> item2;

cout << " Does customer have a club card? (y/n): ";

switch (y) {
case ('Y'):
Y = y;
cin >> y;
break;
case ('N'):
N = n;
cin >> n;
default:
break;
}

cout << " Enter tax rate, tax: ";
cin >> rate;

y = 1;
n = 0;
clubCard = 0.10;
discounts = 2;

basePrice = double(item1) + double(item2);
cout << " Base Price: ";
cin >> basePrice;


if (item1 < item2) {/*i have to fix the math the total is coming out to $10. supposed to be 25*/
(item1 / discounts);
salePrice = item1;
totalDiscount = item2 + salePrice;
}
else {
(item2 < item1);
(item2 / discounts);
salePrice = item2;
totalDiscount = item1 + salePrice;
}


cout << " Price after discount: " << totalDiscount << endl;

return 0;

}
use [ code ] ... [ /code ] tags (without spaces near the brackets). *really* helps.

Some tips:
1. Good variable names. It's not just a rate; it's a tax_rate. Boolean variables are usually named as is_something or has_something.
Stylistic notes: Typically in C/C++ we use underscores to separate words. thisStyle, starting with a lowercase letter, is a Java thing. Classes and methods are typically CamelCase.
2. Indentation! This will save you one day.
3. with <iomanip> , you can output floats/doubles with x decimal points as cout << fixed << setprecision(x);
4. Minimize number of variables. Total is a running total; just keep reusing it and showing calculations as you go. Too many variables will make things more complicated and will make it harder to maintain when you want to come back to your code to update it.
5. See below how to handle fall-through switch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>

using namespace std;

int main() 
{
    double item1, item2;
    double tax_rate;
    double total;
    char answer;
    bool has_club_card;  // If true, applies 50% off on lesser priced item
    
    cout << "Enter the price of the first item: $ ";
    cin >> item1;
    cout << "Enter price of second item: $ ";
    cin >> item2;
    
    cout << "Does customer have a club card? (y/n): ";
    cin >> answer;
    switch (answer) 
    {
        case 'Y':
        case 'y':
            has_club_card = true;
            break;
        case 'N':
        case 'n':
        default:
            has_club_card = false;
            break;
    }
    
    cout << "Enter tax rate (e.g. 8.25): ";
    cin >> tax_rate;

    cout << endl << endl;
    cout << fixed << setprecision(2);
    
    total = item1 + item2;
    cout << "Initial total: $" << total << endl;
    
    if (has_club_card)
    {
        if (item1 < item2)
            total -= item1 / 2.0;
        else
            total -= item2 / 2.0;
        cout << "Total after Club Discount: $" << total << endl;
    }
    total *= (1 + tax_rate/100.0);
    cout << "Total after " << tax_rate << "% tax: $" << total << endl;
    
    return 0;

}


Can test at https://repl.it/repls/RubberyFavorableDrawing

Example output:
Enter the price of the first item: $  10
Enter price of second item: $  20
Does customer have a club card? (y/n):  y
Enter tax rate (e.g. 8.25):  8.25


Initial total: $30.00
Total after Club Discount: $25.00
Total after 8.25% tax: $27.06


Note: in that example, the final total was 27.0625. In the real world, perhaps that total would be rounded up to $27.07 . Not sure if it's relevant to this exercise, though.
Last edited on
Topic archived. No new replies allowed.