Length Converter

Is there anyway to make this simpler? And/or that can function better?

http://pastebin.com/0SXJFuBn

Couldn't post the code here, exceeds the maximum character limit.

I am a beginner, have only been learning C++ for the last 10-15 hours.
Array-like containers (like std::vector). You can have both the names of the units and numeric constants in what is essentially a lookup table. Then you won't need that many functions, and you can even easily add more units.
closed account (j3Rz8vqX)
Simpler? not really, but significantly shorter.

Example:
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
#include <iostream>
#include <cstdlib>

using namespace std;
void procedure(double value, int index, const string verb[]);

int main()
{

    int sel = 0;
    const string verb[] = {"NauticalMile: ","Mile: ","Yard: ","Meter: ","Foot: ","Inch: ","Centimeter: ","Millimeter: ","Kilometer: "};

    cout << "                        Welcome to the Length Converter" << endl;
    cout << "                               Version: 0.5.0" << endl;
    cout << "                              Made by: Marc G." << endl << endl;

    while (sel != -1) {

        cout << "[1] Nautical Mile\n[2] Mile\n[3] Yard\n[4] Meter\n[5] Foot\n[6] Inch\n[7] Centimeter\n[8] Millimeter\n[9] Kilometer" << endl;
        cout << "Please pick one (Type -1 to Exit!): ";
        cin >> sel;
        cout << endl;

        if (cin.fail())
        {
            cout << "----------------------------------" << endl;
            cout << "Error: String Entry Detected - Terminating Program!" << endl;;
            cout << "----------------------------------" << endl << endl;
            cin.clear();
            cin.ignore(1000,'\n');
            break;
        }

        if(sel<=9&&sel>0)
        {
            double value;
            cout << "Choose how many "<<verb[sel-1];
            cin >> value;
            cin.clear();
            cin.ignore(1000,'\n');
            procedure(value,sel,verb);
        }
        else if(sel != -1)
        {
            cout << "----------------------------------" << endl;
            cout << "Please select one of the options on the list!" << endl;
            cout << "----------------------------------" << endl << endl;
        }
    }
    system("PAUSE");
    return 0;
}
void procedure(double value, int index, const string verb[])
{
    const static double table[][8] =
    {
        {1.15078,2025.37,1852,6076.12,72913.4,185200,1.852e+6,1.852},
        {0.868976,1760,1609.34,5280,63360,160934,1.609e+6,1.60934},
        {0.000493737,0.000568182,0.9144,3,36,91.44,914.4,0.0009144},
        {0.000539957,0.000621371,1.09361,3.28084,39.3701,100,1000,0.001},
        {0.000164579,0.000189394,0.333333,0.3048,12,30.48,304.8,0.0003048},
        {1.3715e-5,1.5783e-5,0.0277778,0.0254,0.0833333,2.54,25.4,2.54e-5},
        {5.3996e-6,6.2137e-6,0.0109361,0.01,0.0328084,0.393701,10,1e-5},
        {5.3996e-6,6.2137e-6,0.00109361,0.001,0.00328084,0.0393701,0.1,1e-6},
        {0.539957,0.621371,1093.61,1000,3280.84,39370.1,100000,1e+6}
    };
    const static string line = "----------------------------------";
    cout << line << '\n';
    for(int i=0;i<8;++i)
        cout<<verb[i+1]<<value*table[index-1][i]<<'\n';
    cout << line << "\n\n";
}
@Dput What's the benefits of writing it that way, user wise?
I can see for a programmer it would be much smaller and easier to handle.
It makes no difference to the end user.

It is "simpler" for the original programmer to write (less, or different "issues"). It is most likely easier for the maintenance programmer, who at the request of the end user has to add more units to code, whose logic has to be figured out by reading the code.

Note the redundancy:
1
2
3
const string verb[] = {"Nautical Mile: ","Mile: ","Yard: ","Meter: ","Foot: ","Inch: ","Centimeter: ","Millimeter: ","Kilometer: "};

cout << "[1] Nautical Mile\n[2] Mile\n[3] Yard\n[4] Meter\n[5] Foot\n[6] Inch\n[7] Centimeter\n[8] Millimeter\n[9] Kilometer" << endl;

Compared to (uses C++11):
1
2
3
4
5
const vector<string> verb {"Nautical Mile","Mile","Yard","Meter","Foot","Inch","Centimeter","Millimeter","Kilometer"};

for ( size_t i = 0; i < verb.size(); ++i ) {
  cout << '[' << i+1 << "] " << verb[i] << '\n';
}


Topic archived. No new replies allowed.