d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help C++ > New To C++
Add Reply New Topic New Poll
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Jan 18 2013 01:59pm
I'm new to C++ and I've been assigned an assignment already and my bookstore has no stock of the book I need and won't till after the assignment.
Here is the assignment front and back that they assigned.
If you could show me step by step and explain to me what each part means and also sites that are helpful.
I use Microsoft Visual Studio 2010.
Thanks.

Front


Back
Member
Posts: 625
Joined: Nov 18 2009
Gold: 23.95
Jan 18 2013 04:48pm
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

#define A 144
#define T 1/3

int main ()
{
cout << "My name is Bob the Builder \\n"; //Shows up in console
cout << "This is my Patio Brick Program\n"; //Tells the program title
cout << "It will tell you the info for various test cases\n"; //Tells the program objective
int m; // One dimension of the patio
int n; // The other dimension of the patio
cout << "Please enter a patio's dimension: ";
cin >> m; //Ask the user for a number input
cout << "Please enter the other patio dimension: ";
cin >> n; //Asks the user for the other number
cout << "The Patio Area is " << m*n << "ft.\n"; //Finds the Area of the Patio and tells the user what it is
int a;
a = m*n;
int k;
k = (a*A)/32;
int j;
j = k*.1;
cout << "The # of bricks needed for the dimension is: " << (j+k) << ".\n";
cout << "The volume of of gravel needed is: " << a/T << ".\n";
int v;
v = a/T; //For Volume
int c;
c = 8.5; //For bucket
cout << "The number of scoops needed is: " << (v/c)+1 << ".\n"; //+1 is to round up
int g;
g = .45;
cout << "The total brick cost is: " << (j+k)*g << ".\n";
int s;
s = g*(j+k); //s is the total brick cost
int h;
h = 1+(v/c); //h is the number of scoops
int l;
l = 5.95;
cout << "The total gravel cost is: " << h*l << ".\n";
int w;
w = h*l; //w is total gravel cost
cout << "The total cost of everything is: " << w+s << ".\n";
return 0;
}


I get 0 as the total brick cost?
What can i do to clean this up?
I also need to know how to round a answer up and how to show a certain # of decimal places in the result.
Also how do i show squared ft or volume cubed after the result # in the cmd window?
I used examples online.
Member
Posts: 6,192
Joined: Dec 13 2010
Gold: 6,669.99
Jan 18 2013 06:47pm
Use floats or doubles instead of int of your variables. For example, int g = 0.45 will be equal to 0. If you place it as float or double, it will display / do the math correctly. I would suggest changing them all.

To clean it up, i highly suggest not using variables like a,b,c,d,f,g,etc... Use names like Volume, Cost, TotalCost. Names that we can clearly see what they are without comments.

As for the # of decimals, just add this to your code
cout.precision(#);
Just need to add it once at the start of your program.
Replace # by a number

This post was edited by ShadowFiend on Jan 18 2013 06:48pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Jan 18 2013 08:02pm
I'd suggest defining some more constants (using obvious names like shadowfiend suggested). Things like:
cost_of_brick
cost_of_sand
extra_margin_of_error (the +10% thing)

That way when the prices or policies of the company change, it will be easy to update.

This post was edited by Azrad on Jan 18 2013 08:02pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jan 18 2013 08:56pm
Quote (ShadowFiend @ Jan 18 2013 07:47pm)
Use floats or doubles instead of int of your variables. For example, int g = 0.45 will be equal to 0. If you place it as float or double, it will display / do the math correctly. I would suggest changing them all.

To clean it up, i highly suggest not using variables like a,b,c,d,f,g,etc... Use names like Volume, Cost, TotalCost. Names that we can clearly see what they are without comments.

As for the # of decimals, just add this to your code
cout.precision(#);
Just need to add it once at the start of your program.
Replace # by a number


don't you need to include iomanip for precision or is that something else?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll