When this program prompts the user for input, it works fine for the correct input. But for the wrong inputs, it returns a zero for every character, including the white spaces. My goal of this program is to return a roman numeral as an integer. For the wrong inputs, I would like to return only one zero and a prompt to reenter another value. What can I further do to fix this program?
Here is the code:
driver.c
/* This file contains a test driver which reads a number in roman
* numerals and prints it in decimal. I uses the function
* get_roman() in roman.c
* */
#include <stdio.h>
#include "roman.h"
#define FLUSH while(getchar() != '\n')
main()
{ int number;
/* prompt for first roman number entry */
printf("Enter a number in roman numerals(EOF to quit): ");
/* while there are more numbers */
while((number = get_roman()) != EOF)
{ printf("The number is %d\n",number);
/* prompt for next number */
printf("Enter an number in roman numerals(EOF to quit): ");
}
/* clean up screen */
printf("\n");
}
roman.h
/* This file contains the prototypes and macros for converting
* roman numerals.
* */
/* These macros represent valid roman numerals and their values */
#define M 1000
#define D 500
#define C 100
#define L 50
#define X 10
#define V 5
#define I 1
int get_roman(void);
/* returns the next roman number in the input or EOF */
roman.c
/* This file contains the functions used to read and convert a number
* in roman numerals.
* */
#include <stdio.h>
#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 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();
}
/* return EOF if detected */
if(rdigit == EOF) return EOF;
/* return the number */
return num;
}
romanutil.h
/* This file contains the prototypes for utilities used in
* converting roman numerals.
* */
int is_roman(char c);
/* tests if c is a valid roman numeral, returns true or false */
int convert_roman(char c);
/* converts roman numeral c to its value, NULL if invalid */
char to_upper(char);
/* converts a character to upper case */
romanutil.c
/* This file contains the functions used to read and convert a number
* in roman numerals.
* */
#include <stdio.h>
#include "roman.h"
#include "romanutil.h"
#include "tfdef.h"
#include "chrutil.h"
int is_roman(char c)
/* This function is given a character and returns true if it is
* a valid roman numeral, flase otherwise. */
{
/* convert digit to upper */
c = to_upper(c);
/* test the digit */
switch(c)
{ case 'M':
case 'D':
case 'C':
case 'L':
case 'X':
case 'V':
case 'I': return TRUE;
default : return FALSE;
}
}
int convert_roman(char c)
/* This function is given a roman numeral and returns its value.
* NULL is returned if the character is not valid */
{ int digit;
/* convert digit to upper */
c = to_upper(c);
/* convert the digit */
switch(c)
{ case 'M':
digit = M;
break;
case 'D':
digit = D;
break;
case 'C':
digit = C;
break;
case 'L':
digit = L;
break;
case 'X':
digit = X;
break;
case 'V':
digit = V;
break;
case 'I':
digit = I;
break;
default :
digit = (int)NULL;
}
/* and return its value */
return digit;
}
char to_upper(char c)
{
if(IS_LOWER(c)) return c - 'a' + 'A';
return c;
}
tfdef.h
/* File: tfdef.h */
#define TRUE 1
#define FALSE 0