Thread: help on roman.c

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    help on roman.c

    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);
    }
    sorry about the new thread.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,186
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed
Website Security Test