0
What is wrong with this c++ code?
#include <iostream> using namespace std; int main() { int a; cout << "a is" << endl; cin >> a; int b; cout << "b is" << endl; cin >> b; if (a > b) { cout << "greatest is " << a << endl; } if (b > a) { cout << "greatest is " << b << endl; } }
3 Answers
+ 1
Error: You're printing a and b on the console before it is defined in line 6 and 9.
To avoid this, swap 6th and 7th, and swap 9th and 10th line!
0
you have put two if it is worng
but you have to put if and else like thisđ
if (a>b)
{
cout<<"greatest is a:"<<a<<endl;
}
else(b>a)
{
cout<<"greatest is b:"<<b;
}
0
My suggestion is to :-
1. First set all the variables at the beginning after "int main (){"
2. Then you can then get user input with the "cin >>".
3. Now use the "if else" statements and there respective conditions.
!! Hope it helped !!