Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If the user enters invalid characters, we would like the function to return 0 as

ID: 3596289 • Letter: I

Question

If the user enters invalid characters, we would like the function to return 0 as a special value indicating an error instead of a partial result so far. We can use 0 as a special value because there is no Roman number representation for 0 (the Romans didn't understand 0) so 0 would not be returned for normal input. The driver should print an error message when this occurs and let the user try entering another number. You may modify any of the files that you need to, but you will need to break the link and copy the file for those you want to change. I just need to add a while and printf statement in here. I just dont know where to add it to.

#include
#include "roman.h"
#include "romanutil.h"

int get_roman(void)
/* This function reads the next number in roman numerals from the input
        and returns it as an integer */
{
   char rdigit;
   int num = 0;
   int dig_value, last_dig_value = M;


        /* get the first digit */
        rdigit = getchar();

        while(rdigit == getchar())
        {
                printf("ERROR: not a valid roman number");
        }
        while(rdigit == ' ' || rdigit == ' ')
        {
        rdigit = getchar();
        }

        /* while it is a roman digit */
        while(is_roman(rdigit))
        {

                /* convert roman digit to its value */
                dig_value = convert_roman(rdigit);
                /* if previous digit was a prefix digit */
                if(dig_value > last_dig_value)
                        /* adjust total */
                        num = num - 2 * last_dig_value + dig_value;
                /* otherwise accumulate the total */
                else num = num + dig_value;
                /* save this digit as previous */
                last_dig_value = dig_value;

/* get next digit */
                rdigit = getchar();

        while(rdigit == ' ' || rdigit == ' ')
        {
        rdigit = getchar();
        }
        }

        /* return EOF if detected */
        if(rdigit == EOF) return EOF;
        /* return the number */
        return num;

Explanation / Answer

#include <stdio.h>

#include <string.h>

//declare the function prototypes

int convertRomanToIntegers(char romans[]);

int toDecimal(char symbol);

//define the main function

int main()

{

            //declare and integer variable and a string

            int roman_equi_int = 0;

            char roman[100];

            //continue the process until the user enters a proper

            //Roman value

            do

            {

                        //prompt the user to enter the roman value

                        printf(" Enter a roman number: ");

                        fgets(roman, 100, stdin);

                        //call the function convertRomanToIntegers to convert the roman into equivalent decimal value

                        roman_equi_int = convertRomanToIntegers(line);

                        //condition to check whether the return value is 0 or not

                        //if not zero the string is valid and break the loop

                        if (roman_equi_int != 0)

                                    break;

            } while (1);

            //if the input is valid then print the roman number entered by the user

            //and the equivalent decimal value to the console

            printf("The roman number is: %s ", roman);

            printf("The equivalent decimal form is: %d. ", roman_equi_int);

            return 0;

}

//definition of the function convertRomanToIntegers that contains the logic to convert the roman value

//to the respective decimal value

int convertRomanToIntegers(char romans[])

{

            //declare the required variables

            int i, a = 0, b;

            int length = strlen(romans) - 1;

            //loop till the there are no more roman values

            for (i = 0; i < length; i++)

            {

                        if (romans[i] != ' '|| romans[i]!=' ')

                        {

                                    //call toDecimal function to convert each character of roman

                                    //to its equivalent decimal value

                                    b = toDecimal(romans[i]);                            

                                    //add the returned decimal value

                                    a = a + b;

                        }

                        if (b == 0)

                        {

                                    //if there is any invalid character then print an error message

                                    printf(" Sorry! No such value to convert.");

                                    return 0;

                        }

            }       

            return a;

}

//defintion of function toDecimal that takes a character and returns its

//equivalent decimal value

int toDecimal(char symbol)

{

            if (symbol == 'I')

                        return 1;

            else if (symbol == 'V')

                        return 5;

            else if (symbol == 'X')

                        return 10;

            else if (symbol == 'L')

                        return 50;

            else if (symbol == 'C')

                        return 100;

            else if (symbol == 'D')

                        return 500;

            else if (symbol == 'M')

                        return 1000;

            return 0;

}