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()
{
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
#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_statusreturn 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
constdouble tax_rate = 0.075 ;
constdouble tax_amount = purchase_amount * tax_rate ;
// 4. compute the total amount
constdouble 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.
}