in Education by
I have written a piece of code to validate a keyword, it validates and makes sure that the word is 5 letters long and it is all letters with no numbers in it. However, when I run it, the code seems to stop working at all and doesn't prompt me for the next question, I've tested it without this code and this part of the code is the problem, as it works fine without it. The code: cout<<name1<<", please enter the keyword (5 characters): "<<endl; cin>>key; for(int i = 0; i < keylength; i++){ if(isalpha(key[i]) == 1){ validnum += 1; } } if(validnum == keylength && key.length() == keylength){ validated = true; } else{ validated = false; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
As mentioned in the comments, there is no need for any loops to determine if the key is 5 characters and is all alphabetic. Using std::all_of will test if all the characters are alphabetic: #include #include #include #include bool isValidData(const std::string& key) { return key.size() == 5 && std::all_of(key.begin(), key.end(), ::isalpha); } int main() { //Test data std::string testKeys[] = {"1abcdef", "abcde", "abcdefghijkl", "a", "xyzab"}; for (size_t i = 0; i < std::size(testKeys); ++i) { // Output results std::cout << testKeys[i] << " " << (isValidData(testKeys[i])?"OK":"BAD") << "\n"; } } Output: 1abcdef BAD abcde OK abcdefghijkl BAD a BAD xyzab OK Also, if you took a look at your code, it is not clear what the goal of the code is without running the code. Compare that to the function isValidData: if you say what that function has in it, it reads almost as your description: "The key size must be 5, and all of the characters must be alpha".

Related questions

0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I want to store the hex values into a string, but I don't know to do that when my string is not giving me the ... using namespace std; int main(){ string s2 = "HelloWorld"; cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I need a way to remove the first character from a string which is a space. I am looking for a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I want to delete some certain characters that used wrongly in a string. "........I.wanna.delete. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How do you import a plain text file as a single character string in R? I think that this will probably ... is probably unstable too. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I'm using Python to open a text document: text_file = open("Output.txt", "w") text_file.write("Purchase Amount: ... how to do this? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I want an efficient way to append one string to another in Python. var1 = "foo" var2 = "bar" var3 = var1 + ... built-in method to use? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    What's the simplest way to count the number of occurrences of a character in a string? e.g. count the number of ... had a little lamb' Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I'm looking through the Apple's Vision API documentation and I see a couple of classes that relate to text ... overview of Vision. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript ... '\ 'def_id=' + def_id Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    Does Python have something like an empty string variable where you can do: if myString == string.empty: Regardless, ... not as good. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    In Python how can I convert an integer to a string? I am trying this: r = 16 r.str() I am trying to ... but there's nothing like that. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Is there any way to pad a numeric string with zeroes to the left side in a Python script, I want it to be of a specific length? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...