# Progreamming Converting Arabic numbers to Roman

Tags:

Last response: in Applications

hk3008

February 18, 2012 5:10:36 PM

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

#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

More about : progreamming converting arabic numbers roman

theDanijel

February 19, 2012 4:37:01 PM

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

hk3008

February 20, 2012 5:42:39 AM

#include <string>

#include <iostream>

using namespace std;

std::string numerals = "VXLCDM";

int main()

{

char roman_Numeral;

int arabic_Numeral = 0;

cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";

while(cin.get(roman_Numeral))

{

if(roman_Numeral == 'M')

arabic_Numeral = arabic_Numeral + 1000;

else if(roman_Numeral == 'D')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral, 5) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 500;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 500;

continue;

}

}

else if(roman_Numeral == 'C')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral, 4) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 100;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 100;

continue;

}

}

else if(roman_Numeral == 'L')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral, 3) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 50;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 50;

continue;

}

}

else if(roman_Numeral == 'X')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral, 2) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 10;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 10;

continue;

}

}

else if(roman_Numeral == 'V')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral, 1) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 5;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 5;

continue;

}

}

else if(roman_Numeral == 'I')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 1;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 1;

continue;

}

}

else

break;

}

cout << arabic_Numeral << endl;

return 0;

}

which apperently I converted incorecctly,

Related resources

- Movie based on islam - Forum
- Ask any thing about.........? - Forum
- What do you guys know about islam - Forum
- font/unicode question - Forum
- font/unicode question - Forum

hk3008

February 20, 2012 5:44:18 AM

theDanijel

February 20, 2012 7:43:47 AM

This last code will compile, but are you sure it's correct? As I see it, the alorithm is not correct (example it would not correctly convert CM to 900 or VC or IX).

I suggest you input the whole number before the conversion. Arabic number read to an int (cin>>arabic_Numeral;) and roman to a string(string roman_Numeral;cin>>roman_Numeral;), that way it will be easier to manipulate.

hk3008

March 1, 2012 1:02:34 AM

theDanijel

March 1, 2012 6:59:13 AM

hk3008 said:

ok so maybe the smilies are throwing me off wanna give me a small example I tried to play around putting cin >> arabic_Numeral; in a few places but cant seem to get it to work as well with my coding that I have it ask for the roman numeral but after you put it in and hit enter it just closes dos out so I am lostI forgot that instead of ; and ) you got smilies. First of all, cin>>arabic_Numeral goes at only one place. That is after you ask for input. Try something like this:

std::string roman_Numeral;

int arabic_Numeral = 0;

while(1){

cout << "Enter the number you want to convert to roman (0 to exit) : ";

cin>>arabic_Numeral

if(arabic_Numeral==0) break;

//your conversion code here

cout<<"The roman numeral for that number is: "+roman_Numeral;

}

The code you have to convert arabic to roman has flaws. Try rewriteing you code a different way. Don't start from "M", but work your way to it from "I". In other words, start from the last digit in the arabic numeral and convert it one digit at a time.

P.S. I could post you the entire code, but that would be doing your work for you, but this way you will learn to do it yourself.

hk3008

March 1, 2012 8:35:13 PM

#include <string>

#include <iostream>

using namespace std;

int main()

