Selasa, 28 Februari 2012

Find Errors


1. What is wrong with this code:

cout<<”Enter n:”;

cin>>n;

if (n<0)


     cout<<”That is negative. Try again”<<endl;

cin>>n;

else

cout<<”ok n=”<<n<<endl;

Answer:
There is more than one statement between the if clause and the else clause. They need to be made into a compound statement by enclosing them in braces { }.

cout<<”Enter n:”;

cin>>n;

if (n<0)

             
{
 cout<<”That is negative. Try again”<<endl;

      cin>>n;
  }else

cout<<”ok n=”<<n<<endl;
2.      What is wrong with this code:
if(x = 0) cout<<x<<”=0\n”;
else cout<<x<<”!=0\n”;

Answer:
if (x = = 0)cout<<x<<"=0\n";
3.       What is wrong with this code:
if (x < y < z) cout<<x<<”<”<<y<<”<”<<z<<endl; 

Answer:
if (x < y && y <z) cout<<x<<”<”<<y<<”<”<<z<<endl;

Tiada ulasan:

Catat Ulasan