Jump to content
HybridZ

Any C++ Gurus?? Need some help...


Challenger

Recommended Posts

Im working on a program for C for engineers. Its not an assignment just trying to practice some and get used to variables, printing, taking user imput, doing calculations, etc. Im making a simple program to calculate what it costs to fill n tanks of radius r and height h with a certain material. Gravel, water, wheat or asphalt... all I could think of. :) I then have prices for each of these per unit of volume so it will show how much it costs to fill the tanks. But Im having some issues getting it to work.

 

-----------------------------

 

#include

 

int main()

 

{

double radius;

double height;

double quantity;

double area;

double volume;

int material;

double unit_cost;

double total_cost;

 

printf("nWhat is the radius of your storage tank?n");

 

scanf("%1f", &radius);

 

printf("What is the height of your storage tank?n");

 

scanf("%1f", &height);

 

printf("How many tanks would you like to build?n");

 

scanf("%1f", &quantity);

 

area = 3.14 * radius * radius;

 

volume = area * height * quantity;

 

printf("What would you like to store in these containers?n1) Graveln2) Watern3) Wheatn4) Asphaltn");

 

scanf("%d", &material);

 

if (1 == material)

 

{

unit_cost = 1.56;

 

}

 

if (2 == material)

 

{

unit_cost = .65;

 

}

 

if (3 == material)

 

{

unit_cost = 3.24;

 

}

 

if (4 == material)

 

{

unit_cost = 4.11;

 

}

 

 

total_cost = volume * unit_cost;

 

 

printf("nIt will cost you %f dollars to fill up all your tanks with the nmaterial you selected.", total_cost);

 

 

return 0;

 

}

 

 

--------------------------

 

Can anyone give me an tips on what to change or if there is anything really obvious? It seems to not even recognize values you put in for the radius and height... Thanks

 

BTW, Im using DevC++ as my compiler.

Link to comment
Share on other sites

Its been a while since I've delved into C, I use VB mostly. Try changing the f in this line scanf("%1f", &radius); to lf (for long float) and see if it works. I think a double is classified as a lf for some odd reason. If it works, you can change the rest of it similarly

Link to comment
Share on other sites

It is, indeed, related to the type specifier (%f for floats, %lf for doubles, %Lf if you want to get crazy and use long doubles) in your scanf calls. Different types, even of the same variety (e.g. floating point), can be stored in wildly different internal formats.

 

If you're really going to use C++, try using STL methods like cin and cout. They make things much simpler - not having to remember the format specifier for each type you want to use is incredibly helpful. The basic library is also usually , not (canonically in C++, FYI).

 

switch is also generally better than a sequence of if blocks, for exclusive selection off the same variable. There are some circumstances where nested if blocks are better for performance, but it can be rather tricky for generally minimal gain.

Link to comment
Share on other sites

Another question to ask is why you decided to use doubles instead of floats. The float type has more than enough precision and range to handle the values you're dealing with and uses less memory.

It's also just a style thing but it's much easier to read code where the variable being tested is on the left side of the == and the value on the right, like this (material == 2).

 

Hyuri is correct about it being easier to use the standard C++ IO library (once you figure out the syntax) and scanf is a function I avoid like the plague, it has all sorts of issues. I've been programming in C/C++ for over 25 years and haven't used it for the last 20 of them.

 

You can also calculate the square of a value using this symbol ^. Your area calculation statement then becomes area = 3.14 * radius^2;

Link to comment
Share on other sites

#include 
#include   // for decl of M_PI

using namespace std;

int main()
{

   double radius= 0.0;
   double height= 0.0;
   double quantity= 0.0;
   double area= 0.0;
   double volume= 0.0;
   int material= 0;
   double unit_cost= 0.0;
   double total_cost= 0.0;

   cout << endl << "What is the radius of your storage tank? ";
   cin >> radius;

   cout << endl << "What is the height of your storage tank? ";
   cin >> height;

   cout << endl << "How many tanks would you like to build? ";
   cin >> quantity;

   area = M_PI * pow(radius, 2);
   volume = area * height * quantity;

   cout << endl << "What would you like to store in these containers?n1) Graveln2) Watern3) Wheatn4) Asphaltn ? ";
   cin >> material;

   switch(material){

   case 1:
       unit_cost = 1.56;
       break;

   case 2:
       unit_cost = .65;
       break;

   case 3:
       unit_cost = 3.24;
       break;

   case 4:
       unit_cost = 4.11;
       break;

   }

   total_cost = volume * unit_cost;


   cout << endl << "It will cost you " << total_cost << " dollars to fill up all your tanks with the material you selected." << endl;

   return 0;

}

 

