Untitled
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;
}
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:
Hope this helps!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement