Thread: Convert to Roman Numerals - Program not working, suggest a workaround!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    47

    Convert to Roman Numerals - Program not working, suggest a workaround!

    Code:
    /*
    Write a general-purpose function to convert any given year into its roman equivalent. 
    The following table shows the roman equivalents of decimal numbers:
    
    Decimal:.........Roman
    1................i	
    5................v
    10...............x
    50...............l
    100..............c
    500..............d
    1000.............m
    
    Example:
    Roman equivalent of 1988 is mdcccclxxxviii
    Roman equivalent of 1525 is mdxxv
    
      */
    
    
    #include<stdio.h>
    main()
    {
    	int year;
    	int convert (int year);
    	
    
    	while (1)
    	{
    
    		printf("Note:Enter a four year digit year.\n\n");
    
    		printf("Enter the year that you wanna convert to Roman: " );
    		scanf ("%d", &year);
    
    		if (year> 1999)
    		{
    			printf("Invalid Year.Please enter again.\n\n");
    		}
    	}
    
    	convert(year);
    
    
    }
    
    
    
    convert(int year);
    {
    
    	int i;
    
    	{
    	 i=(year/1000); //thousands place
    	if(i==1)
    	 {
    	 printf("m");
    	 }
    
    
    	 i=(year%1000)-(year/1000); //hundreds place
    	 switch (i)
    	{
     	case 1:
    	printf("c");
    	break;
    	
    	case 2:
    	printf("cc");
    	break;
    	
    	case 3:
    	printf("ccc");
    	break;
    	
    	case 4:
    	printf("cd");
    	break;
    	
    	case 5:
    	printf("d");
    	break;
    	
    	case 6:
    	printf("dc");
    	break;
    	
    	case 7:
    	printf("dcc");
    	break;
    	
    	case 8:
    	printf("dccc");
    	break;
    	
    	case 9:
    	printf("dcccc");
    	break;
    	
    	}
    
    
    
    	i=(year-((year/100)*100))/10; //tens place
    	switch(i)
    	{
    	case 1:
     	printf("x");
     	break;
    
    	case 2:
     	printf("xx");
     	break;
    
    	case 3:
     	printf("xxx");
        break;
    
    	 case 4:
    	printf("xl");
    	break;
    
    	case 5:
    	printf("x");
    	break;
    
    	case 6:
    	printf("xl");
    	break;
    
    	case 7:
    	printf("xll");
    	break;
    
    	 case 8:
    	printf("xlll");
    	break;
    
    	case 9:
    	printf("xllll");
    	break;
    
    	}
    
    
    
        i=year%10; //ones place
    	 switch(i);
    	 {
    	 case 1:
    	 printf("i");
         break;
    
    	 case 2:
    	 printf("ii");
       	break;
    
    	 case 3:
    	printf("iii");
    	break;
    
    	 case 4:
    	printf("iv");
    	break;
    
    	case 5:
    	printf("v");
    	break;
    
    	 case 6:
    	printf("vi");
    	break;
    
    	case 7:
    	printf("vii");
    	break;
    
    	case 8:
    	printf("viii");
    	break;
    
    	 case 9:
    	printf("ix");
    	break;
    	 }
    }
    
    return 0;
    
    }


    The program looks too big, but its just cuz of the Case Statements.

    So..can anybody suggest a workaround?

    THe program doesn't work!
    It gives some error related to the brackets of the 'convert' function! I can't debug it!

    Any ideas fellas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > convert(int year);
    Remove that ;

    If you'd started with
    Code:
    convert(int year);
    {
    }
    Then pressed compile, you would only have 3 lines to consider, and you'd probably figure it out.

    > The program looks too big,
    All those cases look just like array indices
    Code:
    char *hundreds[] = { "c", "cc", "ccc", "cd" };
    printf( "%s", hundreds[pos] );
    > printf("dcccc");
    All your 9's are wrong, this should be "cm" for example.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    47
    >printf("dcccc");
    All your 9's are wrong, this should be "cm" for example.
    [/quote]
    Write a general-purpose function to convert any given year into its roman equivalent.
    The following table shows the roman equivalents of decimal numbers:

    Decimal:.........Roman
    1................i
    5................v
    10...............x
    50...............l
    100..............c
    500..............d
    1000.............m

    Example:
    Roman equivalent of 1988 is mdcccclxxxviii
    Roman equivalent of 1525 is mdxxv
    [/quote]

    Absolutely!
    I was confused too! I just took a look at the example in the question and figured out that the 9 would be dcccc.
    But logically it should have been cm!


    Also, I have not yet reached the point of arrays. So it must be okay..to go ahead with this program which looks fine.


    And the semicolon after the function --> convert (int year); ...was the most lousy mistake of all times!!! hehe
    Last edited by duffmckagan; 08-20-2006 at 08:30 PM.

  4. #4
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    you got that one right duffmckagan... maybe you copied the function prototype and forgot to remove the ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to convert numbers into words
    By Kingsley in forum C Programming
    Replies: 5
    Last Post: 07-01-2006, 07:50 PM
  2. Program was working good until.....
    By cazil in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2002, 10:53 PM
  3. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM
  4. Program logic not working..
    By ronkane in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2002, 08:31 PM
  5. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM