LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2010, 09:19 PM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
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.
 
Old 04-09-2010, 12:45 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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
 
Old 04-09-2010, 04:15 PM   #3
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
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.
 
Old 04-09-2010, 11:28 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-10-2010, 12:37 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,999

Rep: Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190Reputation: 3190
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Serial port : Read data problem, not reading complete data anujmehta Linux - Networking 5 09-06-2010 06:10 AM
Problems with data validation and cin darkangel29 Programming 2 09-14-2008 08:23 PM
Yet another: apt-get key validation problem haertig Debian 4 11-22-2006 09:41 AM
mysql - data validation at the table-creation-time prabhatsoni Linux - Software 2 03-24-2006 12:13 AM
Form validation problem (PHP+MySQL) linuxfond Programming 13 09-08-2003 12:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration