Length Conversion

I am writing a program that converts from one unit like mm to another e.g cm. The program should prompt invalid unit when something other than mm, cm, m or km is entered as the unit to be converted from or unit to be converted to. The problem is how I initialize the units. When debugging, input ends the program. What do I do?

//Program to shows conversion of units
#include <iostream> // for cin, cout
#include <string>
#include <cmath>

using namespace std;

int main()
{
char init_unit[2], sec_unit[2]; //declaration of the conversion units
double conv_val; //declaration of the number to be converted

// Welcome message
cout<< "km m cm mm km m cm mm km m cm mm km m cm mm km m cm mm\n"
<< "| |\n"
<< "| Welcome to Topiloe Metric unit converter |\n"
<< "| |\n"
<< "km m cm mm km m cm mm km m cm mm km m cm mm km m cm mm\n\n";

//request conversion unit
cout<<"Enter initial conversion unit (mm, cm, m, km): \n"; //user enters units
cin>>init_unit;
if (init_unit != 'mm'||init_unit !='cm'||init_unit !='m'||init_unit !='km')
{
cerr<<"--> Sorry, unit to convert from is invalid\n"
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else
{
cout<<"Enter unit to convert to (mm, cm, m, km): \n"; //user enters units
cin>>sec_unit;
}
if (sec_unit != 'mm'||sec_unit != 'cm'||sec_unit != 'm'||sec_unit != 'km')
{
cout<<"--> Sorry, unit to convert TO is invalid\n"
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'mm' && sec_unit == 'cm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10<<" "<<sec_unit<<endl //cm=mm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'mm' && sec_unit == 'm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*100<<" "<<sec_unit<<endl //m=mm*100
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'mm' && sec_unit == 'km')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*100000<<" "<<sec_unit<<endl //km=100000*mm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'cm' && sec_unit == 'mm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10<<" "<<sec_unit<<endl //cm=mm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'cm' && sec_unit == 'm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10<<" "<<sec_unit<<endl //cm=m*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'cm' && sec_unit == 'mm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10<<" "<<sec_unit<<endl //cm=mm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'cm' && sec_unit == 'm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10<<" "<<sec_unit<<endl //m=cm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'cm' && sec_unit == 'km')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10000<<" "<<sec_unit<<endl //km=10000*cm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'm' && sec_unit == 'mm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/100<<" "<<sec_unit<<endl //m=mm*100
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'm' && sec_unit == 'cm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10<<" "<<sec_unit<<endl //m=cm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'm' && sec_unit == 'km')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*1000<<" "<<sec_unit<<endl //km=1000*m
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'km' && sec_unit == 'mm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/100000<<" "<<sec_unit<<endl //km=100000*mm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'km' && sec_unit == 'cm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10000<<" "<<sec_unit<<endl //km=10000*cm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == 'km' && sec_unit == 'm')
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/1000<<" "<<sec_unit<<endl //km=1000*m
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}

}
[/code]
The main issue arises when attempting to compare your char arrays in the if statements.

Easiest fix for this is just to change your character arrays to strings and then change the ' ' to " " when comparing the values. Additionally, you will need to change some of your if statements such as:

if (sec_unit != "mm"||sec_unit != "cm"||sec_unit != "m"||sec_unit != "km")

with this line of code, even if the user enters in one of the correct dimensions such as "mm" the value will still return false as it is not "cm" or "m"..ect.
Change the or's to ands (|| to &&).
the operands don't seem to work for the strings. I also believe && will change the sematics of the code
I would have to see your code after you changed it to see why it is not accepting it. And the && will change the semantics which is why I want you to change it. After changing the ||'s to &&'s and changing the character arrays to strings, I got the following code to compile and run.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//Program to shows conversion of units
#include <iostream>	 // for cin, cout
#include <string>
#include <cmath>

using namespace std;

int main()
{
string init_unit, sec_unit;	 //declaration of the conversion units
double conv_val;	 //declaration of the number to be converted

// Welcome message 
cout<< "km m cm mm km m cm mm km m cm mm km m cm mm km m cm mm\n"
<< "| |\n"
<< "| Welcome to Topiloe Metric unit converter |\n"
<< "| |\n"
<< "km m cm mm km m cm mm km m cm mm km m cm mm km m cm mm\n\n";

//request conversion unit
cout<<"Enter initial conversion unit (mm, cm, m, km): \n";	//user enters units
cin>>init_unit;

if (init_unit != "mm" && init_unit != "cm" && init_unit != "km" )
{
cerr<<"--> Sorry, unit to convert from is invalid\n"
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else 
{
cout<<"Enter unit to convert to (mm, cm, m, km): \n";	//user enters units
cin>>sec_unit;

}
if (sec_unit != "mm" && sec_unit != "cm" && sec_unit != "m" && sec_unit != "km")
{
cout<<"--> Sorry, unit to convert TO is invalid\n"
<<"Thank you for using Topiloe Metric Unit converter Program\n"; 
}
else if (init_unit == "mm" && sec_unit == "cm")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10<<" "<<sec_unit<<endl //cm=mm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "mm" && sec_unit == "m")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*100<<" "<<sec_unit<<endl //m=mm*100
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "mm" && sec_unit == "km")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*100000<<" "<<sec_unit<<endl //km=100000*mm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "cm" && sec_unit == "mm")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10<<" "<<sec_unit<<endl //cm=mm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "cm" && sec_unit == "m")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10<<" "<<sec_unit<<endl //cm=m*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "cm" && sec_unit == "mm")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10<<" "<<sec_unit<<endl //cm=mm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "cm" && sec_unit == "m")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10<<" "<<sec_unit<<endl //m=cm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "cm" && sec_unit == "km")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*10000<<" "<<sec_unit<<endl //km=10000*cm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "m" && sec_unit == "mm")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/100<<" "<<sec_unit<<endl //m=mm*100
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "m" && sec_unit == "cm")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10<<" "<<sec_unit<<endl //m=cm*10
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "m" && sec_unit == "km")	
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val*1000<<" "<<sec_unit<<endl //km=1000*m
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "km" && sec_unit == "mm")	
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/100000<<" "<<sec_unit<<endl //km=100000*mm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "km" && sec_unit == "cm")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/10000<<" "<<sec_unit<<endl //km=10000*cm
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
else if (init_unit == "km" && sec_unit == "m")
{
cout<<"Enter numerical value to convert: \n";
cin>>conv_val;
cout<<"Result of conversion: "<<conv_val<<" is equivalent to "<<conv_val/1000<<" "<<sec_unit<<endl //km=1000*m
<<"Thank you for using Topiloe Metric Unit converter Program\n";
}
system("Pause");
}

Topic archived. No new replies allowed.