I've got an assignment in which I've to create a roman numeral to integer converter. As my code stands, I've just used a map to relate characters to integer values, which are added to a sum, accounting for values like IV etc. For strings that do not wholly consist of roman numerals, I've been trying to make it stop adding as soon as a character without a mapped value is found but I'm running into issues.
#include <iostream>
#include <cctype>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[]) {
map<char, int> num;
num.insert({ 'I', 1});
num.insert({ 'V', 5});
num.insert({ 'X', 10});
num.insert({ 'L', 50});
num.insert({ 'C', 100});
num.insert({ 'D', 500});
num.insert({ 'M', 1000});
string str;
while(getline(cin,str)){
int sum = 0;
transform(str.begin(), str.end(), str.begin(), ::toupper);
for (int j = 0; j < str.length(); j++){
if (num.count(str[j]) != 1){
break;
}
if (num[str[j]] < num[str[j+1]]){
sum += num[str[j+1]] - num[str[j]];
j++;
continue;
}
sum += num[str[j]];
}
cout << sum << "\n";
}
return 0;
}
I thought that num.count(str[j]) would return 0 if a mapped value wasn't found, and then I could use that to break the for loop but I'm wrong somewhere. When I give the command line argument of iittii, I would want it to output a sum of 2 (as it would stop when it encounters the first t) but instead I get 4.
Is my implementation incorrect or is there an alternative method to achieveing this?
Thank you