how to directly convert char to numbers?

How do I fix my C program to Convert numbers to Roman numerals?

  • Having trouble printing the numbers 900, 500 and much more. It gives me the wrong roman numerals. source code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ int i, n; char str[4]; char roman[20]=""; printf("Enter a number (between 1 and 3999): "); scanf("%s", str); printf("Digits of the number: "); for(i=0; i<=3; ++i) { printf("%c,", str[i]); } n = atoi(str); printf("\nThe number %i as Roman numeral: ",n); if (n/1000>=1) for(i=0;i<n/1000;i++) strcat(roman,"M"); n=n-(1000*(n/1000)); if (n/500>=1) { if(n>=900) strcat(roman,"CM"); else for(i=0;i<n/500;i++) strcat(roman,"D"); } n=n-(500*(n/500)); if (n/100>=1) { if(n>=400) strcat(roman,"CD"); else for(i=0;i<n/100;i++) strcat(roman,"C"); } n=n-(100*(n/100)); if (n/50>=1) { if(n>=90) strcat(roman,"XC"); else for(i=0;i<n/50;i++) strcat(roman,"L"); } n=n-(50*(n/50)); if (n/10>=1) { if(n>=40) strcat(roman,"XL"); else for(i=0;i<n/10;i++) strcat(roman,"X"); } n=n-(10*(n/10)); if (n/5>=1) { if(n>=9) strcat(roman,"IX"); else for(i=0;i<n/5;i++) strcat(roman,"V"); } n=n-(5*(n/5)); if(n>=4) strcat(roman,"IV"); else for(i=0;i<n/1;i++) strcat(roman,"I"); strcat(roman,"\0"); printf("%s",roman); printf("\n"); return 0; }

  • Answer:

    Basically because your maths is wrong! You correctly handle the thousands but then start going wrong. Your next check is for 500's (D) but you start by checking for 900. You subtract a value based on the number of 500's found which means that instead of 999 you end up with 499. You repeat the same mistake in each subsequent check for 400, 90, 40 , 9 and finally 4. rewrite the code along the following lines PSEUDO CODE (FRAGMENT) IF (n IS GREATER THAN OR EQUAL TO 500) ....IF ( n IS GREATER THAN OR EQUAL TO 900) ........CONCATENATE "CM" to roman .......n = n - 900 ....ELSE ........x = n/500 ........LOOP FROM i=0 TO i LESS THAN x ............CONCATENATE "D" to roman ...........n = n - 500; ........END LOOP ....END IF END IF That same code fragment is repeated several times with different values it could ( i.e. SHOULD) be made into a function call by the use of arguments. You should also look at reducing the number of "forced" calculations you are making, calculate once and reuse, not each time. As a side note you declare str as size 4, this isn't actually big enough for a 4 digit string, you really need the trailing 0. Plus you don't actually check if the user has entered a value higher than 3999. You would be better off entering it as a direct integer value not a string variable.

Nicholas at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.