Jump to content
  • Advertisement
Sign in to follow this  
Hammonjj

Untitled

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently learning about function overloading and so I decided to write a simple program just to practice. All it does is get the cube of some number(int or double type). When I run the program I never get the right answers. I tried commenting out each function one at a time and it worked fine, but it doesn't work as a whole Can anyone fix my, probably, rookie mistake? #include<iostream> using namespace std; int h; void cube(int a); void cube(double b); int main() { int num; double num2; cout<<"Enter an integer number to be cubed: "; cin>>num; cout<<"Enter a decimal number to be cubed: "; cin>>num2; cube(num); cube(num2); cin>>h; return 0; } void cube(int a){ a=a*a*a; cout<<a; } void cube(double b){ b=b*b*b; cout<<b; }

Share this post


Link to post
Share on other sites
Advertisement
It works for me. Your problem may be that you don't break line between the two answers so the numbers run together. (I did 4 for the int and 3.4 for the doble and the answer was 6439.304 it really is 64 and 39.304)

It is better to have functions like cube return their result instead of printing it. Then this error would be harder to make:

int cube( int a )
{
return a*a*a;
}

double cube( double b )
{
return b*b*b
}


Hope this helps!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!