# homework code/looking for suggestions

• 03-02-2003
m.albert
homework code/looking for suggestions
Hi I'm a newbie.
Have written as a homework this code (see hereafter) which converts a weight from pounds to kilogams and viceversa.
Can you look at it/test it and give me your ideas for some improvements. I would appreciate your comments on the good and bad aspects of my homework.
Thanks
Michael

Code:

``` // /////////////////////////////////////////////////////////// //converts from pounds to kg #include <iostream> using namespace std; #include <cmath> int choice; void PoundsToKgFunction(); void KgToPoundsFunction(); double Pounds; double Ounces; char ans='Y'; //===== void getWeightInPounds(double& WPounds, double& WOunces); //reads the weight in pounds and ounces void ConvertPoToKg(double& variable1,double& variable2); //converts the weight from pounds and ounces to kg and grams void showResultsPotoKg(double output1, double output2); //prints the results of the conversion //====== //===================================================================== double WKg,WGr,temp1; void GetWeightKg(double& Kg, double& grams); //Gets the weight in Kilograms and Grams void ConvertWeightToPounds(double& variable1, double& variable2); //Converts the Weight into Pounds and Ounces void ShowResults(double output1, double output2); //Prints the results of the conversion //======================================================================= int main() { cout<<"This Program allows you to convert Kg to Pounds and viceversa at your choice. \n"; cout<<endl; while(ans=='Y'||ans=='y') { cout<<"If you want to convert from pounds to kg enter '1'\n" <<"If you want to convert from kg to pounds enter '2'\n"; cin>>choice; if(choice==1) PoundsToKgFunction(); else KgToPoundsFunction(); cout<<"Do you want to continue (Y/N)? "; cin>>ans; } return 0; } void PoundsToKgFunction() { getWeightInPounds(Pounds,Ounces); ConvertPoToKg(Pounds,Ounces); showResultsPotoKg(Pounds,Ounces); } //====== void getWeightInPounds(double& WPounds, double& WOunces) { cout<<"Enter Weight in Pounds and Ounces (separated by a blank): "<<endl; cin>>WPounds; cin>>WOunces; } void ConvertPoToKg(double& variable1,double& variable2) //temp1 is the weight in Pounds { double temp1; temp1=variable1+(variable2/16); variable1=floor(temp1/2.2046); variable2=((temp1/2.2046)-variable1)*1000; } void showResultsPotoKg(double output1, double output2) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); cout<<"The weight is: "<<output1<<" Kg "<<"and "<<output2<<" Grams"<<endl; } void KgToPoundsFunction() {/////////////////////////////////////////////////////////// //converts from kg to pounds GetWeightKg(WKg,WGr); ConvertWeightToPounds(WKg,WGr); ShowResults(WKg,WGr); } void GetWeightKg(double& Kg, double& grams) { cout<<"Enter Weight in Kilograms and Grams separated by a blank: "; cin>>Kg; cin>>grams; } void ConvertWeightToPounds(double& variable1, double& variable2) { temp1=variable1+(variable2/1000); variable1=floor(temp1*2.2046); variable2=(temp1*2.2046-variable1)*16; } void ShowResults(double output1, double output2) { cout<<"This weight is equal to: "<<output1<<" Pounds and "<<output2<<" Ounces "<<endl; }```
• 03-02-2003
axon
Same comments as the author above, plus : name your variables better...don't use a capital letter in the beginning of your variables, and once you find your style, be consistent about it.
Name all the variables in one place, and all the functions in another. Don't mix and match.

Also, you should comment out ALL your functions and their purpose, makes the code less confusing. You could probably overload your functions, in order not to have as many of them. Make each function "universal" - like a little complete program inside your main one.

good luck,

axon
• 03-03-2003
m.albert