garbage with correct output

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Divide a number by another if and only if the second number is not equal to 0

#include<iostream>
using namespace 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; 
}
Last edited on
I would have thought you would get a compiler warning as your function division() doesn't return any value....

plese enter first number : 10
please enter second number : 144
division = 0.06944
6.59754e-039




my problem is with the bolded part in either case it is printed & i think problem lie in the function cout part.
1
2
3
4
5
6
7
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 :-)
}
//Divide a number by another if and only if the second number is not equal to 0

no math requirement is in else part i deadly wanted to print tha statement is there any solution
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.

buddy !!!! you are too good........
Topic archived. No new replies allowed.