Can this be done any cleaner/more efficient?

I made a small database and it works perfectly, but is there a way to make it cleaner?

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
include <iostream>

using namespace std;

string gr = "grootte: ";
string g = "Geboortedatum: ";
string l = "Leeftijd: ";

int main ()
{
    cout<<"Welke persoon zijn details wil je?\nEnter zijn ID nummer\n";
    cout<<"ID   Naam\n\n";
    cout<<"1    Xander\n";
    cout<<"2    Dieter\n";
    cout<<"3    Vincent\n";
    cout<<"4    Erwin\n";
    cout<<"5    Els\n\n";

    int a;
    cin>> a;

    switch (a)
    {
        case 1:
        cout<<"\n"<< g <<"26 september 1995 \n";
        cout<< l <<"152 cm \n";
        cout<< gr <<"15 \n";
        break;

        case 2:
        cout<<"\n"<< g <<"27 juli 1997 \n";
        cout<< l <<"146 cm \n";
        cout<< gr <<"13 \n";
        break;

        case 3:
        cout<<"\n"<< g <<"30 mei 2000 \n";
        cout<< l <<"10\n";
        cout<< gr <<"140 cm \n";
        break;

        case 4:
        cout<<"\n"<< g <<"3 november 1967 \n";
        cout<< l <<"42 \n";
        cout<< gr <<"170 cm \n";
        break;

        case 5:
        cout<<"\n"<< g <<"7 oktober 1967 \n";
        cout<< l <<"42 \n";
        cout<< gr <<"167 cm \n";
        break;
    }

    return 0;
}


Thanks in advance,

Xander
Last edited on
What do you mean by "cleaner"?
Well, without so much typing
Not really, it is a very small program already.
Of course you can. You can eliminate all the redundant cout calls and merge all the typing into one line, and that would save some at the potential expense of readability (there are ways around that).

Aside from that... not really.

Oh... check line 1 for spelling errors.

-Albatross
Last edited on
You should also catch the trailing '\n' left by std::cin on line 20 with std::cin.ignore(); straight after it.
You can improve it more. What would you do if you had data for 50 persons? That switch statement would be awfully big... OOP and look-up tables are your friends ;) Take a look at this:

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
#include <iostream>
#include <string>

using namespace std;

struct Person
{
    string m_n; //name
    string m_g; //birth-date
    string m_gr;//height
    string m_l; //age

    static const string gr;
    static const string g;
    static const string l;

    Person(const char * n_, const char * g_, const char * gr_, const char * l_):
        m_n(n_),m_g(g_),m_gr(gr_),m_l(l_){}
};

const string Person::gr = "Grootte: ";
const string Person::g = "Geboortedatum: ";
const string Person::l = "Leeftijd: ";

ostream & operator<<(ostream & os, const Person & p)
{
    os << Person::g << p.m_g << endl;
    os << Person::gr << p.m_gr << endl;
    os << Person::l << p.m_l << endl;

    return os;
}

const int db_size=5;

Person database[]={
    Person("Xander","26 september 1995","152 cm","15"),
    Person("Dieter","27 juli 1997","146 cm","13"),
    Person("Vincent","30 mei 2000","140 cm","10"),
    Person("Erwin","3 november 1967","170 cm","42"),
    Person("Els","7 oktober 1967","167 cm","42")
};

int main ()
{
    int i;

    cout<<"Welke persoon zijn details wil je?\nEnter zijn ID nummer\n";
    cout<<"ID   Naam\n\n";

    for (i=0; i<db_size; i++)
        cout << i+1 << ' ' << database[i].m_n << endl;

    cin >> i;
    cin.get();
    cout << endl;

    if (i>=1 && i<=db_size)
        cout << database[i-1] << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}
Last edited on
Someone's giving out full solutions more and more.

-Albatross
Well, I post because I like helping people, not because I like posting ;D
Topic archived. No new replies allowed.