error msg instead of the result so far

This is a discussion on error msg instead of the result so far within the C Programming forums, part of the General Programming Boards category; I'm writing a program that reads in characters. If the invalid character is in front of the word, it will ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    30

    error msg instead of the result so far

    I'm writing a program that reads in characters.
    If the invalid character is in front of the word, it will print the error msg, then flush the rest.
    If the invalid character is after a valid character, it will instead print out the result so far instead of an error msg.
    How do I "not print" the result so far?

    This is my driver:
    Code:
    main()
    {  int number;
       int is_invalid;
       is_invalid = FALSE;
    
    
            /*  prompt for first roman number entry  */
            printf("Enter an number in roman numerals(EOF to quit): ");
    
    
            /*  while there are more numbers  */
            while((number = get_roman()) != EOF)
            {
    
            if(number == 0)
            {
            printf("Invalid entry!\n");
            }
    
            else
            {printf("The number is %d\n",number);}
            FLUSH;
            /* prompt for next number  */
            printf("Enter an number in roman numerals(EOF to quit): ");
            }
            /*  clean up screen  */
            printf("\n");
    }

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Post your full source code, please. There's no get_roman() function in that listing.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    this is my roman.c. includes get_roman function.
    Code:
    #include <stdio.h>
    #include "roman.h"
    #include "romanutil.h"
    #include "chrutil.h"
    #include "tfdef.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 it is a white space skip it  */
            while(IS_WHITE_SPACE(rdigit))
            rdigit = getchar();
    
            while(!is_roman(rdigit) && !IS_WHITE_SPACE(rdigit) && rdigit != EOF) return 0;
    
            /*  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;
    }
    this is function that examine if it's a roman numeral.
    Code:
    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;
    Thank you so much!!!!!!
    Sorry I didn't post it clear.

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Hmm.

    There's nothing immediately obvious that seems to glare out as "I'm wrong! Come fix me!".

    What problems are you having?

    If the invalid character is in front of the word, it will print the error msg, then flush the rest.
    If the invalid character is after a valid character, it will instead print out the result so far instead of an error msg.
    Makes sense. To me at least.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    It does seem to make sense to me.

    When i enter X, it should say the number is ten.
    When i enter VI, it should say the number is 6.
    When i enter AX, it should say it is invalid! then skip calculation X.
    When i enter XA, it should say it is invalid!

    First 3 cases, it works perfectly fine.
    But the last one.
    It prints "the number is 10". then thats it.
    no error msg for A.

    That's my only problem right now, have been working on it for 6 hours....

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Hmm. Look here.

    Code:
            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;
    }
    What does it do when it detects a non-roman-numeral-character?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Code:
           
    while(!is_roman(rdigit) && !IS_WHITE_SPACE(rdigit) && rdigit != EOF) return 0;
    it returns 0 when it detects a non roman digit and it's not white space or ctrl d.

    the while loop you quoted happens when is_roman return true.

  8. #8
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    No, watch.

    You enter 'V'.

    It accepts it, because 'V' is a roman numeral.

    You enter 'C'.

    It accepts it, because 'C' is a roman numeral.

    You enter 'Z', which is *not* a roman numeral.

    It exits from the loop, keeping the "VC". Nothing else is wrong, so . . . it continues as normal . . .

    So, is this not your problem? You say you want it to print an error when you enter an invalid character. ("no error msg for A.")
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Quote Originally Posted by Nightowl View Post
    No, watch.

    You enter 'V'.

    It accepts it, because 'V' is a roman numeral.

    You enter 'C'.

    It accepts it, because 'C' is a roman numeral.

    You enter 'Z', which is *not* a roman numeral.

    It exits from the loop, keeping the "VC". Nothing else is wrong, so . . . it continues as normal . . .

    So, is this not your problem? You say you want it to print an error when you enter an invalid character. ("no error msg for A.")

    You are correct.
    enter VCZ,
    When Z is read, the program should ignore the "VC" calculation and just print error.
    (mine prints the result of VC instead of error)
    If there is any non roman numeral, it prints error, no matter where it is placed.

  10. #10
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Quote Originally Posted by maybabier View Post
    When Z is read, the program should ignore the "VC" calculation and just print error.
    . . . but it won't . . . at least, not how you have it set up. So, change it!
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I've been messing with main().
    It seems like everything should be in a loop.
    It makes sense to me that if it reads an invalid it should only print error.

    Code:
    #include <stdio.h>
    #include "roman.h"
    #include "chrutil.h"
    #include "tfdef.h"
    main()
    {  int number;
    
    
            /*  prompt for first roman number entry  */
            printf("Enter an number in roman numerals(EOF to quit): ");
    
    
            /*  while there are more numbers  */
            while((number = get_roman()) != EOF)
            {
            if(number == 0)
            {printf("It's invalid!\n");
            FLUSH;}
    
            else
            {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");
    }
    now i think about it, why doesnt it print result everytime it reads a digit.
    hmmm... i must be missing something.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    but even in get_roman function, when it reads an invalid, return 0,
    0 should update the "number" value and it should just print error.
    what am i missing?

  13. #13
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Probably something very obvious.

    As I noted before, here:

    Code:
            }
    
            /*  return EOF if detected  */
            if(rdigit == EOF) return EOF;
    
            /*  return the number  */
            return num;
    *Nowhere* here do you handle what happens when it's an invalid roman numeral. I've no idea, without the full source and a little while to study it, if you *should* be checking here, but common sense to me says you should.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I included
    Code:
    while(!is_roman(rdigit) && !IS_WHITE_SPACE(rdigit) && rdigit != EOF) return 0;
    in get_roman should get it taken care of...
    not sure....

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    I have found more problems now.. Oh I hate this.

    Enter an number in roman numerals(EOF to quit): cccccccccccccaddddddd
    The number is 1300
    Enter an number in roman numerals(EOF to quit): The number is 3500

    it skipped a.
    it seems like if i put a non roman after a roman it skips it!
    i didn't tell it to...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 12:29 PM
  2. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  3. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  4. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21