5 Replies - 1856 Views - Last Post: 31 August 2010 - 07:51 AM Rate Topic: -----

#1 brennydoogles  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 23-June 09

New to C++ having strange math issues

Posted 31 August 2010 - 06:54 AM

Hello all, I am taking a class which requires me to work in C++. Being fairly competent in Java, C#, and Python I am pretty familiar with programming techniques, but this particular problem has me stumped. The assignment calls for me to create objects for particular shapes, and then calculate the volume of these shapes and do some calculations with them. Two of the shapes are Cone and Cylinder, and their volumes can be calculated as:
Cylinder volume = PI*radius^2*Height
Cone volume = 1/3(PI*radius^2*Height)

I was having some trouble getting the calculations to work, and so in the name of troubleshooting I implemented the calculation with the same formula to verify that I am getting the same results. Given the same data, I am returning different answers. So here is some code:

cone.h
#pragma once
class cone
{
public:
	double volume;
	int x, y, z;

public:
	cone(double base_radius, double height, int x, int y, int z);
	~cone(void);

private:
	// Since this will be handled by the constructor, no reason to let people mess with it.
	double calculateVolume(double radius, double height);
};



cone.cpp
#include "cone.h"
#define _USE_MATH_DEFINES
#include<cmath>

cone::cone(double base_radius, double height, int x, int y, int z)
{
	this->volume = this->calculateVolume(base_radius, height);
	this->x = x;
	this->y = y;
	this->z = z;
}


cone::~cone(void)
{
}

double cone::calculateVolume(double radius, double height)
{
	return (M_PI * (radius * radius) * height);
}



cylinder.h
#pragma once
class cylinder
{
public:
	double volume;
	int x, y, z;

public:
	cylinder(double radius, double height, int x, int y, int z);
	~cylinder(void);

private:
	// Since this will be handled by the constructor, no reason to let people mess with it.
	double calculateVolume(double radius, double height);
};



cylinder.cpp
#include "cylinder.h"
#define _USE_MATH_DEFINES
#include<cmath>


cylinder::cylinder(double radius, double height, int x, int y, int z)
{
	this->calculateVolume(radius, height);
	this->x = x;
	this->y = y;
	this->z = z;
}


cylinder::~cylinder(void)
{
}

double cylinder::calculateVolume(double radius, double height)
{
	return (M_PI * (radius * radius) * height);
}



main.cpp
// File  : main.cpp
// Author: Brendon Dugan [------@gmail.com] (c) 2010
//
// Parses a text file and creates certain shapes based upon the data within the file.

#include<iostream>
#include "box.h"
#include "cone.h"
#include "cylinder.h"
using std::cout;
using std::endl;
 
// Purpose: The main function, allows the user to specify a file to be parsed, parses the file,
//          and then prints relevant information to the  screen.
// Returns: Nothing
void main(void)
{
	box box1 = box(3.0, 4.5, 2.2, 1,8,10);
	cone cone1 = cone(7, 4, 3, 5, 9);
	cylinder cyl1 = cylinder(7, 4, 7, 300, 5);
	cout << "Box 1 volume = " << box1.volume << endl;
	cout << "Cone 1 volume = " << cone1.volume << endl;
	cout << "Cylinder 1 volume = " << cyl1.volume << endl;
}



Output:
Box 1 volume = 29.7
Cone 1 volume = 615.752
Cylinder 1 volume = -9.25596e+061
Press any key to continue . . .




I'm not sure what's happening, and since I'm new to C++ I'm not sure where to go next. Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: New to C++ having strange math issues

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: New to C++ having strange math issues

Posted 31 August 2010 - 06:58 AM

void main(void)

This is NEVER LEGAL in C++.

Where do you set the volume data member in your cylinder class?
Was This Post Helpful? 1
  • +
  • -

#3 brennydoogles  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Re: New to C++ having strange math issues

Posted 31 August 2010 - 07:34 AM

View PostJackOfAllTrades, on 31 August 2010 - 05:58 AM, said:

void main(void)

This is NEVER LEGAL in C++.

Where do you set the volume data member in your cylinder class?


*facepalm* I feel like an idiot for that. As for the
void main(void)
issue, what should replace it? I think I pulled this directly from the example posted by my professor (either that or VS2010 automatically created it).
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: New to C++ having strange math issues

Posted 31 August 2010 - 07:35 AM

Here it should be
this->volume = this->calculateVolume(radius, height); 

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: New to C++ having strange math issues

Posted 31 August 2010 - 07:47 AM

The signature of the standard C++ entry point is either

int main(int argc, char *argv[])

if you need to access the command line arguments, or
int main(void)

if you don't need them and can therefore ignore them.

See also

This post has been edited by JackOfAllTrades: 31 August 2010 - 07:48 AM

Was This Post Helpful? 1
  • +
  • -

#6 blutrane  Icon User is offline

  • 私もクールです

Reputation: 25
  • View blog
  • Posts: 1,934
  • Joined: 17-May 01

Re: New to C++ having strange math issues

Posted 31 August 2010 - 07:51 AM

JackOfAllTrades beat me to it. The link he provides (and the subsequent link to the text referred) gives a good explanation as to why

void main()


should be avoided. I was taught this too in my high school programming class. No howz.

It belongs in the garbage...along with

goto
>.< there's a thread on bad programming habits somewhere here...

This post has been edited by blutrane: 31 August 2010 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1