code is compiling correctly problem is with the output the division = result + garbage is showing I tried a lot but that garbage is not removed.compile & see result
at the end
//Divide a number by another if and only if the second number is not equal to 0
#include<iostream>
usingnamespace std;
float division(float,float);
main()
{
int num1,num2;
cout<<"Please enter first number : ";
cin>>num1;
cout<<"Please enter second number : ";
cin>>num2;
cout<<division(num1,num2);
}
float division (float num1,float num2)
{
if(num2 != 0)
cout<<"\ndivision = "<<num1/num2<<endl;
else
cout<<"\nplease enter digit other than zero"<<endl;
}
in main() you output the result returned by your function division(), but you are not returning a value from your division function. You need to calculate the division and return the result rather than just cout it.
1 2 3 4 5 6 7 8 9
float division (float num1,float num2)
{
if(num2 != 0)
cout<<"\ndivision = "<<num1/num2<<endl;
else
cout<<"\nplease enter digit other than zero"<<endl;
something is missing here :-)
}
You could either call your division function only if the second number is != 0
or return 0 from your division function if the second number passed in is == 0
Either way if you define a function to return a value... it needs to return a value.