is there any error in my codes? |
There are no syntax errors in your code (i.e. it's valid C++).
But it isn't doing what you require, so it's got logic errors. In this case, you're missing a lot of cases (if you want to stick to the nested if tests, that is.)
So, how do i troubleshoot this? |
You could add output for all the elses, so you can see what's going on with your logic.
But...
As well as being a lot tidier, manudude03's version is using operator&& which causes the less-than conditions to be evaluated in groups, e.g.
if (A<B && A<C && A<D && A<E)
is testing that A is less than B, C, D, and E
all at the same time. But your style of code does the tests one by one and must therefore handle every single, possible permutation independently. But you're missing several cases, which is why the logic doesn't work out.
Andy
PS To get the nested if tests to work as well as manudude03's code, you need:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
if(A<B)
if(A<C)
if(A<D)
if(A<E)
cout<<"\n A is smallest";
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else // EXTRA
if(D<E) // EXTRA
cout<<"\n D is smallest"; // EXTRA
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else // EXTRA
if(C<D) // EXTRA
if(C<E) // EXTRA
cout<<"\n C is smallest"; // EXTRA
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else // EXTRA
if(D<E) // EXTRA
cout<<"\n D is smallest"; // EXTRA
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else
if(B<C)
if(B<D)
if(B<E)
cout<<"\n B is smallest";
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else // EXTRA
if(D<E) // EXTRA
cout<<"\n D is smallest"; // EXTRA
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else
if(C<D)
if(C<E)
cout<<"\n C is smallest";
else // EXTRA
cout<<"\n E is smallest"; // EXTRA
else
if(D<E)
cout<<"\n D is smallest";
else
cout<<"\n E is smallest";
| |