![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #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 | |
| | #2 |
| and the hat of vanishing 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);
{
}
> The program looks too big, All those cases look just like array indices Code: char *hundreds[] = { "c", "cc", "ccc", "cd" };
printf( "%s", hundreds[pos] );
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 | |
| | #3 | |
| Registered User Join Date: Jan 2006
Posts: 47
| 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |