Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Josh

macrumors 68000
Original poster
Mar 4, 2004
1,640
1
State College, PA
Hi,

I've wrote a small program to calculate the volume of a hollow metal "tube."

Here is my code:
Code:
#include <iostream>
#include <cmath>
using namespace std;

#define pi 3.141592654;

int main () {
	float r,t,h,result;	// radius, thickness, height
	
	// the measurements
	r = 5.0;
	t = 3.0;
	h = 10.0;
	
	// doing the math
	result = (pi*pow(r+t,2)*h)-(pi*pow(r,2)*h);
		
	// return the result
	cout << "The required amount of metal is: \n" << result << " cubic centimeters.\n";
	
	return 0;
	
}

The result = line is the one giving me problems, but I don't see what's wrong with it :confused:

It gives me the invalid arguement of 'unary *' error, as well as some "expected ) before ;" and "expected ; before (" errors as well.

Thanks for any help, I really appreciate it.

*EDIT*

Fixed it somehow.
 

SamMiller0

macrumors member
Aug 17, 2004
66
0
San Jose, CA
You need to read Scott Meyers' book titled "Effictive C++", item #2 says "prefer consts, enums, and inlines to #define".

You should change your #define pi to:

Code:
const double pi = 3.141592654;

and it should work fine
 

fimac

macrumors member
Jan 18, 2006
95
1
Finland
Josh said:
Here is my code:
Code:
#define pi 3.141592654;

The result = line is the one giving me problems, but I don't see what's wrong with it :confused:

The trailing semi-colon is the source of your problem; #define'd words work rather like search-and-replace. Either remove the semi-colon or declare pi as a const (which IMHO is better).
 

Josh

macrumors 68000
Original poster
Mar 4, 2004
1,640
1
State College, PA
Thanks for the info.

I'll have to look into that book; the only c++ book I currently have is our class textbook, which doubles as an intro to comp. science, so it's not very thorough I don' think.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.