A More efficient way of doing it

So I decided to do a program that only converts numbers to roman numerals(I'll probably incorporate both but that's for another time) and I want to ask if anyone knows a more efficient way of doing it. By the way, I did a through check of 10 numbers to make sure that it was producing the correct roman numerals and if you find a number that doesn't produce the correct pattern then please comment on it.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>

using namespace std;

int main()
{
    string romanNumeral; // The variable that will concatenate each roman numeral.
    int integer; // The variable that holds the number the user wishes to convert.

    cout << "Please enter an integer that you wish to convert to roman Numerals. ";
    cin >> integer;

    while(integer >= 5000 || integer <= 0)
    {
        cerr << "This integer is out of the domain. Only numbers from 1-4999 are allowed. ";
        cin >> integer;
    }

    while(integer >= 1000)
    {
        integer -= 1000;
        romanNumeral += 'M';
    }

    if(integer >= 900 && integer + 100 >= 1000)
    {
        integer -= 900;
        romanNumeral += "CM";
    }

    while(integer >= 500)
    {
        integer -= 500;
        romanNumeral += 'D';
    }

    if(integer >= 400 && integer + 100 >= 500)
    {
        integer -= 400;
        romanNumeral += "CD";
    }

    while(integer >= 100)
    {
        integer -= 100;
        romanNumeral += 'C';
    }

    if(integer >= 90 && integer + 10 >= 100)
    {
        integer -= 90;
        romanNumeral += "XC";
    }

    while(integer >= 50)
    {
        integer -= 50;
        romanNumeral += 'L';
    }

    if(integer >= 40 && integer + 10 >= 50)
    {
        integer -= 40;
        romanNumeral += "XL";
    }

    while(integer < 50 && integer >= 10)
    {
        integer -= 10;
        romanNumeral += 'X';
    }

    if(integer == 9 && integer + 1 == 10)
    {
        integer -= 9;
        romanNumeral += "IX";
    }

    while(integer >= 5 && integer < 10)
    {
        integer -= 5;
        romanNumeral += 'V';
    }

    if(integer == 4 && integer + 1 == 5)
    {
        integer -= 4;
        romanNumeral += "IV";
    }

    while(integer > 0 && integer < 5)
    {
        integer -= 1;
        romanNumeral += 'I';
    }

    cout << "Roman Numeral: " << romanNumeral << endl;

    return 0;
}
Last edited on
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
#include <iostream>
#include <string>
#include <utility>

std::string to_roman(int value)
{
    static const std::pair<int, const char*> romandata[] = {
        {1000, "M" }, {900, "CM"}, {500, "D" }, {400, "CD"}, {100, "C" },
        {  90, "XC"}, { 50, "L" }, { 40, "XL"}, { 10, "X" }, {  9, "IX"},
        {   5, "V" }, {  4, "IV"}, {  1, "I" },
    };
    std::string result;
    for (const auto& current: romandata) {
        while (value >= current.first) {
            result += current.second;
            value  -= current.first;
        }
    }
    return result;
}

int main()
{
    for (int i = 1; i <= 4000; ++i) {
        std::cout << to_roman(i) << '\n';
    }
}
Thanks MiiniiPaa. I'll just want to ask this because it has been bugging for awhile. What's the difference between these two for loops.

1
2
3
4
5
6
7
8
9
10
11
for(blah: blah) 
{
    // stuff here.
}

versus 

for(blah = 0; blah < blah2; blah++)
{
   // .stuff here.
}
Last edited on
Topic archived. No new replies allowed.