I have to write a program for a class in C++ and when I run the program, the calculations for the subtotal do not calculate correctly. Also the city does not display when I run the program. Please help! Below are the instructions and the written code.
1.Calculate the cost of the doflingies and the sales tax. Then calculate the shipping cost and total cost.
#include <iostream>
#include <string>
#include <iomanip> /* Necessary for setw */
usingnamespace std;
int main ()
{
string name, address, city, state, zipCode;
int doflingies;
/* Prompt user to enter shipping information */
cout << "Enter full name of the purchaser: ";
getline(cin, name); /* Necessary for correct output */
cout << "Enter the street address: ";
getline(cin, address);
cout << "Enter city: ";
//cin>>city;
getline(cin, city);
cout << "Enter state abbreviation: ";
cin >> state;
cout << "Enter zip code: ";
cin >> zipCode;
cout << "Enter the amount of doflingies ordered: ";
cin >> doflingies;
/* Display shipping information */
cout << "Ship to: " << " " << name << endl;
cout << right << setw(20) << address << endl;
cout << " "" "" " << " " << city << ",";
cout << state << " " << zipCode << endl;
cout << endl;
cout << "Number of Doflingies ordered: " << doflingies << endl;
cout << endl;
int huge, large, medium, small, remainder;
huge = 50;
large = 20;
medium = 5;
small = 1;
remainder = 0;
/*Mathematical calculations */
huge = doflingies/50; //why did you initialize "huge" with 50 when it was going to be overriden?
doflingies %= 50;
large = doflingies/20;
doflingies %= 20;
medium = doflingies/5;
doflingies %= 5;
small = doflingies/1;
doflingies %=1;
/*Display the output */
cout << left << setw(9) << "Countainer Size " << right << setw(20) << "Number required" << endl;
cout << "-------------" << " " << setw(20) << " -----------" << endl;
cout << left << setw(10) << "Huge"
<< right << setw(20) << huge << endl;
cout << left << setw(10) << "Large"
<< right << setw(20) << large << endl;
cout << left << setw(10) << "Medium"
<< right << setw(20) << medium << endl;
cout << left <<setw(10) << "Small"
<< right << setw(20) << small << endl;
cout << endl;
/*Assign doubles for cost */
constdouble huge_cost = 4.00;
constdouble large_cost = 2.00;
constdouble medium_cost = 0.75;
constdouble small_cost = 0.20;
double shippingSubTotal;
shippingSubTotal = (huge * huge_cost) + (large * large_cost) + (medium * medium_cost) + (small * small_cost);
cout << fixed << showpoint << setprecision(2) << endl;
/*Assign constant doubles for doflingy prices and quantity discounts.*/
constdouble priceOver500 = 15.95;
constdouble price250_499 = 17.50;
constdouble price100_249 = 18.95;
constdouble price1_99 = 19.95;
double doflingySubTotal = 0.0;
/*Calculations of Doflingies subtotal. */
if (doflingies >= 500)
doflingySubTotal = doflingies * priceOver500;
elseif (doflingies >= 250)
doflingySubTotal = doflingies * price250_499;
elseif(doflingies >= 100)
doflingySubTotal = doflingies * price100_249;
else
doflingySubTotal = doflingies * price1_99;
/*Assign a const double sales_tax at a value of 7.75% (0.0775) */
constdouble sales_tax = 0.0775;
double totalTax;
totalTax = (doflingySubTotal + shippingSubTotal) * sales_tax;
double totalcost;
totalcost = doflingySubTotal + shippingSubTotal + totalTax;
/*Display receipt information */
cout << "---------------------------------" << endl;
cout << "Doflingy total cost: " << setw(10) << "$" << doflingySubTotal << endl;
cout << "Sales Tax: " << setw(15) << "$" << totalTax << endl;
cout << "Shipping Cost: " << setw(10) << "$" << shippingSubTotal << endl;
cout << endl;
cout << "---------------------------------" << endl;
cout << "Total: " << setw(10) << "$" << totalcost << endl;
return 0;
}
The problem with the city has been commented out.
The output i received is the expected.
Number of Doflingies ordered: 100
Countainer Size Number required
------------- -----------
Huge 2
Large 0
Medium 0
Small 0
---------------------------------
Doflingy total cost: $0.00
Sales Tax: $0.62
Shipping Cost: $8.00
---------------------------------
Total: $8.62
In line 46, huge gets to 2.
In line 47, doflingies gets to 0. 49,51,53 no longer make sense.
Thank you for your help, I have solved the city problem, but when I calculate the subtotal, I receive an answer of 0. As I understand it, depending on how many doflingies a person orders, the subtotal is calculated through if/else statement, but the calculations are always 0.
Maybe you didn't see it in my post, and didn't understand the second part of shadowCODE's post either. In my post I said doflingies is less than 5. My mistake, it's actually 0.
Do you know what this part of your code does? doflingies %=1;
It is the same as saying doflingies = doflingies % 1;
In plain English, it means to set doflingies equal to the remainder if you divide doflingies by 1.
We all know that 1 divides evenly into any other integer, right? So what is the remainder? It is always 0.
Therefore, doflingies %=1; is the same as doflingies = 0;