So i'm toying around making calculators

Well, downloaded Visual Studio Express and started toying around making calculators, such as this simple one, that calculates the area of a circle after having the user declare it's radius.

(The following code is not part of the problem, it's just a hint to how simple my coding is. They could've teached this in Elementary school, even in some countries they do.)

#include<iostream>
using namespace std;

int main(void)
{

double dRadius = 0.0;
double dPi = 3.14159265;

cout << "Please enter the radius of the circle." << endl;
cin >> dRadius;

double dArea = (dRadius*dRadius / dPi);

cout << "The area of your the circle is " << dArea << endl;

system("pause");
return 0;

}

Simple right?
Well i have a slight issue with how i have written the calculations
in this calculator. This one functions by prompting the user for a cylinders radius and height, and then calculates the volume of the cylinder.

dVolume = ((pow(dRadius,2) / dPi) * dHeight);

Using this equation, the program starts and is functional, however dVolume equals 1.#INF, meaning it's infinite (i beleive).

This is the line with the calculation in it, attempting to use the cmath or math.h librarys power function (i.e Making dRadius sqared).
Using this equation, the program starts and is functional, however dVolume equals 1.#INF, meaning it's infinite (i beleive).


For those interested in the entire code, or in case it can be used to more thouroughly solve my problem, here's the rest of it.

#include<iostream>
#include<math.h>
#include<cmath>

using namespace std;

int main(void)
{

double dRadius = 0.0;
double dHeight = 0.0;
double dVolume = 0.0;
double dPi = 0.0;

cout << "Please enter the radius of the top of the sylinder." << endl;
cin >> dRadius;
cout << "Please enter the height of your cylinder." << endl;
cin >> dHeight;

dVolume = ((pow(dRadius,2) / dPi) * dHeight);

cout << "The Volume of your cylinder is " << dVolume << endl;

system("pause");
return 0;

}
Last edited on
Well, this is wrong for a start - double dArea = (dRadius*dRadius / dPi);
The area of a circle is radius*radius*pi.

which would make the volume radius*radius*pi*height

Another thing:
1
2
3
4
double dRadius = 0.0;
double dHeight = 0.0;
double dVolume = 0.0;
double dPi = 0.0; //error  this should NOT be zero. 
dPi = 0.0;

dVolume = ((pow(dRadius,2) / dPi) * dHeight);

Division by zero on purpose....
Well there's the problem. Final code is functional for those who want it.

#include<iostream>
#include<math.h>
#include<cmath>

using namespace std;

int main(void)
{

double dRadius = 0.0;
double dHeight = 0.0;
double dVolume = 0.0;
double dPi = 3.14;

cout << "Please enter the radius of the top of the sylinder." << endl;
cin >> dRadius;
cout << "Please enter the height of your cylinder." << endl;
cin >> dHeight;

dVolume = ((pow(dRadius,2) * dPi) * dHeight);

cout << "The Volume of your cylinder is " << dVolume << endl;

system("pause");
return 0;

}
Topic archived. No new replies allowed.