Number conversion switch/end loop issues

I have two issues I cannot quite figure out in my program. The first has to do with the default case of the switch. As it is now the when the default case is triggered, the program prints;

Input a Roman Number and I will tell you its integer equivalent :
u
Not a valid roman number, try again
The decimal value of the roman number is 0

Would you like to enter another number? If yes enter capital Y, if not enter capital N.

I think it has something to do with the condition outside the loop not accounting for the default case of the switch. I'm not sure how or if I can declare the default case in the conditional statement. The other issue is that the bool if/else's to end the program work, but I noticed that in addition to the specified "Y" any character entered (y, p, #) results in the same "Input roman number" promt as when entering "Y". Is it just cycling regardless of the condition statement?

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

using namespace std;

int main()
{
    
    string romNum; 
    string nextRoman; 
    int decimal = 0; 
    
    bool done = false; 
    
    int I_counter = 0; 
    int X_counter = 0;
    int C_counter = 0;
    int M_counter = 0;
    
    int V_counter = 0; 
    int L_counter = 0;
    int D_counter = 0;
    
    while(done == false)
    {
        
            cout << "Input a Roman Number and I will tell you its integer equivalent : " << endl;
            cin >> romNum;
        
            decimal = 0;    
        
            I_counter = 0;
            X_counter = 0;
            C_counter = 0;
            M_counter = 0;
        
            V_counter = 0;
            L_counter = 0;
            D_counter = 0;
        
           for(int i = 0; i < romNum.length(); i++) 
           {
               
               switch(romNum.at(i)) 
               {
                   case 'M': decimal += 1000;   M_counter += 1; break;
                   case 'D': decimal += 500;    D_counter += 1; break;
                   case 'C': decimal += 100;    C_counter += 1; break;
                   case 'L': decimal += 50;     L_counter += 1; break;
                   case 'X': decimal += 10;     X_counter += 1; break;
                   case 'V': decimal += 5;      V_counter += 1; break;
                   case 'I': decimal += 1;      I_counter += 1; break;
                   default : cout << "Not a valid roman number, try again " << endl; break;
                }
               
           }
        
        if(I_counter > 4 || X_counter > 4 || C_counter > 4 || M_counter > 4 || V_counter > 1 || L_counter > 1 || D_counter > 1) 
        {
            cout << "Not a valid roman number, try again " << endl << endl;
        }
        
        else
        {
            cout << "The decimal value of the roman number is " << decimal << endl << endl;
        }
        
        
        cout << "Would you like to enter another number? If yes enter capital Y, if not enter capital N. " << endl << endl;
        cin >> nextRoman;
        
        if(nextRoman == "Y")
        {
            done = false;
        }
        
        else if(nextRoman == "N")
        {
            done = true;
        }
        
    }
    
    return 0;
}
For your first problem (not accounting for the default case, you'd need to make a flag variable which you could then test for in your following if statement.

In regards to the program continually running, you're using an if/else-if statement. This means if neither case ( nextRoman == "Y" or nextRoman == "N" ) evaluates to true, the program will not change the value of done, meaning it remains true, meaning the while condition evaluates to true and keeps running the program. You'd either need to add an else statement for invalid input to fall through too, or remove the second condition and quit the program if the user enters anything else but "Y".
Well. . . how do you account for roman numerals like IX? The I needs to be subtracted from the X, right?

I think you only would ever have 3 I, as in VIII, nine being IX, 3 C as in DCCC, 900 being CM. . .etc.

Topic archived. No new replies allowed.