Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program is supposed to convert roman numbers to digits. The problem in my co

ID: 3847576 • 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

Thousands Hundreds Tens Units 100 c 2000 200 CC 20 XX 3000 MMM 300 CCC 30 XXX 3 III 400 CD 500 -D 600 DC 60 1.x 700 DCC 70 LXX 7

Explanation / Answer

The mistake you made is you are breaking from loop whenever you find a string like 'XC' OR 'CD' OR 'CM' OR 'MU'.

Why are U doing this? Do U think there is no a roman number like 'XCV'=95 OR 'XCIX'=99 OR 'CDVIII'=408 OR 'CDXXXVI'=436 OR 'CML'=950 OR 'CMXLVII'=947 OR 'MUDCC'=4700.

I made changes at break statements and added 'i++' statements in your code.

you can find the changes I made. I comment these statements like '//my change' and '//end here'

#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;
               //my change
               //break;
               i++;
               //end here
           }
           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;
               //my change
               //break;
              
               i++;
               //end here
           }
           else if(roman[i+1]=='M')
           {
               result+=900;
               //my change
               //break;
               i++;
               //end here
           }
          
           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;
               //my change
               //break;
               i++;
               //end here
           }

       }
       //end of the last if statement --------------------------------------------
       else
       {
           printf("Error sai");
           break;
       }

   }// end of the loop
   if(result!=0)
   printf("%d",result);
  
   return 0;
}// end of the code