Jump to content
  • entry
    1
  • comments
    6
  • views
    2986

Part 1: The story of a man who is learning C++, and some headaches attached to it.


Orpheus

755 views

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

I remember teaching myself C++.

Obviously you're going into a programming career, and I can tell you right now, C++ is going to help you a ton. :)

 

If you're not though, it's always a good thing to know, it makes you think differently about certain situations.

Link to comment

The great thing about c++ (and well the internet) is you have so many resources available to you. Everything you will encounter has probably already been programmed, so you can find great examples of how to do things already. Most of the programming you are doing is not "reinventing the wheel". So you shouldn't have too hard of a time finding help if needed.

Link to comment

I taught myself C++ in highschool, and it helped me ace a programming class in college. It's pretty fun, to me.

Link to comment

C++ is an awesome language to learn. You'll enjoy it once you get the syntax down so long as you can think through it logicially.

ie) I want it to do this, then this, then this.

 

PS. Use float for intergers with decimals as a double is just a bigger verison or a float and uses unnessary space allocations.

 

 

EDIT: If you ever have problems with a program feel free to pm me on here, maybe I can help you out.

Link to comment
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.