LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with Data Validation (https://www.linuxquestions.org/questions/programming-9/problem-with-data-validation-800912/)

gregarion 04-08-2010 09:19 PM

Problem with Data Validation
 
Hey guys, i am having a huge problem checking the data i input to make sure it is correct. What i am trying to achieve is when i input a value, it will check if the input is all digits and if it is not, check to see if it contains certain alphabets. Thus for example, if i were to input in data such as "11A" , the program will then imform me "There is an important alphabet in the program." This would be my expected output.

Here is the program i have wrote...

Code:

     

int test(string r ){
  const int arraySize = 10;


 char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'};
   
   
         
        for(int cntr = 0; cntr <r.length(); cntr++)
           
            if(!isdigit(r[cntr])){
                for(int new1 =0; new1<arraySize2;new1++)
                      for(int cntr1 = 0; cntr1 <r.length(); cntr1++)
                  if(array2[new1] == r[cntr1]){
                      return 2;  //will return2 when it finds the same
                                  // char in the array and the string r.
                  }
                  else{
                      return 3; //will return 3 when there is a char
                                //in the string which isnt in the array
                  }
            }
            else {
         
                return 1; // will return 1 when string is all digit.
            }


int main()
    {
         
  string r = "11D";
          test(r);

          if(test(r) == 1)
          {
              cout << "ALL ARE DIGITS" << endl;
          }
          if (test(r)== 2)
          {
              cout << "There is an important alphabet in the program."  << endl;
          }
            if (test(r)== 3)
          {

              // testRoman(r);
                cout << "THERE IS AN ALPHABET IN THE STRING WHICH IS NOT IN THE ARRAY" << endl;
          }
}


So, the problem i am facing is when i input in data such as 11 or A , the prog will come out the right input. But if i were to put in data such as "11A" , the output coming out will be "ALL ARE DIGITS". The problem which causes this seems to be in the return statement , as once as it finds the first char which is a digit, it will then return 1 and not continue checking the rest of the string. Is there a way i can stop or continue a loop if it has met the condition i stated?

Any suggestions on what i can do or any other way available for me to check my input? Hope for some help. Thanks.

grail 04-09-2010 12:45 AM

I seem to have seen this exact type question recently in another post??

Anyhoo, I believe you need to have a good hard look at your 'if' statement in test()
( oh and checking out why the first 'for loop' in there has no braces??)

Let us use your example of 11A:

Code:

//So on entering test(), r = '11A'
for(int cntr = 0; cntr <r.length(); cntr++){

    if(!isdigit(r[cntr])){// here cntr = 0, so r[0] = '1' which is a digit
        //skip this part as isdigit
    }
    else{
        return 1; // personally I think bad to rip out of the middle of everything (but just MO)
    }            // so we have only check the first character, but we got what were after so send back a 1
}

//back to main
if(test(r) == 1)
{
    cout << "ALL ARE DIGITS" << endl;
}

Let me know if that makes it clearer

gregarion 04-09-2010 04:15 PM

Hey, i managed to solve part of the problem by breaking down the function and seperating it. I solved the integer part. But i am having a problem searching for particular characters which are stored in the array.
Code:

bool testRoman(string r) {
        const int arraySize2 = 19;
        char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'};
     
       
 

        for(int c = 0 ; c < arraySize2 ; c++)
                    for(int e = 0; e < r.length(); e++)
                        if(array2[c] == r[e])
                        {
                            return true ;
                           
                        }
       
             
              else {
                  return false ;
              }
        }





      int main()
    {
          string r = "DDA";

   

          testRoman(r);
 
          if ( testRoman(r)){
            cout << "got number" << endl;

        }
          else{
              cout << "romans" << endl;
          }



}

For the code i wrote, it works fine when i enter only one letter into the string. But when i enter data such as "DDA" , the expected output is "got number" but i am getting the output "romans".

I guess the problem is because the program just checks the first char in the string and immediately returns. How am i able to get the prog to check the entire string and then imforming me if the string contains any characters in the array?

If there are other ways or functions which i should be trying out, please feel free to help.

graemef 04-09-2010 11:28 PM

There is always a danger when you have return statements within a loop of returning before you should. Which is what is happening with your code, you will only ever compare the first character in your array against the list that you have. What you need to do is think about how the logic of you problem should work, sketch that out and then start coding.

As a general rule if you are looking for a match then it is alright to return when you have found a match, but it is not okay to return when you don't have a match.

grail 04-10-2010 12:37 AM

Well I am not sure you have read the replies fully, but try changing your functions
to be of the format, one entry and one exit:

Code:

bool testRoman(string r) {//this is considered the entry point

    <set boolean variable here>

    <do your code here>

    return <your boolean variable> // this is your one exit
}

This is not to say that multiple exits should never be used but when learning a language it is a good rule
of thumb until you learn some of the intricacies associated with the language.


All times are GMT -5. The time now is 10:36 AM.