//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";
}

}

You need to fix all the compiler errors before you attempt to run the program.

the two variables declared on line 9 are too small if you expect to enter two characters on line 21. cin will add a string null terminating character, so increase the size of the two variables by 1 byte each.

if (init_unit != 'mm' || init_unit != 'cm' || init_unit != 'm' || init_unit != 'km')

Two characters must be enclosed in double quotes, such as "mm". and since init_unit is a character array you can't use == operator to compare it with another character array

 if ( strcmp(init_unit,"mm") != 0 ||  strcmp(init_unit,"cm") != 0 ||  strcmp(init_unit,"m") != 0 ||  strcmp(init_unit,"km") != 0)

You have the same problem in many other lines in that program.

That means I have to declare as char init_unit[2] and sec_unit[2]. How do I take care of the parts where I have else if from line 58 downwards

That means I have to declare as char init_unit[2] and sec_unit[2].

No, declare as char init_unit[3], sec_unit[3];

How do I take care of the parts where I have else if from line 58 downwards

By calling stramp() similar to how I showed you in my last post.

That would all be simpler if you used std::string instead of char arrays.

std::string init_unit, sec_unit;

Then you can use the == operator like you did, but still have to put the two characters in double quotes, such as "mm" istead of 'mm'

This is what I have now but when I use mm or cm or m or km, it shows the invalid message and then asks me to input the valu which it does correctly. How do I tackle that part?

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

using namespace std;

