C++ beginner help

For some reason the code is giving me an output for the discountpercentAwarded is 432120. Im trying to get the output to be according to my choice of books purchased.

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

 #include<iostream>

int main()
{
int amountBooks;
int storeCost = 39.95;
int discountpercentAwarded;

std::cout << "Enter The Number Of Books Purchased: ";
std::cin >> amountBooks;
std::cout << "Name: Advanced C++ Programming by Gaddis\n";

if(amountBooks == 0)
{
std::cout << "Discount Percent Awarded: " << discountpercentAwarded <<std::endl;
discountpercentAwarded = 0.00;
}
if(amountBooks <= 1)
{
std::cout << "Discount Percent Awarded:" << discountpercentAwarded <<std::endl;
discountpercentAwarded = 0.02;
}
if(amountBooks == 2)
{
std::cout << "Discount Percent Awarded: " << discountpercentAwarded <<std::endl;
discountpercentAwarded = 0.04;
}
if(amountBooks == 3)
{
std::cout << "Discount Percent Awarded: " << discountpercentAwarded <<std::endl;
discountpercentAwarded = 0.06;
}
if(amountBooks >= 4)
{
std::cout << "Discount Percent Awarded:"  << discountpercentAwarded <<std::endl;
discountpercentAwarded = 0.10;
}
else
{
if(amountBooks < 0)
std::cout << "Number of books cannot be negative!! \n\n";

std::cout << "Store Cost: " << storeCost <<std::endl;



}
} 
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
#include <iostream>
#include <iomanip>

int main()
{
    // use double to represent numbers with a fractional part
    /*int*/ const double storeCost = 39.95; 


    std::cout << "Name: Advanced C++ Programming by Gaddis\n"
              << "cost per book is " << storeCost << '\n'
              << "Enter The Number Of Books Purchased: ";
    int amountBooks;
    std::cin >> amountBooks;

    if(amountBooks < 0)
    {
         std::cout << "Number of books cannot be negative!! \n\n";
         return 1 ; // exit from program without doing anything else
    }

    /*int*/ double discountpercentAwarded  ;
    if(amountBooks == 0) discountpercentAwarded = 0.00 ;
    else if(amountBooks == 1) discountpercentAwarded = 0.02;
    else if(amountBooks == 2) discountpercentAwarded = 0.04;
    else if(amountBooks == 3) discountpercentAwarded = 0.06;
    else discountpercentAwarded = 0.10; // amountBooks >= 4

    std::cout << std::fixed << std::setprecision(2) // print two digits after the decimal point
              << "Discount Percent Awarded: " << discountpercentAwarded << '\n' ;

    const double gross_cost = amountBooks * storeCost ;
    std::cout << "gross cost: " << amountBooks << " books @ " << storeCost
              << " == " << gross_cost << '\n' ;

    const double discount_amount = gross_cost * discountpercentAwarded ;
    std::cout << "discount amount: " << discount_amount << '\n' ;

    const double net_cost = gross_cost - discount_amount ;
    std::cout << "net cost: " << net_cost << '\n' ;
}
Topic archived. No new replies allowed.