Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a 2 program Write a program that formats product information entered by th

ID: 3930406 • Letter: W

Question

write a 2 program

Write a program that formats product information entered by the user and calculate the total amount of purchase.

Enter item number:583

Enter unit price:13.5

Enter quantity:2

Enter purchase date (mm/dd/yyyy):09/15/2016  

Item      Unit Price     QTY Purchase Date Total Amount

583      $    13.50     2    9/15/2016      $   27.00

The item number, quantity and date should be left justified; the unit price and total amount should be right justified. Hint: Use tabs to line up the columns.

Explanation / Answer

Calc.cpp

#include <iostream>

using namespace std;

int main()
{
int itemNo, quantity;
double price;
string date;
cout<<"Enter item number: ";
cin >> itemNo;
cout<<"Enter unit price: ";
cin >> price;
cout<<"Enter quantity: ";
cin >> quantity;
cout<<"Enter purchase date (mm/dd/yyyy): ";
cin >> date ;
double total = price * quantity;
cout<<"Item Unit Price QTY Purchas Date Total Amount"<<endl;
cout<<std::left<<itemNo<<" "<<"$"<<std::right <<price<<" "<<std::left<<quantity<<" "<<std::left<<date<<" "<<"$"<<std::right<<total<<endl;

return 0;
}

Output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                        

sh-4.3$ main                                                                                                                                                                                                                

Enter item number: 583                                                                                                                                                                                                      

Enter unit price: 13.5                                                                                                                                                                                                      

Enter quantity: 2                                                                                                                                                                                                           

Enter purchase date (mm/dd/yyyy): 09/15/2016                                                                                                                                                                                

Item    Unit Price      QTY     Purchas Date    Total Amount                                                                                                                                                                

583     $13.5   2       09/15/2016      $27