int main()
{
    char init_unit[2], sec_unit[2];
    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 ( strcmp(init_unit,"mm") != 0 ||  strcmp(init_unit,"cm") != 0 ||  strcmp(init_unit,"m") != 0 ||  strcmp(init_unit,"km") != 0)
    {
        cout<<"--> 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 ( strcmp(sec_unit,"mm") != 0 ||  strcmp(sec_unit,"cm") != 0 ||  strcmp(sec_unit,"m") != 0 ||  strcmp(sec_unit,"km") != 0)
        {
            cout<<"--> Sorry, unit to convert TO is invalid\n"
            <<"Thank you for using Topiloe Metric Unit converter Program\n";
        }
    else
    {
        cout<<"Enter numerical value to convert: \n";
        cin>>conv_val;
    }
    if (strcmp(init_unit,"mm") == 0 && strcmp(init_unit,"cm") == 0)
    {
        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 (strcmp(init_unit,"mm") == 0 && strcmp(init_unit,"m") == 0)
    {
        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 (strcmp(init_unit,"mm") ==0 && strcmp(sec_unit,"km")==0)
    {
        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 (strcmp(init_unit,"cm") ==0 && strcmp(sec_unit,"mm")==0)
    {
        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 (strcmp(init_unit,"cm") ==0 && strcmp(sec_unit,"m")==0)
    {
        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 (strcmp(init_unit,"cm") ==0 && strcmp(sec_unit,"km")==0)
    {
        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 (strcmp(init_unit,"m") ==0 && strcmp(sec_unit,"mm")==0)
    {
        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 (strcmp(init_unit,"m") ==0 && strcmp(sec_unit,"cm")==0)
    {
        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 (strcmp(init_unit,"m") ==0 && strcmp(sec_unit,"km")==0)                         
    {
        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 (strcmp(init_unit,"km") ==0 && strcmp(sec_unit,"mm")==0)                            
    {
        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 (strcmp(init_unit,"km") ==0 && strcmp(sec_unit,"cm")==0)
    {
        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 (strcmp(init_unit,"km") ==0 && strcmp(sec_unit,"m")==0)
    {
        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";
    }

}

line 10 is still wrong -- you failed to make the correction I pointed out. The pogram isn't going to work unless you increase the size of those two variables.

line 23: use && instead of ||. Let's say you enter "mm". Line 23 is going to fail because it's not "nm", so you need the && operator to say if it's not any of those strings.

line 41: Think about what the && operator does, according to line 41 you expect init_unit to be both "mm" and "cm" at the same time. Not possible. You need the || operator instead of && operator.

All the other lines foollowing line 41 are using the wrong && operator for the same reason as I mentioned above.

I have made the change but it is meant to quit if the user enters the wrong unit. Even when I use the correct value, though it runs, it shows me the invalid message instead of the next instruction.

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

using namespace std;

int main()
{
    char init_unit[3], sec_unit[3];
    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 ( strcmp(init_unit,"mm") != 0 ||  strcmp(init_unit,"cm") != 0 ||  strcmp(init_unit,"m") != 0 ||  strcmp(init_unit,"km") != 0)
    {
        cout<<"--> 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 ( strcmp(sec_unit,"mm") != 0 ||  strcmp(sec_unit,"cm") != 0 ||  strcmp(sec_unit,"m") != 0 ||  strcmp(sec_unit,"km") != 0)
        {
            cout<<"--> Sorry, unit to convert TO is invalid\n"
            <<"Thank you for using Topiloe Metric Unit converter Program\n";
        }
    else
    {
        cout<<"Enter numerical value to convert: \n";
        cin>>conv_val;
    }

I already told you what's wrong with it, reread my previous post, second paragraph. You have the && and || operators mixed up. Sometimes even more experienced programmers have that problem.

Man you are awesome. Thanks alot. I got it already. I also put the return 0 after line 26 and 34. It made the program end when the user inputs wrong values. Now it is perfect. Please kindly look at my second post if you could help out

You never answered the last question, the problem states it's supposed to be in java.

I don't think so but is it possible to combine Java and C++ on the same compiler?

Not that I know of. I don't know very much about java. But you might be able to use the same IDE, such as NetBeans (don't know about it either).

I was told its completely C++

This is what I have now but the output from line 32, it should output the characters on the same line but it shows the characters on different lines. The second comparism doesn't seem to come out well too

/Program to shows analysis of texts
    #include <iostream>      // for cin, cout
    #include <string>
    #include <iomanip>

    using namespace std;

    int main()
    {
        string word1, word2;        //declaration of words

        // Welcome message 
        cout<< "------------------------------------------------\n"
            << "    Topiloe's Text Analyzer - Release 1.0  \n"
            << "------------------------------------------------\n\n";

        cout<<"Enter two words on one line: ";
        cin>>word1>>word2;
        cout<<"Second word you entered is <"<<word2<<"> \n";
        cout<<"It is "<<word2.length()<<" characters long\n";
        cout<<"Starts with the letter '"<<word2.substr(0,1)<<"'\n";
        int last_word;
        last_word=word2.length()-1;
        cout<<"Ends with the letter '"<<word2.substr(last_word,1)<<"'\n\n";

        cout<<"First word you entered is <"<<word1<<"> \n";
        cout<<"It is "<<word1.length()<<" characters long\n";
        cout<<"Starts with the letter '"<<word1.substr(0,1)<<"'\n";
        last_word=word1.length()-1;
        cout<<"Ends with the letter '"<<word1.substr(last_word,1)<<"'\n\n";

        for(int i = 0; i < word1.length(); i++)
        {
            for(int j = 0; j < word2.length(); j++)
            {
                if(word1[i] == word2[j])  //letter matched
                    {
                        cout<<"The leters in <"<<word1<<"> which are also in <"<<word2<<"> are "<<word1[i]<<"\n";
                    }
            }
        }
        cout<<"There are "<<word1.(word2)<<" letters in "<<word1<<" which are also in "<<word2<<endl;

        for(int i = 0; i < word2.length(); i++)
        {
            for(int j = 0; j < word1.length(); j++)
            {
                if(word2[i] == word1[j])  //letter matched
                {
                    cout<<"The leters in <"<<word2<<"> which are also in <"<<word1<<"> are "<<word2.find(word2[i])<<"\n";
                }
            }
        }
        cout<<"There are "<<word2.find(word1)<<" letters in "<<word2<<" which are also in "<<word1<<endl;

        cout<<"Thank you for using Topiloe Text Analyzer\n";
        return 0;

    }

If you want all the characters on the same line then remove the newline character '\n' from the cout statement(s).

The second comparism doesn't seem to come out well too

Line number please???

I was told its completely C++

You mean NetBeans? It's an IDE that's used for several different languages.

Line number 50. It outputs integers instead of characters I mean alphabets

This is the output I have but it should be a n c and not separating the characters.
Also I should not have 1,5 and 6 it should be characters like the first one should be

line 50 should probably be the same as line 38.

I have gotten a good solution to it. It is;

//Program to shows analysis of texts
#include <iostream>      // for cin, cout
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    string word1, word2;        //declaration of words

    // Welcome message 
    cout<< "------------------------------------------------\n"
        << "    Topiloe's Text Analyzer - Release 1.0  \n"
        << "------------------------------------------------\n\n";

    cout<<"Enter two words on one line: ";
    cin>>word1>>word2;
    cout<<"Second word you entered is <"<<word2<<"> \n";
    cout<<"\tIt is "<<word2.length()<<" characters long\n";
    cout<<"\tStarts with the letter '"<<word2.substr(0,1)<<"'\n";
    int last_word;
    last_word=word2.length()-1;
    int count_word1_in_word2 = 0, count_word2_in_word1 = 0;
    cout<<"\t with the letter '"<<word2.substr(last_word,1)<<"'\n\n";

    cout<<"First word you entered is <"<<word1<<"> \n";
    cout<<"\tIt is "<<word1.length()<<" characters long\n";
    cout<<"\tStarts with the letter '"<<word1.substr(0,1)<<"'\n";
    last_word=word1.length()-1;
    cout<<"\tEnds with the letter '"<<word1.substr(last_word,1)<<"'\n\n";
    cout<<"The leters in <"<<word1<<"> which are also in <"<<word2<<"> are \n";
    for(int i = 0; i < word1.length(); i++)
    {
        for(int j = 0; j < word2.length(); j++)
        {
            if(word1[i] == word2[j])  //letter matched
            {
                cout<<word1[i]<< " ";
                count_word2_in_word1++;
            }
        }
    }
    cout<<endl;
    cout<<"There are "<<count_word2_in_word1<<" letters in "<<word1<<" which are also in "<<word2<<endl;
    cout<<"The leters in <"<<word2<<"> which are also in <"<<word1<<"> are \n";
    for(int i = 0; i < word2.length(); i++)
    {
        for(int j = 0; j < word1.length(); j++)
        {
            if(word2[i] == word1[j])  //letter matched
            {
                cout<<word2[i]<<" ";
                count_word1_in_word2++;
            }
        }
    }
    cout<<endl;
    cout<<"There are "<<count_word1_in_word2<<" letters in "<<word2<<" which are also in "<<word1<<endl;

    cout<<"Thank you for using Topiloe Text Analyzer\n\n";
    return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.