The program is supposed to convert roman numbers to digits. The problem in my co
ID: 3847575 • Letter: T
Question
The program is supposed to convert roman numbers to digits. The problem in my code is that when the user starts with (XC, CM, or MU) the loop breaks and won't read the rest of the input. for example (XCV should print 98) but it only reads reads XC (90). I tried to use "continue" but it didn't work. Please explain what have I done wrong
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char roman[20];
int result = 0;
printf("Enter a roman numeral: ");
scanf("%s", roman);
int length = 0;
int i = 0;
while(roman[i]!='')
{
roman[i]=toupper(roman[i]);
i++;
}
length = i;
printf("%d ",length);
for(i=0;i<length;i++)
{
if(roman[i]=='I')// if you get an I ,look for a V or one or two more I ,otherwise its error
{
if(i+3 < length)
{
printf("Error I");
break;
}
else if(roman[i+1]==''|| (roman[i+1]=='I'))//one ,two or three
{
result+=1;
}
else if(roman[i+1]=='V')
{
result+=4; break;
}
else if(roman[i+1]=='X')
{
result+=9;break;
}
else
{
printf("Error I2");
break;
}
}
else if(roman[i]=='V')
{
if( i+4 < length)//worst possible is VIII, not more than that
{
printf("Error V");
break;
}
else if(roman[i+1]==''|| (roman[i+1]=='I'&& roman[i+2]=='')|| (roman[i+1]=='I'&&roman[i+2]=='I'))//one ,two or three
{
result+=5;
}
else
{
printf("Error V2");
break;
}
}
else if(roman[i]=='X')
{
if(roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=10;
}
else if(roman[i+1]=='L')
{
result+=40;
}
else if(roman[i+1]=='C')
{
result+=90; break;
}
else
{
printf("Error X");
break;
}
}
else if(roman[i]=='L')
{
if(i+16 < length) //fix me
{
printf("Error L");
break;
}
else if(roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=50;
}
else
{
printf("Error L2");//fix me
break;
}
}
else if(roman[i]=='C')
{
if(i+11 < length)
{
printf("Error C1");//fix me
break;
}
else if(roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=100;
}
else if(roman[i+1]=='D')
{
result+=400; break;
}
else if(roman[i+1]=='M')
{
result+=900; break;
}
else
{
printf("Error C2");//fix me
break;
}
}
else if(roman[i]=='D')
{
if(i+12 < length)
{
printf("Error D1");//fix me
break;
}
else if(roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=500;
}
}
else if(roman[i]=='M')
{
if(i+16 < length)
{
printf("Error M1"); //fix me
break;
}
else if(roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=1000;
}
else if (roman[i+1] == 'U'){
result +=4000; break;
}
}
//end of the last if statement --------------------------------------------
else
{
printf("Error");
break;
}
}// end of the loop
if(result!=0)
printf("%d",result);
return 0;
}// end of the code
Explanation / Answer
The corrected code is given below:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char roman[20];
long result = 0;
printf("Enter a roman numeral: ");
scanf("%s", roman);
int length = 0;
int i = 0;
while(roman[i]!='')
{
roman[i]=toupper(roman[i]);
i++;
}
length = i;
printf("%d ",length);
for(i=0;i<length;i++)
{
if(roman[i]=='I')// if you get an I ,look for a V or one or two more I ,otherwise its error
{
if(i+3 < length)
{
printf("Error I");
break;
}
else if(roman[i+1]==''|| (roman[i+1]=='I'))//one ,two or three
{
result+=1;
}
else if(roman[i+1]=='V')
{
result+=4; break;
}
else if(roman[i+1]=='X')
{
result+=9;break;
}
else
{
printf("Error I2");
break;
}
}
else if(roman[i]=='V')
{
if( i+4 < length)//worst possible is VIII, not more than that
{
printf("Error V");
break;
}
else if(roman[i+1]==''|| (roman[i+1]=='I'&& roman[i+2]=='')|| (roman[i+1]=='I'&&roman[i+2]=='I'&&roman[i+3]=='')||(roman[i+1]=='I'&&roman[i+2]=='I'&&roman[i+3]=='I'))//one ,two or three
{
result+=5;
}
else
{
printf("Error V2");
break;
}
}
else if(roman[i]=='X')
{
if(roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=10;
}
else if(roman[i+1]=='L')
{
result+=40;++i;
}
else if(roman[i+1]=='C')
{
result+=90;++i;
}
else
{
printf("Error X");
break;
}
}
else if(roman[i]=='L')
{
if(i!=0 &&result<=50) //fix me
{
printf("Error L");
break;
}
else if(roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=50;
}
else
{
printf("Error L2");//fix me
break;
}
}
else if(roman[i]=='C')
{
if(i!=0 &&result<100)
{
printf("Error C1");//fix me
break;
}
else if(roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=100;
}
else if(roman[i+1]=='D')
{
result+=400;++i;
}
else if(roman[i+1]=='M')
{
result+=900; ++i;
}
else
{
printf("Error C2");//fix me
break;
}
}
else if(roman[i]=='D')
{
if(i!=0 &&result<=500)
{
printf("Error D1");//fix me
break;
}
else if(roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=500;
}
else
{
printf("Error D2");
break;
}
}
else if(roman[i]=='M')
{
if(i!=0 &&result<1000)
{
printf("Error M1"); //fix me
break;
}
else if(roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=1000;
}
else if (roman[i+1] == 'U'){
result +=4000;++i;
}
}
else if(roman[i]=='U')
{
if(i!=0 &&result<=5000)
{
printf("Error U");
break;
}
else if(roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=5000;
}
else
{
printf("Error U2");
break;
}
}
else if(roman[i]=='Y')
{
if(i!=0 && result<10000)
{
printf("Error Y");
break;
}
else if(roman[i+1]=='B')
{
result+=90000;
++i;
}
else if(roman[i+1]=='K')
{
result+=40000;
++i;
}
else if(roman[i+1]=='Y'||roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
result+=10000;
else
{
printf("Error Y2");
break;
}
}
else if(roman[i]=='K')
{
if(i!=0 && result<=50000)
{
printf("Error K");
break;
}
else if(roman[i+1]=='U'||roman[i+1]=='Y'||roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=50000;
}
else
{
printf("Error K2");
break;
}
}
else if(roman[i]=='B')
{
if(i!=0 &&result<100000)
{
printf("Error B");
break;
}
else if(roman[i+1]=='N')
{
result+=900000;
++i;
}
else if(roman[i+1]=='E')
{
result+=400000;
++i;
}
else if(roman[i+1]=='B'||roman[i+1]=='U'||roman[i+1]=='Y'||roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
result+=100000;
else
{
printf("Error B2");
break;
}
}
else if(roman[i]=='E')
{
if(i!=0 &&result<=500000)
{
printf("Error E");
break;
}
else if(roman[i+1]=='K'||roman[i+1]=='B'||roman[i+1]=='U'||roman[i+1]=='Y'||roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
{
result+=500000;
}
else
{
printf("Error E2");
break;
}
}
else if(roman[i]=='N')
{
if(i!=0 &&result<1000000)
{
printf("Error N");
break;
}
else if(roman[i+1]=='N'||roman[i+1]=='E'||roman[i+1]=='K'||roman[i+1]=='B'||roman[i+1]=='U'||roman[i+1]=='Y'||roman[i+1]=='M'||roman[i+1]=='D'||roman[i+1]=='C'||roman[i+1]=='L'||roman[i+1]==''||roman[i+1]=='X'|| roman[i+1]=='V'|| roman[i+1]=='I')
result+=1000000;
else
{
printf("Error N2");
break;
}
}
//end of the last if statement --------------------------------------------
else
{
printf("Error");
break;
}
}// end of the loop
if(i==length)
printf("%ld",result);
return 0;
}// end of the code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.