hi, how can i get this program to function properly. if "IX" is entered, i get 11 instead of 9. i wanted to write an equation where if the one letter was smaller than the next (IX), then subtract 2 times the value of the first. how can i incorporate that into my program? thanks
Code:
#include <stdio.h>
int main(int argc, char* argv[])
{
int ctr,sum=0,count;
char caro[11];
for (count=0; argv[1][count] != '\0' && count < 11; count++)
{
caro[count] = argv[1][count];
switch(caro[count])
{
case 'M':
sum += 1000;
break;
case 'D':
sum += 500;
break;
case 'C':
sum += 100;
break;
case 'L':
sum += 50;
break;
case 'X':
sum += 10;
break;
case 'V':
sum += 5;
break;
case 'I':
sum += 1;
break;
default:
printf("Illegal letter encountered!\n");
break;
}
}
/*
for(ctr = 0; ctr < 11; ctr++)
{
if(caro[ctr] < caro[ctr+1])
sum -= (2*caro[ctr]);
}
*/
if(argc > 2)
printf("Usage: roman <numeral>\n");
else
printf("The decimal equivalent of %s is %d\n", argv[1], sum);
}