## anonymous 4 years ago C Question: What's wrong with my program? [2]

1. anonymous

This is supposed to be a program that accepts an integer from 1 to 3,999 and converts it to Roman numeral form. Initially the values for the digits I got were correct, but when I added the switch sections of the code I get decimal numbers for the digits. Also, the switches themselves don't seem to be working well as the program can only choose between case 1 and default. I suspect that the error has something to do with how I dealt with the strings. #include <stdio.h> int main (void) { int Num, Digit[4]; char *RThou[5], *RHund[5], *RTens[5], *ROnes[5]; // Digit[3]: Thousands digit | RThou[5]: Thousands digit in Roman numerals // Digit[2]: Hundreds digit | RHund[5]: Hundreds digit in Roman numerals // Digit[1]: Tens digit | RTens[5]: Tens digit in Roman numerals // Digit[0]: Ones digit | ROnes[5]: Ones digit in Roman numerals printf("Input an integer from 1 to 3,999: "); scanf("%d", &Num); if (Num < 1 || Num > 3999) printf("Invalid input.\n"); else { Digit[3] = Num/1000 % 10; Digit[2] = Num/100 % 10; Digit[1] = Num/10 % 10; Digit[0] = Num % 10; switch (Digit[3]) { case 1: RThou[5] = "M"; printf("M"); break; case 2: RThou[5] = "MM"; printf("MM"); break; case 3: RThou[5] = "MMM"; printf("MMM"); break; default: RThou[5] = ""; break; } switch (Digit[2]) { case 1: RHund[5] = "C"; break; case 2: RHund[5] = "CC"; break; case 3: RHund[5] = "CCC"; break; case 4: RHund[5] = "CD"; break; case 5: RHund[5] = "D"; break; case 6: RHund[5] = "DC"; break; case 7: RHund[5] = "DCC"; break; case 8: RHund[5] = "DCCC"; break; case 9: RHund[5] = "CM"; break; default: RHund[5] = ""; break; } switch (Digit[1]) { case 1: RTens[5] = "X"; break; case 2: RTens[5] = "XX"; break; case 3: RTens[5] = "XXX"; break; case 4: RTens[5] = "XL"; break; case 5: RTens[5] = "L"; break; case 6: RTens[5] = "LX"; break; case 7: RTens[5] = "LXX"; break; case 8: RTens[5] = "LXXX"; break; case 9: RTens[5] = "XC"; break; default: RTens[5] = ""; break; } switch (Digit[0]) { case 1: ROnes[5] = "I"; break; case 2: ROnes[5] = "II"; break; case 3: ROnes[5] = "III"; break; case 4: ROnes[5] = "IV"; break; case 5: ROnes[5] = "V"; break; case 6: ROnes[5] = "VI"; break; case 7: ROnes[5] = "VII"; break; case 8: ROnes[5] = "VIII"; break; case 9: ROnes[5] = "IX"; break; default: ROnes[5] = ""; break; } printf("%d %d %d %d\n", Digit[3], Digit[2], Digit[1], Digit[0]); printf("%c %c %c %c\n", *RThou[5], *RHund[5], *RTens[5], *ROnes[5]); } }

2. anonymous

I don't know C but problem is NOT with switch statements printf("%c %c %c %c\n", *RThou[5], *RHund[5], *RTens[5], *ROnes[5]); maybe this prints ONLY 1 character?

3. anonymous

You're accessing memory that's out of the boundaries of your array in ALL of your switch() statements. For example, you are writing RThou[5] even though RThou[] is a char* array of size 5; arrays in C (and other languages like C++, Java, Python, etc) are zero-indexed, which means you can only subscript an array from 0 to array_size-1: i.e. if I declare an array A[10], I can only use A[0], A[1], ..., A[9] and anything else would be undefined behavior.

4. anonymous

that's what i thought, because always 1 char was written

5. anonymous

but in others languages i would use String for it :P

6. anonymous

in C, you could make good use of <string.h> here

7. anonymous

@Tomas.A: Yep, I realized soon enough that changing %c to %s should allow me to print more than 1 character. @agdgdgdgwngo: I'm afraid I'm not sure if I understand you. So if we imagine RThou[5] to look like this: [] [] [] [] [] 0 1 2 3 4 Does it mean in my switch statements, I'm trying to jam all those characters in a slot that does not exist? How can I make them fit into slots 0-4?

8. anonymous

right. try dereferencing valid slots.

9. anonymous

But if that's the case, how come it works for the first 2 digits, and fails for the last 2?

10. anonymous

switch (Digit[3]) { case 1: RThou[5] = "M"; printf("M"); break; case 2: RThou[5] = "MM"; printf("MM"); break; case 3: RThou[5] = "MMM"; printf("MMM"); break; default: RThou[5] = ""; break; } you are using printf statements there.

11. anonymous

Even without the printf statements, the program is able to identify the correct values for the thousands and hundreds digits. Just not the tens and ones digits.

12. anonymous

Okay, it seems that changing Digit[0] to Digit[4] makes the program output the correct value for the ones digit.

13. anonymous

digit[4] doesn't give you a segfault?

14. anonymous

For some weird reason, no.

15. anonymous

post the code on a site like http://ideone.com so we can debug it

16. anonymous
17. anonymous

http://ideone.com/vaYtT works alright O.o

18. anonymous

Ah. The tens digit will always output to 1. '3333' should not work.

19. anonymous

http://ideone.com/2L3MA got rid of all the out of array bounds stuff

20. anonymous

Hmm. Never knew you could just do away with declaring the number of slots in a string. Does that work for arrays of other data types?

21. anonymous

I don't think so :(

22. anonymous

If I *really* wanted to specify the number of slots in the strings, what should I have done?

23. anonymous

you can do lots of stuff, like defining char arrays of specific sizes, malloc(), etc.

24. anonymous

Another question: How come removing the array bounds made the code work?

25. anonymous

It actually works even when you are storing your strings in places outside the bounds of your arrays, since you are operating on the stack.

26. rsmith6559

Since you declared the thousands, hundreds, tens and ones consecutively, when you were going beyond the end of the thousands you could possibly have been putting the thousands value into the hundreds, then the hundreds values into the tens, and sooner or later putting the value into who knows what.