Question
C++ Roman Numeral to int question. #include #include #include using namespace std; int main() { const int I = 1, V = 5, X =
C++ Roman Numeral to int question.
#include #include #include
using namespace std;
int main() {
const int I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000; //Sets up the values for the conversion.
cout
string s;
string convertRomanToDecimal(string s); // Required 1 string convertDecimalToRoman(string s); // Requred 2
int romNumberAdd = 0; //Initialize this here. int romInt = 0, nextInt = 0; //Also initializing this early.
cin >> s;
if (s == "0" || s == "O") { cout
if (s != "0" && s != "O") //Tests to see if it is a valid non 0 or O number. { int strLength = s.length(); // This counts the length of the player inputted string. int strArray[1000]; //Not optimal, but this would be as long as any sane person would try to type in. A number overflow would likely happen before someone exceeds this.
for (int i = 0; i
s[i] = toupper(s[i]); //Makes everything uppercase. if (s[i] != 'M' && s[i] != 'D' && s[i] != 'C' && s[i] != 'L' && s[i] != 'X' && s[i] != 'V' && s[i] != 'I') //Checks proper letters. { cout
//Below counts the actual letters. Places them into an array to count for the next step. if (s[i] == 'M') //Adding up one thousand. strArray[i] = 1000;
if (s[i] == 'D') //Adding up five hundred. strArray[i] = D;
if (s[i] == 'C') //Adding up one hundred. strArray[i] = C;
if (s[i] == 'L') //Adding up fifties. strArray[i] = L;
if (s[i] == 'X') //Adding up tens. strArray[i] = X;
if (s[i] == 'V') //Adding up fives. strArray[i] = V;
if (s[i] == 'I') //Adding up ones. strArray[i] = I; }
for (int j = 0; j
if (j
}
cout
I've got the requirements in the first for loop, with each value being added to the array "strArray", but I'm having trouble adding the differences of the symbols to equal the correct value. IV either equals 6, or a random number when I've tried it. Any help would be appreciated. The bolded portion is where it'd need to be changed I think.
HOW TO PARSE ROMAN NUMBERS There is no standard set of rules for parsing Roman Numbers, so we will establish our own. The only symbols we will allow are M (1000), D (500), C (100), L (50), X (10), V (5), and I (1). Note that we accept both upper and lower case. Set the accumulator to zero. Parse from left to right. If the symbol you see is followed by a symbol of equal or lesser value o Consume that symbol and add that value to the accumulator If the symbol you see is followed by a symbol of greater value o Consume both symbols and add their difference to the accumulator Continue until the string has been consumed. If any value produced is greater than the preceding value, the Roman Number is invalidStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started