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";
}
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 &&).
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.
//Program to shows conversion of units
#include <iostream> // for cin, cout
#include <string>
#include <cmath>
usingnamespace 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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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";
}
elseif (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");
}