0

I'm writing a simple c++ measurement program that asks the user input to select from which unit they would like to measure the unit from and to the unit? but I don't know if the structure is correct (my C++ really sucks). Here is my code:

#include <iostream>
using namespace std;

int main()
{
    int storeFROM, storeTO;
    char mm, cm, m, kk;

    cout << "Enter the initial unit (mm, cm, m, or km): ";
    cin >> storeFROM;

    cout << endl;

    if ((storeFROM != 'mm') || storeFROM != 'cm' || storeFROM != 'm' || storeFROM != 'km') {
        cout << "--> Sorry unit to convert FROM is invalid" << endl;
    }
    else if ((storeFROM == 'mm') || storeFROM == 'cm' || storeFROM == 'm' || storeFROM == 'km') 
    {
        cout << "Enter the initial unit(mm, cm, m, or kk) :";
        cin >> storeTO;
    }

    // Calculate the selected units
    system("pause");
}

I hope someone can help me asking the user input to ask from which unit they want, this is exactly how it should show:

enter image description here

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
PatrickGamboa
  • 672
  • 4
  • 22
  • 43
  • Some part of the last sentence is missing in the question. – jrok Feb 26 '14 at 18:45
  • yea i didn't add the metric calculations because I might know how to do that, it's just setting up the structure of how to ask the question, is the part that i am really bad at :( – PatrickGamboa Feb 26 '14 at 18:47
  • Start by storing user input in the right datatypes. Right now you are trying to store `char`s and `strings`s in `int`s, and later on compare them to `char`s and `string`s again. Also you cant compare strings using `==` – Tim Feb 26 '14 at 18:53

4 Answers4

2

Here is the right code:

bool isValidUnit(const std::string &unit);
int convertValue(double value, const std::string &unitFrom, const std::string &unitTo);

int main() {

    ...

    std::string storeFROM, storeTO;

    cout << "Enter the initial unit (mm, cm, m, or km): ";
    cin >> storeFROM;

    if (!isValidUnit(storeFROM)) {
        cout << "--> Sorry unit to convert FROM is invalid" << endl;
        ...
    }

    cout << "Enter the initial unit (mm, cm, m, or km): ";
    cin >> storeTO;

    if (!isValidUnit(storeTO)) {
        cout << "--> Sorry unit to convert TO is invalid" << endl;
        ...
    }

    double value;

    cout << "Enter the value in (" << storeFROM << "): ";
    cin >> value;

    double valueConverted = convertValue(value, storeFROM, storeTO);

    cout << "Value in (" << storeTO << "): " << valueConverted << endl;

    return 0;
}

bool isValidUnit(const std::string &unit) {
    return unit == "mm" || unit == "cm" || unit == "m" || unit == "km";
}

double unitMultiplier(const std::string &unit) {
    if (unit == "mm") return 0.001;
    if (unit == "cm") return 0.01;
    if (unit == "m") return 1;
    if (unit == "km") return 1000;
    return 0.;
}

double convertValue(double value, const std::string &unitFrom, const std::string &unitTo) {
    if (unitFrom == unitTo) return value;
    if (value <= 0.) return 0.; // or value :)

    // Get it in meters
    int valueInMetes = value * unitMultiplier(unitFrom);
    return valuesInMeter / unitMultiplier(unitTo);
}
maverik
  • 5,508
  • 3
  • 35
  • 55
  • the !isValidUnit at the if statement is getting me an error, do I put this bool at the beginning before using isValidUnit at the if statement? – PatrickGamboa Feb 26 '14 at 19:03
  • 2
    @PatrickGamboa, you should put this function (the whole function or its declaration) before you reference to it. Place it before main function – maverik Feb 26 '14 at 19:10
  • I got it working entirely, just editing some few codes, but the process worked! ok sorry to bother u but using my code, how do I use the units that the user inputed and do the specific unit conversion? hope you can help :) – PatrickGamboa Feb 26 '14 at 19:45
  • I've updated the answer. Not sure it works right because didn't check it. – maverik Feb 26 '14 at 20:01
  • wow thank you! I've added them in, im just not sure about the conversion result if they are the right one, but I will get back to you! :D – PatrickGamboa Feb 26 '14 at 20:49
  • hey! yea i did the conversion, for example i wanted to convert from km to mm and I added 56.12 km, the program actually convert the units to 5.612e+007, but the actual conversion is supposed to be 56123000.00 mm, i mixed up some conversion units, but I wasn't able to convert it to the proper units, any help here? – PatrickGamboa Feb 26 '14 at 21:06
  • 1
    @PatrickGamboa, `5.612e+007 = 56120000` and this is the right result. – maverik Feb 26 '14 at 21:10
  • ah wait a sec the result gave me an exponential result, i added a cout.precision(9) to give more accurate result, thank you very much for your help! really appreciate it! :D – PatrickGamboa Feb 26 '14 at 21:15
1

In your program, storeFROM and storeTO are both strings, so you should declare them as strings:

string storeFROM, storeTO;

And when comparing them with values like "cm", "mm", etc, you should quote them in "", not '' as you definitely don't want them to be multicharacter literals. It should look like this:

if ((storeFROM != "mm") || storeFROM != "cm" 
                        || storeFROM != "m" || storeFROM != "km") 
{
    cout << "--> Sorry unit to convert FROM is invalid" << endl;
}
else if ((storeFROM == "mm") || storeFROM == "cm" 
                             || storeFROM == "m" || storeFROM == "km") 
{
    cout << "Enter the initial unit(mm, cm, m, or kk) :";
    cin >> storeTO;
}
Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
0
string storeFROM, storeTO; // note string thingie
float value1, value2;

cin>>storeFROM;

if ((storeFROM != "mm") || storeFROM != "cm" || storeFROM != "m" || storeFROM != "km") )
{
    cout << "--> Sorry unit to convert FROM is invalid" << endl;
}
else  
{
    cin>>value1;
    cout << "Enter the initial unit(mm, cm, m, or kk) :";
    cin >> storeTO;
    if((storeTO == "mm") || storeTO == "cm" || storeTO == "m" || storeTO == "km"))
    {
       cin>>value2;
          //convert here...
          //hint: use switch-case
    }
}
//end programme

here...

Sly_TheKing
  • 518
  • 7
  • 14
0

You may not enter a non-acceptable symbols as for example letter 'm' for an object of type int.

You can use this code as a template for your program.

#include <iostream>
#include <string>
#include <initializer_list>
#include <algorithm>
#include <cstdlib>

std::ostream & display_meazures( const std::initializer_list<const char *> &l,
                                 std::ostream &os = std::cout )
{
    auto it = l.begin();
    for ( size_t i = 0; i < l.size(); i++ )
    { 
        if ( i == l.size() - 1 ) os << "or ";
        os << *it;
        if ( i != l.size() - 1 ) os << ", ";
    }

    return os;
}

int main()
{
   std::string storeFROM, storeTO;

   std::initializer_list<const char *> meazure = { "mm", "cm", "m", "km" };

   while ( true )
   {
      std::cout << "Enter the initial unit (" << display_meazures( meazure ) 
                << "): ";
      std::cin >> storeFROM;

      if ( std::find( meazure.begin(), meazure.end(), storeFROM ) != 
           meazure.end() ) 
      {
        break;
      }

      std::cout << "--> Sorry unit to convert FROM is invalid" << std::endl;
      std::cout << std::endl;
   }

// Calculate the selected units

   std::system( "pause" );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Looks like an overkill. You can also use [user defined literals](http://en.cppreference.com/w/cpp/language/user_literal) – maverik Feb 26 '14 at 19:20