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

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++)
                 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.
                       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";

           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);

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.

6 Years
Discussion Span
Last Post by firstPerson

Your code does not compile.

To solve your problem, remove the else part which returns 1.

Add a initialised (true) flag in the function. Whenever an alphabet is encountered wich does not match your two conditions , set the value of the flag to a new value (false).

After the entire string is checked, use the flag to return 1 or some other value.

Edited by thomas_naveen: n/a


All you are doing is testing the first character and returning 1, 2, or 3.
You need to set a variable to 0.
Instead of returning in your loop, set the variable to the appropriate number. But be sure to check the value before setting it otherwise the value will reflect only the last character.

At end of the function, return variable;


Sorry WaltP. im trying out your method and i do not really understand what you mean. Could you give an example of what you trying to mean?


Also don't use magic numbers. Use constants.
For example :

enum FuncReturn{ ALL_DIGITS , INVALID_INPUT };
int myCheck(const string& str){
 if(str.find_first_of("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz") != string::npos) return INVALID_INPUT;
 else if(str.find_first_of("!@#$%^&*()-_+=';[]{}:<>?/.,`~") != string::npos) return INVALID_INPUT;
 else return ALL_DIGITS;
int main(){
 FuncReturn f;
 f = myCheck("123123143431a");

Edited by firstPerson: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.