Tuesday, July 28, 2009

Need help with my Programing?

Hey everyone i am writing this code and cant seem to find my error in it. I have just started doing C++ and i am trying to find some errors i am useing Microsoft visual C++ and its is comming up with 11 errors and 2 warrings but i cant seem to find them any help would be great here is the code i wrote








// pp 2.2-2





#include %26lt;iostream%26gt;





int main()


{


const double W_IN_LBS = 1.0


const double W_IN_KIL = 0.453592





int pounds,


kilograms;


double total_pounds





// Obtain data from user





cout %26lt;%26lt; "Enter your weight in pounds:";


cin %26gt;%26gt; pounds;





// Convert everything to kilograms





total_kilograms = 1.0 * 0.453592 ;





kilograms = total_kilograms; // truncates to interger


// display results


cout %26lt;%26lt; end1;


cout %26lt;%26lt; "your weight in kilograms is" %26lt;%26lt; kilograms %26lt;%26lt; "kilograms" %26lt;%26lt; end1;


return 0;


}

Need help with my Programing?
Everything mentioned above is correct but you also need to do the following to get your program compiling and working correctly.





1) Add the following line after the #include as cin and cout are part of the standard namespace





using namespace std;





2) When the user enters in the pounds value you need to pass that on to the conversion formula ie





total_kilograms = pounds * 0.453592;





also define total_kilograms as its missing ie





double total_kilograms;





3) Cast total_kilograms to an integer to get rid of the warning.





kilograms = (int)total_kilograms; // truncates to interger





although personally it looks a lot more accurate to print the double value out but its up to you.





4) Dont forget to


- add the semi-colons at the ends of the lines that need them


- initialise the variables to 0 (this is especially useful for pounds ie int pounds=0;)


- its endl not end1





Good luck
Reply:Every line of code needs to end with a semicolon. Some of your lines don't have one. I bet that covers almost all of your errors and warnings.





ie, const double W_IN_LBS = 1.0 %26lt;- no semicolon





In the future, please post the compiler errors too - they do say what the problem is, or at least point to the line the problem is
Reply:You're missing a bunch of semicolons. Also, your logic is incorrect; you probably meant to compute "total_kilograms" by multiplying "pounds" by W_IN_KIL.
Reply:Your missing a semicolon at the end of "double total_pounds", "const double W_IN_LBS = 1.0" and "const double W_IN_KIL = 0.453592"


and you're typing "end1" instead of "endl"
Reply:I haven't done C++ in a year or so, but I'll try





"int pounds,"





You put a , instead of a ;





"double total_pounds"





There is no ;





Also, you have a constant double as a single integer. You don't need it to be a double, can just be an integer. I'm pretty sure that wouldn't be an error, but it takes up more space with it being a double rather than an integer.





Sorry if this isn't much help, I'm rather rusty.


No comments:

Post a Comment