Also ^ is the bitwise xor operator, and does not do power-of operations.

 

Dave

Link to comment
Share on other sites

Ok I ended up figureing out the pow(radius,2) with the help of a friend at school (CS major).

 

It seems to work fine now for any value.

 

#include

#include

int main()

{

double radius;

double height;

int quantity;

double area;

double volume;

int material;

double unit_cost;

double total_cost;

 

printf("nWhat is the radius of your storage tank?n");

 

scanf("%lf", &radius);

 

printf("What is the height of your storage tank?n");

 

scanf("%lf", &height);

 

printf("How many tanks would you like to build?n");

 

scanf("%ld", &quantity);

area = 3.14 * pow(radius,2);

 

volume = area * height * quantity;

 

printf("What would you like to store in these tanks?n1) Graveln2) Watern3) Wheatn4) Asphaltn");

 

scanf("%d", &material);

 

if (1 == material)

 

{

unit_cost = 1.56;

 

}

 

if (2 == material)

 

{

unit_cost = .65;

 

}

 

if (3 == material)

 

{

unit_cost = 3.24;

 

}

 

if (4 == material)

 

{

unit_cost = 4.11;

 

}

 

 

total_cost = volume * unit_cost;

 

 

printf("nIt will cost you $%.2f to fill up all your tanks with the nmaterial you selected.n", total_cost);

 

 

 

 

 

 

return 0;

 

}

 

Link to comment
Share on other sites

Okay, if you're going to use , printf, and scanf, why not just go back to C? You're using a C++ compiler, but you're writing C code.

 

Also, using pow() to square adds the overhead of a function call (and the overhead of the math library!) without any real readability benefit. And M_PI, while common, is not a standard definition.

 

 

And heck, wheelman, most of my experience over the last few years has been C#!

Link to comment
Share on other sites

Hyuri,

 

I think any decent compiler will inline a single function call to a library, so there is no overhead. The readability benefit I agree is non-existent though. :)

 

Dave

Yeah, but how many ... other-than-decent compilers are still floating around, still being used, still being written? Besides, it's just a bad habit to get into, assuming that the compiler will fix any performance hits your coding might induce. Better to code for performance in the first place if possible.

Link to comment
Share on other sites

I'd hope noone is using other than decent compilers. I agree with you its better to code for performance, but performance isn't the only constraint as I'm sure you'll agree: Code maintainability, readability are also important. I don't think its incorrect to make code more readable if you know there is no performance hit with the compiler you're using.

 

I more put in the call to pow() as an alternative way to get the power-of (^) functionaly previously talked about.

 

Talking shop on hbz makes me feel wierd.

 

 

Dave

Link to comment
Share on other sites

Ive got another one for you guys.

 

Heres the code.

 

#include

#include

#include

int main()

{

double total;

double prevtotal;

int a;

int b;

int c;

int p;

 

 

a = 1;

b = 6;

p = 0;

c = rand();

 

srand(time(0));

 

total = prevtotal = 0;

 

while (a < 99999999 && b < 99999999)

{

total = c * pow( sin((a % B) * (a % B)), 2);

printf("%.0lf", total);

 

a = a + 1;

b = b + 1;

 

if (total < prevtotal)

//printf("nnnnnnn");

prevtotal = total;

}

 

 

 

return 0;

 

Dont mind the part concerning the total and prevtotal. We were just trying to find when it switched from positive to negative in an earlier version of the program.

 

Is there anyway to make this a screen saver? It goes on for quite a long time and if needed it could be looped so it repeats. Also with my green font in the command prompt it looks like the matrix! :mrgreen: I think this would be an awesome screen saver, and its easily modified so it only displays ones and zeros so it looks like binary code... All you have to do in this case is remove the C variable from the equation for the total.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...