Regarding my project!(problem)

Friends,
I 've choosen distance converter as my project.Main theme of my project is to convert kilometer,feet,inches,centimeter into eachother.I 've initiated making program but got stuck and became unable to make program.please help me :(
I'm posting the code:
CODE:1
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
void main()
{
float a;
char b[15];
char c[15];


cout<<"Enter the length:"<<endl;
cin>>a;
cout<<"Enter the unit of value:"<<endl;
cin>>b;
cout<<"Enter the unit in which you wana convert:"<<endl;
cin>>c;
if(b=="inches" && c=="feet")
cout<<"The converted value is: "<<a*0.0833<<endl;
if(b=="inches" && c=="centimeter")
cout<<"The converted value is: "<<a*2.54<<endl;
if(b=="inches" && c=="meter")
cout<<"The converted value is: "<<a*0.0254<<endl;
if(b=="inches" && c=="kilometer")
cout<<"The converted value is: "<<a*2.5E-05<<endl;
if(b=="inches" && c=="yard")
cout<<"The converted value is: "<<a*0.02778<<endl;
if(b=="inches" && c=="mile")
cout<<"The converted value is: "<<a*1.5E-05<<endl;

}

The code above is entirely not working for me though compiler giving no error.

CODE:2

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
void main()
{
float a;
char b[15];
char c[15];


cout<<"Enter the length:"<<endl;
cin>>a;
cout<<"Enter the unit of value:"<<endl;
cin>>b;
cout<<"Enter the unit in which you wana convert:"<<endl;
cin>>c;
if(strlen(b)==6 && strlen(c)==4)
cout<<"The converted value is: "<<a*0.0833<<endl;
if(strlen(b)==6 && strlen(c)==10)
cout<<"The converted value is: "<<a*2.54<<endl;
if(strlen(b)==6 && strlen(c)==5)
cout<<"The converted value is: "<<a*0.0254<<endl;
if(strlen(b)==6 && strlen(c)==9)
cout<<"The converted value is: "<<a*2.5E-05<<endl;


}

Problem here is this's the conversions of inches to other units .When i want to convert feet into other units then following ambiguity arises:
if(strlen(b)==6 && strlen(c)==4)
cout<<"The converted value is: "<<a*0.0833<<endl
if(strlen(b)==4 && strlen(c)==6)
cout<<"The converted value is: "<<a*12<<endl

How to solve this problem?
Last edited on
This doesn't do what you think:

if(b=="inches" && c=="feet")

#include <cstring> and do it like this:

if(strcmp(b,"inches")==0 && strcmp(c,"feet")==0)

Alternatively, you could #include <string> and declare b and c like this:

1
2
string b;
string c;

Then, if(b=="inches" && c=="feet") will work.

EDIT: However, I'd prefer something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std;

const int units_max=3;
double convert[units_max][units_max]=
{
    1.0,        0.01,       0.00001,
    100.0,      1.0,        0.001,
    100000.0,   1000.0,     1.0
};

const string units[units_max]={"centimeter(s)","meter(s)","kilometer(s)"};

int main()
{
    double value;
    double new_value;
    int units_from;
    int units_to;

    cout << "enter value: ";
    cin >> value;

    for (int i=0; i<units_max; i++)
        cout << '(' << i+1 << ") " << units[i] << endl;

    cout << "enter current units: ";
    cin >> units_from;

    cout << "enter desired units: ";
    cin >> units_to;

    new_value=value*convert[units_from-1][units_to-1];

    cout << "converted value: ";
    cout << new_value << endl;

    while (cin.get()!='\n');
    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
Last edited on
@ master
Thanks a ton!
i got you :)

EDIT:

Unable to understand below:

double convert[units_max][units_max]=
{
1.0, 0.01, 0.00001,
100.0, 1.0, 0.001,
100000.0, 1000.0, 1.0
};

Let me understand bold .How u did?
Last edited on
Well, the first line is how you convert centimeters to other units:
a centimeter is 1.0 centimeter, 0.01 meters, 0.00001 kilometers.

The second line is how you convert meters to other units:
a meter is 100.0 centimeters, 1.0 meter, 0.001 kilometers.

The third line is how you convert kilometers to other units:
a kilometer is 100000.0 centimeters, 1000.0 meters, 1.0 kilometer.

When user chooses 2,1 (meters to centimeters) the multiplication operand for the conversion is convert[1][0] which is the number in second row and first column, which is 100.0.
Thanks!

If i want to include weight converter too in your program then what modifications should i do?
Like:
Enter type of value
Enter value
Enter current unit of value
Enter desired unit

Waiting for your reply!
Last edited on
Topic archived. No new replies allowed.