Noob in need of help.

Hi I was wondering if someone could help me figure this out. I’m extremely new to coding. My first problem is my if . Else statement doesn’t seem to work also when computing my total amount the total tax does not register to be added in.

Please help. Thanks for those who viewed

#include <iostream>
#include <iomanip>
int main()
{

//Variables
float Purchase_Amount = 0.0;
float tax = 0.075;
float total_Tax = 0.0;
float Shipping = 0.00;
float Total_Amount = 0.0;

if (Purchase_Amount < 50.00)
Shipping = 0.00;
else (Purchase_Amount);
Shipping = 5.99;

std::cin>> Purchase_Amount;
std::cout<< "Purchase_Amount= "<< Purchase_Amount << " : ";
std::cout<< "total_Tax= "<< Purchase_Amount * tax << " : ";
std::cout<< "Shipping= "<< Shipping << " : ";
std::cout<< "Total_Amount= "<< Purchase_Amount + total_Tax + Shipping << " : ";



return 0;

}
@B12885

You also don't have a cin for the variable Purchase_Amount before giving Shipping its value, so Shipping will always be 0.0, what it was initialized at the start.

Move the line std::cin>> Purchase_Amount; to just above that if/else section
Last edited on
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
#include <iostream>
#include <cstdlib> // for EXIT_FAILURE
#include <iomanip> // std::setw, std::setprecision

int main()
{
    // 1. read in the purchase amount
    // favour using double as the default floating point type
    double purchase_amount = 0.0 ;
    std::cout << "purchase amount? " ;
    std::cin >> purchase_amount ;

    // it doesn't make sense if the purchase amount is non-positive
    if( purchase_amount <= 0.0 )
    {
        // ideally, output to std::cerr because this is an error message
        std::cout << "invalid purchase amount.\n" ;

        // we return EXIT_FAILURE to indicate a failed program status
        // https://en.cppreference.com/w/cpp/utility/program/EXIT_status
        return EXIT_FAILURE ;
    }

    // 2. compute the shipping cost
    // shipping cost is zero if purchase amount is less than 50, 5.99 otherwise
    double shipping_cost = 5.99 ;
    if( purchase_amount < 50.0 ) shipping_cost = 0.0 ;
    // note: we can also write this as:
    // const double shipping_cost = purchase_amount < 50.0 ? 0.00 : 5.99 ;
    // with the advantage that shipping_cost can be declared a constant;
    // it has a value that can't be (accidentally) changed after it is initialised.

    // 3. compute the tax amount
    const double tax_rate = 0.075 ;
    const double tax_amount = purchase_amount * tax_rate ;

    // 4. compute the total amount
    const double total_amount = purchase_amount + shipping_cost + tax_amount ;

    // print the results. we use std::setw() to align the output columns
    // https://en.cppreference.com/w/cpp/io/manip/setw
    std::cout << '\n' << std::fixed << std::setprecision(2) // print two digits after the decimal point
              << std::setw(20) << "purchase amount: " << std::setw(10) << purchase_amount << '\n' ;

    if( shipping_cost > 0.0 ) // print the shipping cost (only) if it is non-zero
        std::cout << std::setw(20) << "shipping cost: " << std::setw(10) << shipping_cost << '\n' ;

    std::cout << std::setw(20) << "tax: " << std::setw(10) << tax_amount << "\n\n"
              << std::setw(20) << "total amount: " << std::setw(10) << total_amount << '\n' ;

    // there is an implicit return 0 at the end of main
    // (if we reach the end of main without executing a return statement earlier)
    // returning zero indicates that the program execution was a success.
}
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
#include <iostream>
#include <iomanip>

int main()
{
    float Purchase_Amount = 0.0;
    float tax = 0.075;
    float total_Tax = 0.0;
    float Shipping = 0.00;
    float Total_Amount = 0.00;
    
    std::cout << "Enter purchase amount: $";
    std::cin>> Purchase_Amount;
    
    total_Tax = Purchase_Amount * tax;
    if (Purchase_Amount < 50.00)
    {
        Shipping = 0.00;
    }
    else
    {
        Shipping = 5.99;
    }
    
    Total_Amount = Purchase_Amount + total_Tax + Shipping;
    
    std::cout<< "Purchase_Amount: $" << Purchase_Amount << '\n';
    std::cout<< "      Total_Tax: $" << total_Tax << '\n';
    std::cout<< "       Shipping: $" << Shipping << '\n';
    std::cout<< "   Total amount: $" << Total_Amount << '\n';
    
    return 0;
}


@OP You were pretty close. Congratulations :) Now all you need do is adress the important layout and error checking shown in the previous.
Topic archived. No new replies allowed.