# New to C++ having strange math issues

Page 1 of 1

## 5 Replies - 2244 Views - Last Post: 31 August 2010 - 07:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=188465&amp;s=8e2f1706d2b9220de6f273a696d1d7bb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 brennydoogles

Reputation: 13
• 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:

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.
};

```

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->x = x;
this->y = y;
this->z = z;
}

cone::~cone(void)
{
}

{
}

```

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.
};

```

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

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

cylinder::~cylinder(void)
{
}

{
}

```

main.cpp
```// File  : main.cpp
// Author: Brendon Dugan [[email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */] (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

• Saucy!

Reputation: 6237
• Posts: 24,002
• 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?

### #3 brennydoogles

Reputation: 13
• Posts: 59
• Joined: 23-June 09

## Re: New to C++ having strange math issues

Posted 31 August 2010 - 07:34 AM

JackOfAllTrades, 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).

### #4 poncho4all

Reputation: 123
• Posts: 1,422
• 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);
```

• Saucy!

Reputation: 6237
• Posts: 24,002
• 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.

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

### #6 blutrane

• 私もクールです

Reputation: 26
• 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
```

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

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}