Thread: Help with roman numeral calculator

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Help with roman numeral calculator

    So I am trying to write a program that converts roman numerals into decimal numbers.

    I so far have come up with:

    Code:
    #include <stdio.h>
    #include <ctype.h> // importing the tolowerfunction
    //variables
    int decimal, total;
    char numeral[];
    
    
    
    
    
    
    //prototypes
    int decimal_to_roman(char input);
    int roman_to_decimal(char letter);
    
    
    
    
    int roman_str_to_decimal(char input)
    {
        int i;
    
    
        for (i=0; i< strlen(input); i++)
        {
             total += roman_to_decimal(numeral[i]);
    
    
    
    
        }
    
    
    }
    
    
    int roman_to_decimal(char letter)
    {
        switch(tolower(letter))
        {
            case 'm':
                return 1000;
            case 'd':
                return 500;
            case 'c':
                return 100;
            case 'l':
                return 50;
            case 'x':
                return 10;
            case 'v':
                return 5;
            case 'i':
                return 1;
            default:
                break;
        }
    }
    
    
    
    
    int main(void)
    
    
    {
    
    
    
    
    
    
    printf("Please enter the first number: ");
    scanf("%s", &numeral);
    
    
    roman_str_to_decimal(numeral);
    
    
    printf("%d\n", total);
    
    
    
    
    //printf("%c\n", numeral[2]);
    
    
    
    
    }
    but each time I compile it, it times out as if it were hitting an infinite loop. I have a feeling that I am not passing an individual character to the roman_to_decimal function but am unsure why.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) The declaration "char numeral[]" does not specify a string of arbitrary length, but your main() function treats it as if it is in the scanf() call.

    2) strlen() is specified in <string.h>, and you have not #include'd that header file.

    3) roman_str_to_decimal() is treating a single char as if it was an array of char. It is not.

    4) You need to compile with maximum warnings. In other words, get your compiler to issue as many diagnostics as possible. Your compile would have picked up most the problems above.

    5) Roman values do not work only left to right. IX is the value 9. XI is the value 11. Your code will not recognise the difference between them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Quote Originally Posted by grumpy View Post
    1) The declaration "char numeral[]" does not specify a string of arbitrary length, but your main() function treats it as if it is in the scanf() call.

    2) strlen() is specified in <string.h>, and you have not #include'd that header file.

    3) roman_str_to_decimal() is treating a single char as if it was an array of char. It is not.

    4) You need to compile with maximum warnings. In other words, get your compiler to issue as many diagnostics as possible. Your compile would have picked up most the problems above.

    5) Roman values do not work only left to right. IX is the value 9. XI is the value 11. Your code will not recognise the difference between them.
    Thanks for the reply.

    In this example, I am trying to pass it a string such as MXII and not a single char and then have the roman_str_to_decimal() take the nth index of the input and send it through the other function to generate the value for total.

    I am just having problems sending an individual letter to my switch statement.
    Last edited by arkine; 10-11-2013 at 10:10 AM.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Code:
    #include <stdio.h>
    #include <ctype.h> // importing the tolowerfunction
    #include <string.h>// for the strlen()
    //variables
    int decimal, total;
    char numeral[10];
    
    
    
    
    
    
    //prototypes
    int decimal_to_roman(char input);
    int roman_to_decimal(char letter);
    int roman_str_to_decimal(int input);
    
    
    
    
    int roman_str_to_decimal(int input)
    {
        int i;
    
    
        for (i=0; i< strlen(input); i++)
        {
           total += roman_to_decimal(numeral[i]);
    
    
    
    
        }
    
    
    }
    
    
    int roman_to_decimal(char letter)
    {
        switch(tolower(letter))
        {
            case 'm':
                return 1000;
            case 'd':
                return 500;
            case 'c':
                return 100;
            case 'l':
                return 50;
            case 'x':
                return 10;
            case 'v':
                return 5;
            case 'i':
                return 1;
            default:
                break;
        }
    }
    
    
    
    
    int main(void)
    
    
    {
    
    
    
    
    
    
    printf("Please enter the first number: ");
    scanf("%s", &numeral);
    
    
    roman_str_to_decimal(numeral);
    
    
    printf("%d\n", total);
    
    
    
    
    printf("%c\n", numeral[2]);
    
    
    
    
    }
    ok never mind, forgot to prototype and enter the correct datatype for one of my functions. It now spits out the correct number. Thank you for the help.
    Last edited by arkine; 10-11-2013 at 10:24 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read up on arrays, it seems you're missing a few basics. Textbooks, Google for tutorials, etc.

    Declare numeral to have a size, so there is actually room in it for your string:
    Code:
    #define MAX_NUMERALS  10  // constants allow you to easily change the size by changing just one line
    
    int main(void)
    {
        int decimal_value;
        char numeral[MAX_NUMERALS + 1];  // +1 for the null terminator that marks the end of a string in C
    
        decimal_value = roman_str_to_decimal(numeral[2]);  // pass the 3rd char in numeral to the function
    Remember, in C, valid array indexes are from 0 to SIZE-1. That's why numeral[2] is actually the 3rd char (numeral[0], numeral[1], numeral[2]).

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by arkine View Post
    ok never mind, forgot to prototype and enter the correct datatype for one of my functions. It now spits out the correct number. Thank you for the help.
    If it spits out the correct number I would say it's a matter of chance and somewhat like a game of Russian Roulette

    For example:

    Given
    Code:
    int input;
    What do you expect
    Code:
    strlen(input)
    to return?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 09:26 PM
  2. Roman Numeral^^
    By only1st in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2007, 10:58 AM
  3. Roman Numeral...T_T
    By only1st in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 10:06 AM
  4. Roman Numeral Convertor
    By mike1000 in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 02:53 PM
  5. Roman Numeral Calculator
    By The Brain in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2005, 06:23 PM