hk3008 said:
ok I am baffle maybe I am in the wrong class but I have
#include <string>
#include <iostream>
using namespace std;
std string numerals = "1 5 10 50 100 500 1000";
int main()
{
int roman_numeral = 0;
char arabic_numeral;
cout << "Enter the Number : ";
while(cin.get(roman_Numeral))
{
if(arabic_Numeral == '1000')
roman_Numeral = roman_Numeral + M;
else if(arabic_Numeral == '500')
{
arabic_Numeral = cin.peek();
if(numerals.find(arabic_Numeral, 5) != std::string::npos)
{
roman_Numeral = roman_Numeral - D;
continue;
}
else
{
roman_Numeral = roman_Numeral + D;
continue;
}
}
else if(arabic_Numeral == '100')
{
arabic_Numeral = cin.peek();
if(numerals.find(arabic_Numeral, 4) != std::string::npos)
{
roman_Numeral = roman_Numeral - C;
continue;
}
else
{
roman_Numeral = roman_Numeral + C;
continue;
}
}
else if(arabic_numeral == '50')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 3) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - L;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + L;
continue;
}
}
else if(arabic_Numeral == '10')
{
roman_Numeral = cin.peek();
if(numerals.find(arabic_Numeral, 2) != std::string::npos)
{
roman_Numeral = roman_Numeral - X;
continue;
}
else
{
roman_Numeral = roman_Numeral + X;
continue;
}
}
else if(arabic_Numeral == '5')
{
roman_Numeral = cin.peek();
if(numerals.find(arabic_Numeral, 1) != std::string::npos)
{
roman_Numeral = roman_Numeral - V;
continue;
}
else
{
roman_Numeral = roman_Numeral + V;
continue;
}
}
else if(arabic_Numeral == '1')
{
arabic_Numeral = cin.peek();
if(numerals.find(arabic_Numeral) != std::string::npos)
{
roman_Numeral = roman_Numeral - I;
continue;
}
else
{
roman_Numeral = roman_Numeral + I;
continue;
}
}
else
break;
}
cout << roman_Numeral << endl;
return 0;
}
so far but it is not working I definitly need help with this if anyone has any tips or can maybe spot the error I dont know if the math is wrong on it or maybe you cant do the algorithms like you can with arabic numbers
A lot of things is wrong here:
1. you need a char array or a string, not a single char or worst an int for a roman number
2. you can't add or do any operation with another not initialized and worst of all unspecified variable
3. do you even know how this algorithm works? because you are way off with this implementation. Here I can't even tell what are you converting to what...
4. why do you need iostream and numerals string?
Couple of tips:
1. define the variables correct(there is no way to make it the way you started!)
2. handle user input correct (put it all into a single variable so you can work with it!)
3. work out the algorithm on paper first as a workflow diagram. try it for yourself to convert an arabic number to roman