Problem validating

This is a discussion on Problem validating within the C++ Programming forums, part of the General Programming Boards category; hello, I'm working on a roman numeral converter, but I'm having trouble validating that the user input is valid. Even ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    10

    Problem validating

    hello, I'm working on a roman numeral converter, but I'm having trouble validating that the user input is valid. Even when I input a valid roman number the following function returns false. Can someone be kind enough to point out what I'm doing wrong, and also I'm confused as how to validate that the roman number is valid, for example XIDMXM is not valid.

    Code:
    bool isValidSigns(char roman[20])
    {
    	bool valid = true;
    	int i = 0;
    
    	while(roman[i] != '\0')// && valid != false)
    	{
    		cout<<"= "<<roman[i]; //for testing
    		if(roman[i] != 'M' ||'D' ||'C' ||'L'||'X' ||'V' ||'I')
    		{
    			valid = false;
    		}
    		i++;
    	}
    
    	return valid;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    Code:
    if(roman[i] != 'M' ||'D' ||'C' ||'L'||'X' ||'V' ||'I')
    is the problem. It only compares roman[i] with 'M'.

    If roman[i] is not equal to 'M', the result is true and else is checked nothing happens (so valid is set to false).

    If roman[i] is equal to 'M', the next step is to evaluate the expression 'D'. 'D' is a non-zero integral value, so evaluates to true (non-zero). This means the test always drops through to setting valid to be false.

    What you probably intend is this
    Code:
    if(roman[i] != 'M' && roman[i] != 'D' && roman[i] != 'C' && roman[i] != 'L'  && roman[i] != 'X' && roman[i] != 'V' && roman[i] != 'I')
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    10
    Thank you for the help, i tried using && but not && and != and now that I look at the code you provided it makes sense.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,289
    you could use an std::string containing "MDCLXVI", and search for the character in that string using std::string::find().

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    10
    Quote Originally Posted by Elkvis View Post
    you could use an std::string containing "MDCLXVI", and search for the character in that string using std::string::find().
    Well so far i'm getting the output I was expecting. I thought about using a string but felt more comfortable using char[]. I need more practice with string as I moved from C to C++.
    Last edited by Kinto; 10-25-2009 at 04:14 PM.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    Quote Originally Posted by Kinto View Post
    Well so far i'm getting the output I was expecting. I thought about using a string but felt more comfortable using char[]. I need more practice with string as I moved from C to C++.
    Yes, because std::string is much harder to work with, right?

    Code:
    
    int main( void )
    {
        std::string
            txt;
        txt = "foo";
        txt += "bar";    
        if( txt == "foobar" )
            /* do something*/;
        return 0;    
    }
    Versus:

    Code:
    
    int main( void )
    {
        char
            txt[ 1024 ];
        strncpy( txt, "foo", sizeof str );
        strncat( txt, "bar", sizeof str );
        if( strncmp( txt, "foobar", sizeof str ) == 0 )
            /* do something*/;
        return 0;    
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    10
    When people ask me why I drive standard over an automatic, I simply tell that I get bored and like switching gears. well enough of bad analogies thanks for the suggestions and the sample code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. problem with validating data inside function
    By DeathEvil in forum C Programming
    Replies: 7
    Last Post: 07-10-2007, 12:32 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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