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  (#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#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.