I'm trying to divide a string into parts for reading Roman numerals. For example if the user enters
"XI"
I want the program to be able to understand that I is 1 and X is 10 in order for a data validation like this to work.
if(string roman == "X") int roman += 10;
etc.
To access an individual character from a string, use square brackets:
int num = 0;
char r = roman[0];
if (r == 'X') {
num += 10;
}
The above is by no means a complete example, but should be enough to get you started. This example looks at the first character in the string roman
(characters are numbered starting at the left with index 0). It checks to see whether the character is 'X'
, and if so, increments the num
variable by 10.