{

char roman_Numeral;

std::string roman_Numeral;

int arabic_Numeral = 0;

while(cin.get(roman_Numeral))

{

cout << "Enter the number you want to convert to roman (0 to exit) : ";

cin >> arabic_Numeral;

if(arabic_Numeral==0) break;

{

if(roman_Numeral == 'I')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 1;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 1;

continue;

}

}

are you saying that Ineed to get rid of the cin.get and cin.peek?

or rewrite the algorythm for the

if(roman_Numeral == 'I')

{

roman_Numeral = cin.peek();

if(numerals.find(roman_Numeral) != std::string::npos)

{

arabic_Numeral = arabic_Numeral - 1;

continue;

}

else

{

arabic_Numeral = arabic_Numeral + 1;

continue;

}

}

hk3008

March 1, 2012 8:36:32 PM

Best solution

theDanijel

March 1, 2012 10:42:17 PM

Share

This would be my code for converting arabic to roman:

#include<string>

#include <iostream>

using namespace std;

int main()

{

string roman_Numeral;

int arabic_Numeral;

string numerals = "IVXLCDM"; //roman numerals

int index; //index for steping throu top string

int digit; //variable for digit conversion

string roman_digit;

while(1){ //endless loop, good for seing all the input and brakeing when input is not a positive number

cout << "Enter the number you want to convert to roman (0 or negative to exit) : ";

cin>>arabic_Numeral

if(arabic_Numeral<1) break;

roman_Numeral="";

index=0;

while(arabic_Numeral!=0){ //the algoritym for conversion

digit=arabic_Numeral%10; //take the last digit

arabic_Numeral/=10; //leave all the other digits

switch(digit){ //convert digit here depending on the index we know in what range the number is.

case 0:

roman_digit="";

break;

case 1:

roman_digit=numerals[index];

break;

case 2:

roman_dgigit=numerals[index]+numerals[index];

break;

case 3:

roman_digit=numerals[index]+numerals[index]+numerals[index];

break;

case 4:

roman_digit=numerals[index]+numerals[index+1];

break;

case 5:

roman_digit=numerals[index+1];

break;

case 6:

roman_digit=numerals[index+1]+numerals[index];

break;

case 7:

roman_digit=numerals[index+1]+numerals[index]+numerals[index];

break;

case 8:

roman_digit=numerals[index+1]+numerals[index]+numerals[index]+numerals[index];

break;

case 9:

roman_digit=numerals[index]+numerals[index+2];

break;

default:

roman_digit="";

break;

}

roman_Numeral=roman_digit+roman_Numeral; //add the new digit to the previously calculated ones

index+=2; //increes the index to know where to look in the roman digit string

if(index>5){ //only will activate if number is greater than 1000

while(arabic_Numeral!=0){ //all else is just adding the correct count of 'M' in front.

roman_Numeral="M"+roman_Numeral;

arabic_Numeral--;

}

}

}

cout<<"Your roman numeral is: "+roman_Numeral;

}

return 1;

}

Even if it seems like a simple conversion, you must break the process into logical parts. You can allmost never reuse the same code for backward conversion, so you will allways have to make a new algorythm (code).

hk3008

March 8, 2012 7:28:22 PM

1>Rewrite.cpp(19): error C2143: syntax error : missing ';' before 'if'

1>Rewrite.cpp(34): error C2065: 'roman_dgigit' : undeclared identifier

but that was it and I really appreciate the explanations it will make rewriting a lot more comprehendable thank you

theDanijel

March 8, 2012 9:13:49 PM

hk3008 said:

ok I understand a little better especially about the backwards conversion and having to rewrite the algorythm only small issue is when I tried to run it to see how it would go it had some simple debugging wrrors I tried to find them but no luck they seemed really simple to fix1>Rewrite.cpp(19): error C2143: syntax error : missing ';' before 'if'

1>Rewrite.cpp(34): error C2065: 'roman_dgigit' : undeclared identifier

but that was it and I really appreciate the explanations it will make rewriting a lot more comprehendable thank you

the errors you get are from my mistyping in the forum, so just add the ';' and the second has one 'g' too many

hk3008

March 17, 2012 8:40:51 PM

Related resources :

- Forum"Come and Take Them!" - "300" Reviewed
- ForumIntelligent Design
- Forum.......?HOW?
- ForumMuslim Brotherhood Takes Elections by Storm
- ForumMost beautiful month in the year(Ramadan)
- ForumVga version : 5 01 2600 0000 (english)
- ForumWhat is the difference between Extended and normal version of a software?
- ForumConverting AVI on a Cruz Reader
- ForumTyping letters come out as numbers in games
- Forum[Solved] Question about converting video to DVD
- ForumQuestion about converting media to dvd win7
- Forum[Solved] Question about converting pdf to word document
- ForumConverting to DVD, gpu accelerated?
- Forumpst to csv converting
- ForumConverting MKV to Mp4
- More resources

Read discussions in other Applications categories

!