1.

Solve : Need help with C++ code?

Answer»

So what's wrong with this code guys ?


#include ;
#include ;
using namespace std;

int main()
{
float Amount ;

cout << "enter how much you want to pay";
cin >> Amount;

if (Amount>=1500);
cout << "CLASS A";

else if (Amount>700 && Amount <=1499);
cout << "Class b";

else if (Amount>500 && Amount <= 699);
cout << "Class c";

else if (Amount<500);
cout << "ro7 erkab toktok "<
else
cout << "roo7 moot" << endl;

system ("pause");
return 0;


}I know nothing about C++, but I got this version to compile with Dev-C++

Code: [SELECT]#include <iostream>;
#include <cmath>;
using namespace std;

int main()
{
float Amount ;

cout << "enter how much you want to pay";
cin >> Amount;

if (Amount>=1500)
cout << "Class A";

else if (Amount>700 && Amount <=1499)
cout << "Class b";

else if (Amount>500 && Amount <= 699)
cout << "Class c";

else if (Amount<500)
cout << "ro7 erkab toktok "<<endl;

else
cout << "roo7 moot" << endl;

system ("pause");
return 0;


}

GOOD luck. SideWinder's corrections should fix the various syntax errors.

(semicolons are not necessary on any PREPROCESSOR command, such as #include- it just adds an empty statement (no harm done either way)).

a comment on the logic flow. Consider for a moment, the conditions of the various else if's.

a quick example: the first one checks if the amount is larger then 1500. if so, it prints "Class A" and the remainder of the else's is ignored.

This means that the only way the other Else conditions will be evaluated WOULD be if the value is already less then zero- this makes your second check, (Amount <=1499) redundant, since that is a "Known" fact in the else. the same logic applies with each condition, all the way down to the standard "else" which will never be executed at all.



Discussion

No Comment Found