Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compiler Question

Status
Not open for further replies.

FinnMan

Technical User
Feb 20, 2001
75
US
Can some explain to me (C++ newbie) why the following code compiles and runs fine using g++ on linux but always returns a value of 0 when compiled with DevShed on an XP box? I'm assuming it has to do with the compiler and not the platform, am I correct?

****************************
// Include statements
#include <iostream>
using namespace std;
// function prototypes.
float cube_number(float num);
int main()
{
float number;
float number4;
cout << "Please enter a number \n";
cin >> number;

number4 = cube_number(number);
cout << number << " cubed is " << number4;
return 0;
}
float cube_number(float num)
{
// this function simply takes a number, cubes it,
// then returns the answer
float answer;
return answer;
}
***************************************

Thx,
FM
 
CORRECTION: I don't get a valid value under g++ either. All values return 3.98812e-34. Bad code example in the book I'm using??
 
Your cube_number function is wrong.
Try this one:
Code:
float cube_number(float num)
{
  return num * num * num;
}

--
Globos
 
I thought the math (or lack thereof)looked funny. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top