Tuesday, 29 October 2013

C++ Program - Conversion from anything to anything!

Here is the source code for a program to convert anything to anything, like Dollar to Euros and viceversa, or maybe Kelvin temperature to Celsius temperature!
What you need is :

  • Correct formula to convert between quantities :
        e.g., To convert from Celsius to Fahrenheit, you must know that F = (9/5 * C) + 32
Here is the program source code :
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
please share our blog to your friends to support us */
#include<iostream.h>
#include<conio.h>

void main()
{
    int op_1,op_2,op_3,op_4;
    float lbs,kg,faren, cel, dol, eur;

    cout<<"Choose any :"<<endl;
    cout<<"1. Kilogram and pound"<<endl;
    cout<<"2. Farenheit and celcius"<<endl;
    cout<<"3. Dollars to Euros (Current exchange rates applicable)"<<endl;

    cin>>op_1;

    switch(op_1)
    {
        case 1:
           cout<<"Press 1 for Kilogram to pound and 2 for pound to kilogram"<<endl;
           cin>>op_2;
       switch(op_2)
       {
           case 1:
               cout<<"Enter the weight in kg"<<endl;
               cin>>kg;
               lbs = 2.204*kg;
               cout<<kg<<" kg = "<<lbs<<" pounds"<<endl;
               break;
           case 2:
               cout<<"Enter weight in pounds"<<endl;
               cin>>lbs;
               kg = .45*lbs;
               cout<<lbs<<" pounds = "<<kg<<" kg"<<endl;
               break;
        }
        break;
        case 2:
           cout<<"Press 1 for Farenheit to celsius and 2 for Celsius to farenheit"<<endl;
           cin>>op_3;
           break;
       switch(op_3)
       {
           case 1:
              cout<<"Enter the temperature in farenheit"<<endl;
              cin>>faren;
              cel = .5555*(faren - 32.0);
              cout<<faren<<" F = "<<cel<<" C"<<endl;
              break;
           case 2:
              cout<<"Enter the temperature in celcius"<<endl;
              cin>>cel;
              faren = ((1.8*cel) + 32.0);
              cout<<cel<<" C = "<<faren<<" F"<<endl;
              break;
       }
      break;
    case 3 :
       cout<<"Press 1 for Dollars to Euros and 2 for Euros to Dollars"<<endl;
       cin>>op_4;
      switch(op_4)
       {
        case 1:
            cout<<"Enter currency in Dollars"<<endl;
            cin>>dol;
            eur = .73 * dol;
            cout<<dol<<" USD = "<<eur<<" EUROS"<<endl;
            break;
        case 2:
            cout<<"Enter currency in Euros"<<endl;
            cin>>eur;
            dol= 1.38 * eur;
            cout<<eur<<" EUROS = "<<dol<<" USD"<<endl;
            break;
       }
      break;
    default :
        cout<<"Invalid choice! Try again!!!"<<endl;
    }
cout<<"Thanks for using the program!!!"<<endl;
}
For newer IDEs (like Dev C++):
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
please share our blog to your friends to support us */
#include<iostream>
using namespace std;
 
int main()
{
    int op_1,op_2,op_3,op_4;
    float lbs,kg,faren, cel, dol, eur;
 
    cout<<"Choose any :"<<endl;
    cout<<"1. Kilogram and pound"<<endl;
    cout<<"2. Farenheit and celcius"<<endl;
    cout<<"3. Dollars to Euros (Current exchange rates applicable)"<<endl;
 
    cin>>op_1;
 
    switch(op_1)
       {
      case 1 :
         cout<<"Press 1 for Kilogram to pound and 2 for pound to kilogram"<<endl;
         cin>>op_2;
     switch(op_2)
       {
         case 1:
            cout<<"Enter the weight in kg"<<endl;
            cin>>kg;
            lbs = 2.204*kg;
            cout<<kg<<" kg = "<<lbs<<" pounds"<<endl;
            break;
         case 2:
            cout<<"Enter weight in pounds"<<endl;
            cin>>lbs;
            kg = .45*lbs;
            cout<<lbs<<" pounds = "<<kg<<" kg"<<endl;
            break;
       }
       break;
    case 2 :
       cout<<"Press 1 for Farenheit to celsius and 2 for Celsius to farenheit"<<endl;
       cin>>op_3;
      switch(op_3)
       {
        case 1:
            cout<<"Enter the temperature in farenheit"<<endl;
            cin>>faren;
            cel = .5555*(faren - 32.0);
            cout<<faren<<" F = "<<cel<<" C"<<endl;
            break;
        case 2:
            cout<<"Enter the temperature in celcius"<<endl;
            cin>>cel;
            faren = ((1.8*cel) + 32.0);
            cout<<cel<<" C = "<<faren<<" F"<<endl;
            break;
       }
       break;
    case 3 :
        cout<<"Press 1 for Dollars to Euros and 2 for Euros to Dollars"<<endl;
        cin>>op_4;
      switch(op_4)
       {
        case 1:
            cout<<"Enter currency in Dollars"<<endl;
            cin>>dol;
            eur = .73 * dol;
            cout<<dol<<" USD = "<<eur<<" EUROS"<<endl;
            break;
        case 2:
            cout<<"Enter currency in Euros"<<endl;
            cin>>eur;
            dol= 1.38 * eur;
            cout<<eur<<" EUROS = "<<dol<<" USD"<<endl;
            break;
       }
       break;
    default :
       cout<<"Invalid choice! Try again!!!"<<endl;
   }
   cout<<"Thanks for using the program!!!"<<endl;
 return 0;
}
Working

There ain't much to say about the working of this program. It looks huge and confusing, but it really is not! The program is a little bit large since I used nested switch statements (i.e., switch inside body of another switch). But if you look carefully, you will understand that the program is really simple and if I had used, if-else-if statements, the program would have become more complex.


P.S : You may add as many conversions as you want to this program! :)


No comments:

Post a Comment

You might also like