Question

I need help with my coding for C++! The goal of this is to calculate the...

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 <iostream>               // to be able to use cin and cout
#include <typeinfo>               // to be able to use operator typeid
#include <iomanip>               // 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
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

Explanation::

Code in C++ is given below

Note: Updated code is given by comments

Output is provided at the end

Code in C++:

#include <iostream> // to be able to use cin and cout

#include <typeinfo> // to be able to use operator typeid

#include <iomanip> // Include here all the other libraries that required for the program to compile

#include<cmath>

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.

  /**

  * Using floor of (total*10.0+0.5)/10.0 value we get the rounded value

  */

   cout << endl << "For a price $" << price << " and " << tax << "% tax, the total cost of the product is $" << setprecision(1)<<floor(total*10.0+0.5)/10.0 << 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

}

Output:

Enter the price and tax (%) please: 100 8.25

For a price $100.00 and 8.25% tax, the total cost of the product is $108.3

sh: 1: pause: not found

Testing your solution

test(fabs(total - 108.30) < 0.001) failed in file main.cpp, line 42.

sh: 1: pause: not found

Please provide the feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
I need help with my coding for C++! The goal of this is to calculate the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi guys! I need help for the Data Structure class i need to provide implementation of...

    Hi guys! I need help for the Data Structure class i need to provide implementation of the following methods: Destructor Add Subtract Multiply Derive (extra credit ) Evaluate (extra credit ) ------------------------------------------------------- This is Main file cpp file #include "polynomial.h" #include <iostream> #include <sstream> using std::cout; using std::cin; using std::endl; using std::stringstream; int main(int argc, char* argv[]){    stringstream buffer1;    buffer1.str(        "3 -1 2 0 -2.5"    );    Polynomial p(3);    p.Read(buffer1);    cout << p.ToString()...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • please do the program in simple programming it is for my first c++ computer class i...

    please do the program in simple programming it is for my first c++ computer class i posted the example on pic 2,3 which is how this should be done Write a program that calculates and prints the bill for a cellular telephone company. The company offers two types of services: regular and premium. Its rates vary depending on the type of service. The rates are computed as follows: Regular service: $10.00 plus the first 50 minutes are free. Charges for...

  • I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...

    I need help with using the infile and outfile operations in C++... Here's my assignment requirements: Assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to read input from a file called Stock.txt: 1. The...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Hello i am having a bit of trouble with a verified exit for my program. For...

    Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() {    char chChoice3 = ' ';    do    {        cout << "\n\t--Menu--" << endl;        cout << "1) Enter Player Name" <<...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • Hey, i was just wondering how i would calculate over time pay in c++ visual studio...

    Hey, i was just wondering how i would calculate over time pay in c++ visual studio 2017? what i have right now is either returning 0 for some reason or being skipped over? im also wondering about the federal tax as well because that also doesn't seem to work, any help is greatly appreciated! these are my variables char chChoice = ' '; int intempID = 0; int intHours = 0; int intOThours = 0; float flOTrate = 0; float...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT