What is wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

What is wrong?

#include <iostream> using namespace std; int bigger(int a,int b){ if(a>b) return a; return b; } int main() { cout<<"enter a 2 numbers"; cin>>x>>y; bigger(int x,int y); cout<<bigger; return 0; }

6th Jun 2021, 2:10 PM
Habosha Al_krady
Habosha Al_krady - avatar
13 Answers
+ 8
Habosha Al_krady , here is the slightly reworked code with some comments: #include <iostream> using namespace std; int bigger(int a, int b){ if(a>b) { return a; } else { // else was missing here return b; } } int main() { int x = 0, y = 0; // The variables were not declared and initialized. cout<<"enter a 2 numbers: "; cin>>x>>y; cout<<endl << bigger(x, y); return 0; }
6th Jun 2021, 3:47 PM
Lothar
Lothar - avatar
+ 5
Habosha Al_krady , i am not sure what you mean with your latest post. are you referring to the code that i have reworked? if there is an issue with it, can you please give a sample input that creates a problem? thanks!
6th Jun 2021, 8:03 PM
Lothar
Lothar - avatar
+ 3
bigger is not variable,it is a function print the return value immediately, or assign it to a variable and print
6th Jun 2021, 2:18 PM
durian
durian - avatar
+ 3
No, I was just explaining about the condition of the condition and I said there is no need to write else because as we know that if the first condition is not fulfilled, the second condition will be the fulfilled one (I use the Google translation).
6th Jun 2021, 8:31 PM
Habosha Al_krady
Habosha Al_krady - avatar
+ 2
Lothar Habosha Al_krady is right: only the main function needs to be reworked ^^ if you want to rework the bigger function, then show rather the use of ternary operator: int bigger(int a, int b) { return a>b ? a : b; } ;)
6th Jun 2021, 9:48 PM
visph
visph - avatar
+ 2
Habosha Al_krady why there are 2 main functions?
7th Jun 2021, 10:47 AM
durian
durian - avatar
+ 2
Habosha Al_krady , visph , you are both right, that the code in the function is running properly. (my personal opinion): it may look something like cool to have a code like this, but nevertheless i would recommend to use a more explicit style of coding, even if it takes a bit more time by writing it in this way. this time is a good investment to the future to keep code easy to read and to maintain and to make code review successful.
8th Jun 2021, 10:38 AM
Lothar
Lothar - avatar
+ 1
The else did not work because the if clauses when the first condition is not met will certainly fulfill the second.
6th Jun 2021, 6:35 PM
Habosha Al_krady
Habosha Al_krady - avatar
+ 1
Well, I will also try this method
7th Jun 2021, 10:32 AM
Habosha Al_krady
Habosha Al_krady - avatar
+ 1
(This is not working (There is an error? #include<iostream> using namespace std; int main(){ int bigger(int a,int b){ return (a>b)?a:b; } int main() { int x,y; cout<<"enter a 2 numbers"; cin>>x>>y; int c=bigger(x,y); cout<<c; return 0; }
7th Jun 2021, 10:44 AM
Habosha Al_krady
Habosha Al_krady - avatar
+ 1
if it's a code coach you must not output anything else than the expected output: delete the first cout line from your main function...
7th Jun 2021, 10:47 AM
visph
visph - avatar
+ 1
This one is correct #include <iostream> using namespace std; int bigger(int a, int b){ if(a>b) { return a; } else { // else was missing here return b; } } int main() { int x = 0, y = 0; // The variables were not declared and initialized. cout<<"enter a 2 numbers: "; cin>>x>>y; cout<<endl << bigger(x, y); return 0; }
7th Jun 2021, 11:49 AM
Amir Hussain Oroujlou
Amir Hussain Oroujlou - avatar
0
Well thanks for the help
7th Jun 2021, 10:52 AM
Habosha Al_krady
Habosha Al_krady - avatar