Question: I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products

I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products price and the amount of tax, then display it to the user. The numbers we have to test out is 100 for the price and 8.25 for the percentage. When I test is out, the formula works but it isn't rounding the 108.25 to 108.3, which is the total price we are supposed to display to the user after it has been rounded. I tried rounding is it and I have no idea why it isn't working when it seems to be correct. Please help me figure it out. This is what I have so far:

#include // to be able to use cin and cout #include // to be able to use operator typeid #include // Include here all the other libraries that required for the program to compile

using namespace std;

// Ignore this; it's a little function used for making tests inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl << endl; } // This goes along with the above function...don't worry about it #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

int main() { //Declare variables named price, tax, and total that hold single precision real numbers.

float price, tax, taxro, total;

cout << "Enter the price and tax (%) please: "; //Prompt the user to "Enter the price and tax (%) please: ".

cin >> price; //Read the values from the keyboard and store them in price and tax respectively. cin >> tax;

total = price * (1.0 + tax / 100.0); //Calculate the total cost using the expression Price * (1 + Taxes/100) and assign the resulting value to total.

taxro = floor((tax * 100.0) + 0.5) / 100.0; // Round the value of total to ONE decimal digit and reassign the rounded value to total

cout << fixed << setprecision(2); //Format the output to display the values in fixed format with two decimal digits.

//Print a message like the one below: // // For a price $, P, and , X % tax, the total cost of the product is $, T // //where P, X, and T are the values corresponding to variables price, tax, and total respectively.

cout << endl << "For a price $" << price << " and " << tax << "% tax, the total cost of the product is $" << total << endl << endl;

system("pause"); // this is to stop the execution when the exe is used // for running the program

// Do NOT remove or modify the following statements cout << endl << "Testing your solution" << endl << endl; test(typeid(price) == typeid(float)); // Incorrect data type used for price test(typeid(tax) == typeid(float)); // Incorrect data type used for tax test(typeid(total) == typeid(float)); // Incorrect data type used for total if (price == 100.0 && tax == 8.25) // Incorrect rounding of total test(fabs(total - 108.30) < 0.001);

system("pause"); return 0; // successful termination }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!