- 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; }
14 Respostas
+ 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;
}
+ 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!
+ 3
bigger is not variable,it is a function
print the return value immediately, or assign it to a variable and print
+ 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).
+ 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;
}
;)
+ 2
Habosha Al_krady why there are 2 main functions?
+ 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.
+ 1
The else did not work because the if clauses when the first condition is not met will certainly fulfill the second.
+ 1
Well, I will also try this method
+ 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;
}
+ 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...
+ 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;
}
0
Well thanks for the help