Part 1: The story of a man who is learning C++, and some headaches attached to it.
As part of the degree I'm aiming to get, I have to do programming courses for the credit, so I'd log some of the stuff I've worked on here. The first major assignment?
"You are selling jars of honey at a fruit stand. The cost of a jar is $5.40 per jar or 3 jars for $15.00. There is also a 10.0% tax added to the cost of the purchase. Write a C++ program that asks the user to input the number of jars of honey purchased and display to the monitor the cost of the jars, the tax, and the total amount due. The cost per jar, cost per 3 jars, and the tax should be constants. Remember for more than 3 jars, the customer will receive the 3 jar price for all multiples and single jar price for any remainder (use the / and % operators to determine this)."
Yeah, this had me stumped until someone told me to use a crapton of variables. Now I made something that works in the console in linux:
#include <iostream> using namespace std; int main() { int jars, sets_3, sets_1; const double priceSingle = 5.40; const double priceTriple = 15.00; const double taxRate = 0.10; // These constants are used. double cost1, cost2, subTotal, subTax, totalCost; /* These are doubles for decimal purposes */ cout<<"Enter the amount of jars you're buying:"<<endl; cin>>jars; // You input the amt of jars needed here sets_3=(jars/3); sets_1=(jars%3); cost1=(sets_3*priceTriple); cost2=(sets_1*priceSingle); subTotal=(cost1+cost2); subTax=(subTotal*taxRate); totalCost=(subTotal+subTax); // These are calculations. cout<<"The subtotal cost of the jars is:"<<subTotal<<endl; cout<<"The tax on the jars is:"<<subTax<<endl; cout<<"The total cost on the jars is:"<<totalCost<<endl; return 0; }
I think it's only gonna get more tricky from here on out. I had to tag a few comments in, so...
6 Comments
Recommended Comments