Problem with my program
Feb 9, 2014 at 7:49pm UTC
I recently made a program which calculates pi formulas and I'm having trouble calculating the volume of a cone, every time it receives input, it still print out a zero. Is there any way I could solve this?
And as a side question, is there anything I could do to make it faster?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
(#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int main();
void chooser();
const double pi = 3.14159;
void circleCircum()
{
char choose;
do
{
cout << "Enter radius" << endl;
int radius = 0;
cin >> radius;
int circum = pi * ( 2 * radius);
cout << "Circumference is: " << circum << endl;
cout << "Do you want to do this again? (y/n)" << endl;
cin >> choose;
}while (choose == 'y' );
chooser();
}
void surfaceCylinder()
{
char choose;
do
{
cout << "Enter radius" << endl;
int radius = 0;
cin >> radius;
cout << "Enter height" << endl;
int height = 0;
cin >> height;
int surface = (2 * pi * (radius * radius)) + (2 * pi * radius * height);
cout << "Surface Area is:" << surface << endl;
cout << "Do you want to do this again? (y/n)" << endl;
cin >> choose;
}while (choose == 'y' );
chooser();
}
void volumeCylinder()
{
char choose;
do
{
cout << "Enter radius" << endl;
int radius = 0;
cin >> radius;
cout << "Enter height" << endl;
int height = 0;
cin >> height;
int volume = pi * (radius * radius) * height;
cout << "Volume is: " << volume << endl;
cout << "Do you want to do this again? (y/n)" << endl;
cin >> choose;
}while (choose == 'y' );
chooser();
}
void surfaceCone()
{
char choose;
do
{
cout << "Enter radius" << endl;
int radius = 0;
cin >> radius;
cout << "Enter slope" << endl;
int slope = 0;
cin >> slope;
int surface = (pi * radius * slope) + (pi * (radius * radius));
cout << "Surface Area is:" << surface << endl;
cout << "Do you want to do this again? (y/n)" << endl;
cin >> choose;
}while (choose == 'y' );
chooser();
}
void volumeCone()
{
char choose;
do
{
cout << "Enter radius" << endl;
int radius = 0;
cin >> radius;
cout << "Enter height" << endl;
int height = 0;
cin >> height;
int volume = (1/3) * pi * (radius * radius) * height;
cout << "Volume is:" << volume << endl;
cout << "Do you want to do this again? (y/n)" << endl;
cin >> choose;
}while (choose == 'y' );
chooser();
}
void chooser()
{
cout << "Command characters: " << endl;
cout << "Circumference of a circle: c" << endl << "Surface Area of a cylinder: s" << endl << "Volume of a cylinder: v" << endl << "Surface Area of a cone: u" << endl;
cout << "Volume of a cone: o" << endl << "Exit program: x" << endl;
char choose;
cin >> choose;
switch (choose)
{
case 'c' :
circleCircum();
break ;
case 's' :
surfaceCylinder();
break ;
case 'v' :
volumeCylinder();
break ;
case 'u' :
surfaceCone();
break ;
case 'o' :
volumeCone();
break ;
case 'x' :
return ;
break ;
default :
cout << "You did not enter a valid charcter" << endl;
chooser();
break ;
}
}
int main()
{
cout << "Hello, this is the Pi equations calculator alpha 0.3" << endl;
chooser();
cout << "Bye" << endl;
return 0;
}
Feb 9, 2014 at 7:59pm UTC
The problem is that you are using ints, when you should use floats or doubles. For ints 1/3=0
Feb 9, 2014 at 9:46pm UTC
I tried that and it didn't work
Feb 11, 2014 at 6:49am UTC
Check if you get the same answer. Igore the static_cast, I just did that to suppress a warning. Note how I wrote 1/3 as 1./3., and all variables are doubles, not ints
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include<iostream>
using namespace std;
const double pi = 3.14159;
int main()
{
int volume,radius=1,height=1;
volume = static_cast <int >((1/3) * pi * (radius * radius) * height);
cout<<volume<<endl;
double dvolume,dradius=1.,dheight=1.;
dvolume = (1./3.) * pi * (dradius * dradius) * dheight;
cout<<dvolume<<endl;
}
Topic archived. No new replies allowed.