Loop issue?

So I have this assignment that requires me to do the following things:
1. Take a roman numeral input as a cstring.
2. Convert the roman numeral to all uppercase.
3. Validate the the string uses valid roman numeral letters. (it can only use the letters M, D, C, L, X, V, I)
4. Convert the roman numeral to a decimal number.

i have so far gotten the first two requirements down, however i believe i have formatted one or more of my loops wrong, as the output is strange.

Also, i haven't finished writing all of the code, as i am trying to solve this loop problem first.


So, i solved the looping problem, now i just need to figure out how to convert the roman numeral into a decimal number. My instructor actually gave us some hint code, which you can see in the int romtoint(char roman[SIZE]) function.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>


using namespace std;

//Global constants:
const int SIZE = 16;

//Function prototypes

void inttorom();

int romtoint(char roman[SIZE]);
char romaninput();
bool romanvali(char roman[SIZE]);
char romanUper(char roman[SIZE]);

int main()
{
    int choice = 0;
    char roman[SIZE];
    
    do
    {
    cout << "\n----------------------Main Menu----------------------------\n"
         << "Select an option by entering it's number then press enter.\n"
         << "===========================================================\n"
         << "1.] Convert Roman Numeral to Integer." << endl
         << "2.] Convert Integer to Roman Numeral. (beta)" << endl
         << "3.] Exit program." << endl;
    
    cin >> choice;
    
    switch(choice)
    {
        case 3: return 0;
                break;
        case 2: inttorom();
                break; 
        case 1: romaninput();
                break;
        default: cout << "Invalid option." << endl;
    };
    
    }while((choice > 3)||(choice < 1));
        
    romanUper(roman);

    system ("pause");
    return 0;
}
////////////////////////////////////////////////////////////////////////////////
char romanUper(char roman[SIZE])
{
    for(int x=0;(x < SIZE)&&(roman[x]!='\0'); x++)
    {
        if((roman[x] >= 97) && (roman[x] <= 122))
            roman[x] -= 32;
    }
    
    return (roman[SIZE]);
}
////////////////////////////////////////////////////////////////////////////////
bool romanvali(char roman[SIZE])
{     
    for(int x=0;(x < SIZE)&&(roman[x]!='\0'); x++)
    {    
        switch(roman[x])
        {
            case 'M':
            case 'm':
            case 'D':
            case 'd':
            case 'C':
            case 'c':
            case 'L':
            case 'l':
            case 'X':
            case 'x':
            case 'V':
            case 'v':
            case 'I':
            case 'i': continue;   
            default: return false;
        }
    }
    return true;
}
////////////////////////////////////////////////////////////////////////////////
char romaninput()
{
    char roman[SIZE];
    bool good = false;
    cin.ignore();
    
    do
    {
        cout << "\nEnter a roman numeral: ";
        
        cin.getline(roman, SIZE, '\n');
        
        good = romanvali(roman);
        if(!good)
            cout << "Not a valid Roman Numeral.";
            
    }while(!good);
    
    romanUper(roman);
    romtoint(roman);
    //cout << "\nThe roman numeral: " << roman << endl;    

    
    return (roman[SIZE]);
    
}
////////////////////////////////////////////////////////////////////////////////
int romtoint(char roman[SIZE])
{
    int roman2[SIZE];
    
    cout << "\nThe roman numeral is: " << roman << endl;
    
    for(int x=0;(x < SIZE)&&(roman[x]!='\0'); x++)
    {
        if(roman[x] == 'M')
            return 1000;
        
        if(roman[x] == 'D')
            return 500;
            
        if(roman[x] == 'C')
            return 100;
            
        if(roman[x] == 'L')
            return 50;
            
        if(roman[x] == 'X')
            return 10;
            
        if(roman[x] == 'V')
            return 5;
            
        if(roman[x] == 'I')
            return 1;

    }
    return 1;
}
////////////////////////////////////////////////////////////////////////////////
void inttorom()
{
    
}

Last edited on
Topic archived. No new replies allowed.