help to find error

please help to fint where the error is in this code i want the function to display the bigger number among the one there in the program
#include <iostream>
using namespace std;
int max(int a,intb)
{
int a=100;
int b=200;
int ret;
ret= max(a,b);
return ret;
}
int main()
{
int result;
if(a>b)
result=a;
{
cout<<"The answer is"<< result<<endl;
}
else
result=b;
{
cout<<"The answer is"<< result <<endl;
return 0;
}
}
Please, read the compiler error messages and try to update your program yourself. I think that the compiler already pointed out undeclared identifier intb
Last edited on
You have written the function, but for it to work, you must call it! Use this code-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
int max(int a,int b)
{
if(a>b)
    return a;
else if(a<b)
    return b;
}
int main()
{
int c=0,d=0,e=0;
cout<<"Enter first number";
cin>>c;
cout<<"\n Enter second number";
cin>>d;
e=max(c,d);
cout<<"The answer is"<< e<<endl;
return 0;
}
@Sucho


Your code has undefined behavior because function max returns nothing in the case of a == b
Last edited on
Then use this-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
int max(int a,int b)
{
if(a>b)
    return a;
else if(a<b)
    return b;
else if(a==b)
    return 1;
}
int main()
{
int c=0,d=0,e=0;
cout<<"Enter first number";
cin>>c;
cout<<"\n Enter second number";
cin>>d;
e=max(c,d);
if(e!=1)
      cout<<"The answer is"<< e<<endl;
else
      cout<<"The Entered numbers are same";
return 0;
}
Last edited on
@Sucho


This function definition is invalid
1
2
3
4
5
6
7
8
9
int max(int a,int b)
{
if(a>b)
    return a;
else if(a<b)
    return b;
else if(a==b)
    return 1;
}


because its return value 1 is ambiguios. It is not clear whether a == b or the maximum is equal to 1.

The correct function definition will look as

1
2
3
4
int max( int a, int b )
{
   return ( ( a < b ) ? b : a );
}

Yup, I get your point. Foolishly overlooked that. But in your code what will it be if a==b? and moreover i can change my code which will work. Ckeck it out-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
int max(int a,int b)
{
if(a>b)
    return a;
else if(a<b)
    return b;
else if(a==b)
    return (a+b);
}
int main()
{
int c=0,d=0,e=0;
cout<<"Enter first number";
cin>>c;
cout<<"\n Enter second number";
cin>>d;
e=max(c,d);
if(e!=c+d)
      cout<<"The answer is"<< e<<endl;
else
      cout<<"The Entered numbers are same";
return 0;
}

The returned value will never be maximum as the return value changes according to the entered numbers. What do u say @vlad?
Well. how do you determine that a is equal to b? You will use the expression

max( a, b ) == a + b;

This expression looks very strange and only confuses a user.

As for the code I suggested all is simple

1
2
3
4
5
6
7
8
if ( a == b )
{
   std::cout << "a and b are equal each other" << std::endl;
}
else
{
   ats::cout << "the maximum is equal to " << max( a, b ) << std::endl;
}
Last edited on
Topic archived. No new replies allowed.