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:
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. |
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' |
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) { 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. |
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. |
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 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. |