How would I put this together in a main function?

#include <iostream>
#include <string>

using namespace std;

int convertRomanToInt(string input);
int romanCharValue(char r);

int main()
{
cout << "Please enter a roman number or 'Q' to quit." << endl;

}
int convertRomanToInt(string input)
{
int total = 0;
int value = 0;
string romanNumerals = "IVXLCDM";
char numeral[7] = { 'I','V','X','L','C','D','M' };

for (int i = 0; i < input.length(); i++)
{
for (int j = 0; j < romanNumerals.length(); j++)
{
if (input.substr(i, 1) == romanNumerals.substr(j, 1))
{
for (int k = 0; k < romanNumerals.length();k++)
{
if (input.substr((i + 1), 1) == romanNumerals.substr(k, 1))
{
value = romanCharValue(numeral[k]);
}
}
if (romanCharValue(numeral[j]) < value)
{
total -= romanCharValue(numeral[j]);
}
else if (romanCharValue(numeral[j]) >= value)
{
total += romanCharValue(numeral[j]);
}
}
}
}
return total;
}
int romanCharValue(char r)
{
switch (r)
{
case 'I': {return 1; }
case 'V': {return 5; }
case 'X': {return 10; }
case 'L': {return 50; }
case 'C': {return 100; }
case 'D': {return 500; }
case 'M': {return 1000; }
default: "error";
}
}
1
2
3
4
5
string romanNumeral;
cin >> romanNumeral;
// {error checking here, if applicable}
int value = convertRomanToInt(romanNumeral);
cout << value << '\n';
Last edited on
Thank you!
Since the OP deletes their questions after getting answers:

maulk wrote:

#include <iostream>
#include <string>

using namespace std;

int convertRomanToInt(string input);
int romanCharValue(char r);

int main()
{
cout << "Please enter a roman number or 'Q' to quit." << endl;

}
int convertRomanToInt(string input)
{
int total = 0;
int value = 0;
string romanNumerals = "IVXLCDM";
char numeral[7] = { 'I','V','X','L','C','D','M' };

for (int i = 0; i < input.length(); i++)
{
for (int j = 0; j < romanNumerals.length(); j++)
{
if (input.substr(i, 1) == romanNumerals.substr(j, 1))
{
for (int k = 0; k < romanNumerals.length();k++)
{
if (input.substr((i + 1), 1) == romanNumerals.substr(k, 1))
{
value = romanCharValue(numeral[k]);
}
}
if (romanCharValue(numeral[j]) < value)
{
total -= romanCharValue(numeral[j]);
}
else if (romanCharValue(numeral[j]) >= value)
{
total += romanCharValue(numeral[j]);
}
}
}
}
return total;
}
int romanCharValue(char r)
{
switch (r)
{
case 'I': {return 1; }
case 'V': {return 5; }
case 'X': {return 10; }
case 'L': {return 50; }
case 'C': {return 100; }
case 'D': {return 500; }
case 'M': {return 1000; }
default: "error";
}
}
Not sure what you mean by delete post I can see it perfectly fine.
@CodeGoggles,

he probably meant http://www.cplusplus.com/forum/beginner/273708/
Yeah, they've done it on other threads, so I thought I'd make sure the OP was preserved on this one, in case they do it here too.
Registered users can post here. Sign in or register to post.