Unit conversion with 3 functions

This is a discussion on Unit conversion with 3 functions within the C++ Programming forums, part of the General Programming Boards category; Hello I am a very new to C++. I am trying to convert a given number of meters and centimeters ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Unit conversion with 3 functions

    Hello I am a very new to C++. I am trying to convert a given number of meters and centimeters into feet and inches using three function. I think that I am close but since i am new I am having a hard time figuring out what i have done wrong. Thanks,

    Code:
    #include <iostream>
    using namespace std;
    
    
    double unitconversion (double lengthme, double lengthcent);
    void unitoutput (double lengthme, double lengthcent);
    
    
    int main ()
    {
        double lengthme, lengthcent;
        char answer;
        
        do
        {
             cout<<"Enter a length in meters and centimeters to be converted:";
             cin>>lengthme>>lengthcent;
             cout<<unitoutput (lengthme, lengthcent);
             cout<<"\n Test again (y/n)";
             cin>>answer;
             cout<<endl;
             }while (answer== 'y' || answer== 'y');
             
        system ("PAUSE");
        return 0;
    }
    
    
    double unitconversion (double lengthme, double lengthcent)
    {
           lengthme=lengthme/0.3048;
           lengthcent=lengthcent/100;
           lengthcent=lengthcent/0.3048;
           lengthcent=lengthcent*12;
           return (lengthme, lengthcent);
           }
    void unitoutput (double lengthme, double lenghtcent)
    {
           cout<<"The length is "<<lengthme<<" ft and "<<lengthcent<<" in.\n";
           }
    Last edited by Magi; 02-08-2013 at 01:19 PM.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Location
    New Delhi, India, India
    Posts
    6
    Errors:
    1. You haven't called the unitconversion function.
    2. A function can return only one value at time, you might pass the parameters as a reference.
    3. don't cout<<unitoutput(lengthme, lengthcent);
    just call unitoutput(lengthme, lengthcent);

    Suggestions
    1. You are converting the metres to feet and centimetres to inches which is not exactly helpful. You should reconsider the unitconversion function.
    2. while (answer== 'y' || answer== 'y') both 'y's are in lowercase hence redundant.
    Last edited by Chanakya; 02-08-2013 at 01:38 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Ya I know the conversion isn't very helpful. This is for a school project and it specifically asks for this conversion. Weird I know. I made the change but it is still not compiling correctly
    Code:
    #include <iostream>
    using namespace std;
     
     
    void unitconversion (double lengthme, double lengthcent);
    void unitoutput (double lengthme, double lengthcent);
     
     
    int main ()
    {
        double lengthme, lengthcent;
        char answer;
         
        do
        {
             cout<<"Enter a length in meters and centimeters to be converted:";
             cin>>lengthme>>lengthcent;
             unitconversion (lengthme, lengthcent);
             unitoutput (lengthme, lengthcent);
             cout<<"\n Test again (y/n)";
             cin>>answer;
             cout<<endl;
             }while (answer== 'y' || answer== 'y');
              
        system ("PAUSE");
        return 0;
    }
     
     
    void unitconversion (double lengthme, double lengthcent)
    {
           lengthme=lengthme/0.3048;
           lengthcent=lengthcent/100;
           lengthcent=lengthcent/0.3048;
           lengthcent=lengthcent*12;
           }
    void unitoutput (double lengthme, double lengthcent)
    {
           cout<<"The length is "<<lengthme<<" ft and "<<lengthcent<<" in.\n";
           }
    Last edited by Magi; 02-08-2013 at 01:50 PM.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Location
    New Delhi, India, India
    Posts
    6
    pass the parameters as a refrence.
    void unitconversion (double &lengthme, double &lengthcent);
    remove the return statement in unitconversion
    don't cout<<unitoutput(lengthme, lengthcent);
    just call unitoutput(lengthme, lengthcent)

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Awesome!! Doing the '&' in front of the variables actually did the trick. Thank you for your help, I have been surprised in the subtle differences between c and c++; I really apreciate the help.
    Last edited by Magi; 02-08-2013 at 02:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Unit Tests
    By nelson777 in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2011, 08:16 PM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Conversion functions
    By Bones in forum C++ Programming
    Replies: 5
    Last Post: 09-04-2003, 12:37 AM
  4. Conversion and Constructor Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 5
    Last Post: 03-06-2003, 06:39 AM
  5. conversion functions
    By WildBill in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2003, 07:35 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21