Thread: homework code/looking for suggestions

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    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;
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Thanks for your comments. Much appreciated.
    Michael.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    How can I overload the functions if they have the same number of arguments?
    How do I go about making the functions universal?
    Thanks
    Michael

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM