Program doesn't execute correctly

Hi everyone,
as soon as I execute the program that I got below, it first calculates for a couple of minutes and then starts to output infinite 'L's no matter what number I enter

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
  #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
    string zeichenvorrat {"IVXLCDM"};
    int Number;
    vector<char> ConvertedRomanNumber;
    int i;
    int PrintCounter;
    cin >> Number;
    int Thousands = Number / 1000;
    for(i = 1; i <= Thousands; i++) 
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[7]);
        Number -= 1000;
    }
    if (Number / 500 != 0)
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[6]);
        Number -= 500;
    }
    int Hundreds = Number / 100;
    if (Hundreds == 4)
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[5]);
        ConvertedRomanNumber.push_back(zeichenvorrat[6]);
        Number -= 400;
    } 
    else
    {
        for (i = 1; i <= Hundreds; i++) 
        {
            ConvertedRomanNumber.push_back(zeichenvorrat[5]);
            Number -= 100;
        }
        
    }
    if (Number / 50 != 0) 
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[4]);
        Number -= 50;
    }
    int Tenths = Number / 10;
    if (Tenths == 4)
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[3]);
        ConvertedRomanNumber.push_back(zeichenvorrat[4]);
        Number -= 40;
    } 
    else 
    {
        for (i = 1; i <= Tenths, i++;) 
        {
            ConvertedRomanNumber.push_back(zeichenvorrat[3]);
            Number -= 10;
        }
    }
    if (Number / 5 != 0)
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[2]);
        Number -= 5;
    }
    int Ones = Number;
    if (Ones == 4)
    {
        ConvertedRomanNumber.push_back(zeichenvorrat[1]);
        ConvertedRomanNumber.push_back(zeichenvorrat[2]);
        Number -= 4;
    }
    else
    {
        for (i = 1; i <= Ones; i++)
        {
            ConvertedRomanNumber.push_back(zeichenvorrat[1]);
            Number -= 1;
        }
        
    }
    if (Number == 0)
    {
        cout << "Conversion successful" << endl;
        for (i = 0; i < ConvertedRomanNumber.size(); i++)
        {
            PrintCounter = i-1;
            cout << ConvertedRomanNumber[PrintCounter];
        }
        
    }
    else
    {
        cout << "An error occured!" << endl;
    }
    return 0;
}
What debugging of the program have you done? Have you traced though the program execution using the debugger and watching the value of the variables? Given a specific input, where does program execution deviate from that expected from the program design? When you have a deviation then correct that and repeat until the program works as expected.

Once you have an issue, if you don't understand why provide some specific details - line numbers etc where the issue occurs.
That's the point: I can't debug.
Once i try to debug vs code tells me the program doesn't exist eventhough i already compiled it and run it several times. It tells me my .exe is missing or invalid
Last edited on
What compiler/debugger are you using? What os?
L6 zeichenvorrat has 7 chars indexed as 0 to 6 - not 1 to 7!

L15 - zeichenvorrat is indexed with 7 which is outside the valid index range of zeichenvorrat

Similar for other uses of zeichenvorrat

Note in c++, for loops usually start at 0 and have a condition of < - not start at 1 with a condition of <=
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

std::string to_roman(unsigned int i)
{
   if (i > 3999) return {}; // 3999, or MMMCMXCIX, is the largest standard Roman numeral

   static const std::string ms[]  { "","M","MM","MMM" };
   static const std::string cds[] { "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" };
   static const std::string xls[] { "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" };
   static const std::string ivs[] { "","I","II","III","IV","V","VI","VII","VIII","IX" };
   
   return ms[i / 1000] + cds[(i % 1000) / 100] + xls[(i % 100) / 10] + ivs[i % 10];
}

// return values for individual Roman Number characters
unsigned int from_roman(char c)
{
   switch (c)
   {
   case 'I': return 1;    case 'V': return 5;   case 'X': return 10;
   case 'L': return 50;   case 'C': return 100; case 'D': return 500;
   case 'M': return 1000; default:  return 0;
   }
}

// parse the full string for individual characters
unsigned int from_roman(std::string roman)
{
   unsigned int result {};

   for (size_t i {}, n { roman.length() }; i < n; ++i)
   {
      const auto j { from_roman(roman[i]) };   // Integer value of the i'th roman digit
      // Look at the next digit (if there is one) to know whether to add or subtract j
      if (i + 1 == n || j >= from_roman(roman[i + 1])) result += j; else result -= j;
   }

   return result;
}

int main()
{
   std::cout << "1234 in Roman numerals is " << to_roman(1234) << std::endl;
   std::cout << "MMXX in Arabic numerals is " << from_roman("MMXX") << std::endl;
}
1234 in Roman numerals is MCCXXXIV
MMXX in Arabic numerals is 2020
I'm using MinGW on Windows on VS code.. Thx for all the help i got it to work now
Topic archived. No new replies allowed.