Isalpha() function with while loop c++


Isalpha() function with while loop c++



I want to create a while loop that will allow me to input a mix of string numbers until I input string that contains all characters.


while



Also, I have a problem with output


int main() {
string name;
string temp;
cout << "Enter your name:";
cin >> name;
cout << endl;
for(auto a:name) {
if(isalpha(a)) {
temp=name;
} else {
while(!isalpha(a)) {
cout << "Enter your name without digit:";
cin >> name;
cout << endl;
}
}
}
cout << temp << endl;
}



enter image description here




1 Answer
1


for(auto a:name) {



This is a loop over the characters in name, as entered after the prompt "Enter your name:". The current character is assigned to a.


name


"Enter your name:"


a


if(isalpha(a)) {
temp=name;
}



If the letter is alphabetic, assign temp = name (every time the current letter is alphabetic... this is not what you want!).


temp = name


else {



...if the current character (a) is not alphabetic...


a


while(!isalpha(a)) {



...enter a second loop, which will loop until a is alphabetic...


a


cout << "Enter your name without digit:";
cin >> name;
cout << endl;
}



...but a is never again assigned to. Your loop does not terminate.


a



You should re-work your logic. As this looks like a self-study project, I will not write the reworked loop for you, as I think you will learn much more from trying to do it on your own.





A hint on the solution: Your outer loop cannot be over the characters in a given string, as you conditionally want to read in a new string (and then loop over those characters). You can break out of a loop by break or return, of which return is usually preferred for reasons of code structure. You can also loop as long as a variable is set, and clear it inside the loop once you are satisfied with the input.
– DevSolar
Jul 2 at 11:50



break


return


return





the more important thing is that range for would not react to change of container size and content, it would not reset itself magically to beginning of string. No way range for should be a topmost loop in this scenario. Some overcomplicated for loop could be or I'd used while.
– Swift - Friday Pie
Jul 2 at 12:32



while





@Swift-FridayPie: It does not matter if it is range-for, iterator, or index access. Ugly hacks on the loop counter nonwithstanding, you could not (cleanly) reset the outer loop to work on the new string. That is why I said the outer loop could not (cleanly) be the loop over the string contents. -- All that being said, a "proper" solution would of course be using find_if instead of a loop... and would be checking for absence of isdigit() instead of isalpha(), as it makes the condition simpler and is locale-independent...
– DevSolar
Jul 2 at 12:37


isdigit()


isalpha()





(ctd.) ...as well as working as expected on UTF-8 strings as well as plain ASCII. ;-)
– DevSolar
Jul 2 at 12:38





I think we said same thing in different words, I didn't saw your comment until posted mine.C for() loop encompasses all possible loops with check at beginning of loop, and may not use counter at all , it just one of common uses for it. Actually it is defined as while(). What I meant that uppermost loop must be repeating input and input correctness check until string check passes. Would not you need wstring class for Unicode?
– Swift - Friday Pie
Jul 2 at 12:45







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?