Read text, handle white space

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    The problem would be to make the program tolerate the white space and still read the string of characters. the objective would be to read roman numerals. the function can read the roman numerals but the white space read it as a zero and trash the whole thing. I have very little understanding of the usage of "flush" as well.

  2. Relevant commands, code, scripts, algorithms:

#define   IS_WHITE_SPACE(c)   ((c) == ' ' || (c) == '\t' || (c) == '\n')


  1. The attempts at a solution (include all code and scripts):
#include <stdio.h>
#include "roman.h"
#include "romanutil.h"
#include "chrutil.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();

        /* if the function reads 0 */

        if (is_roman(rdigit)==0)
        {
        printf("No Such Thing\n" );
                //return 0;
        }


        /*  while it is a roman digit or a white space  */

        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;


                 /* if there is a white space*/
                if(IS_WHITE_SPACE(rdigit))
                {
                        while(rdigit!='\n')
                        {
                        getchar();
                        return num;
                        }
                }


        /*  return the number  */
        return num;
}
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University Of Hawaii at Manoa, Honolulu (HI), Oahu (Hawaii), Tep Dobry, EE160 (I cannot post any link since this is my third post here, but google ee160 or tep dobry on google and it should be in the link 2011 ee 160: I apologize for any inconvenience)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

I don't understand why you put your if(IS_WHITE_SPACE) at the end.
Also getchar returns the integer represantation fo a char. I always cast them.
So my approch would be:

int c;

while((c = getchar()) != EOF) {
    if(IS_WHITE_SPACE((char) c))
        continue;
    /* now check for is_roman here and do the processing */
}

1 Like