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...



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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" which is wrong as this string does contain an aplhabet which is in the array. 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.




Any suggestions or ways in which i can modify this coding or probably try smting else in order to make sure i can check for all this conditions and input the correct statements?
Have you run this thru a debugger? You should consider doing this as a matter of course.

If the first character in the string is a digit, test returns 1 indicating 'All Digits'.
Topic archived. No new replies allowed.