Parsing/Validation help

I am a c++ beginner and am trying to write a program that will take a roman numeral and convert it into an arabic number (arabic numbers are the format of a number we use today). Validation is obviously a key factor in converting a roman numeral. For example - IIII is not a valid input (IV would be correct). I have seen a lot of if/if else statement examples of this program but that is not a very "elegant" way of writing the program and would contain a massive amount of statements.

I would like to search my string to find an invalid input (ex: IIII, VV, MI). I have set up a bool function to return the results - but for some reason I am always returning a false value when something like just "I" is entered. Any suggestions?

Here is a snipet of my code:

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
class RomanNumeral
{
    
private:
    
    string m_inputString;
    int m_value;
public:
    
    // Constructor
    RomanNumeral(string input)
    {
        m_inputString = input;
    }
    
    // Member function to make all the string uppercase letters
    void convertUp ()
    {
        // Convert to all upper case
        transform(m_inputString.begin(), m_inputString.end(), m_inputString.begin(), ::toupper);
    
    }
  bool validate ()
    {
        
        // Check for repeating numerals
        if (m_inputString.find_first_of("IIII") != string::npos)
            {
                cout << "invlaid" << endl;
                return false;
            }
        else if (m_inputString.find_first_of("VV") != string::npos)
            {
                cout << "invlaid" << endl;
                return false;
            }

         // ...etc...

        else 
            return true;

int main()
{
    string inputString;
    
    cout << "Please input your roman numeral string to conver: " << endl;
    getline(cin, inputString);
    
    // Constructor call
    RomanNumeral romanNumeral(inputString);
    
    // Call converter
    romanNumeral.convertUp();
    
    while (romanNumeral.validate() == true)
    {
        cout << "test" << endl;
    }

    cin.get()
    return 0;
}
Last edited on
I don't remember reading anything on Wikipedia about excessive digits in roman numerals being invalid - is this just a requirement of your assignment?

The wikipedia article is actually really helpful, even though it doesn't mention anything about valid or invalid roman numerals.
@L B - thanks again for your help. There are several rules to roman numerals. I think I can put them all into implementation if I can just figure out how to search the string to find if the sequence of characters are present. I have been searching and just came across string::copy - is that the right direction?
I don't think that's the right direction - what does copying have to do with validating string sequences?

try writing down the rules on paper - specifically the things that make roman numerals invalid.
@L B - maybe I am not being clear on the part I am struggling with. I understand what all the things are that make the string invalid - what I can't figure out is how to search for those in the string. It appears that when I use m_inputString.find_first_of("IIII") it is recognizing each "I" as an invalid character instead of evaluating the string until it finds 4 in a row. Does that make sense?
how high can the number be?
If you are looking for a specific sequence I think that is when you are supposed to use a regular expression regex.

http://www.cplusplus.com/reference/regex/basic_regex/basic_regex/
@Everyone - thanks for all of your help. I ended up rewritting my code completely different, although not the way I wanted, in order to turn it in on time and be functioning.

After coming back to my code - I realized that my mistake was quite small. String::Find() was working the way I thought it should - I simply had a statement further down in my code that was find_first_of("IM") that was causing the problem. Since it was find first of - It was recognizing a simply "I" input as invalid.
Last edited on
Topic archived. No new replies allowed.