C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-20-2006, 12:06 PM   #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?
__________________
C Programs
duffmckagan is offline   Reply With Quote
Old 08-20-2006, 12:31 PM   #2
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 08-20-2006, 08:27 PM   #3
Registered User
 
Join Date: Jan 2006
Posts: 47
Quote:
>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
__________________
C Programs

Last edited by duffmckagan; 08-20-2006 at 08:30 PM.
duffmckagan is offline   Reply With Quote
Old 08-21-2006, 06:42 AM   #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 ;
sangken is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:50 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22