1

I've been stuck on this for two days. I've searched through page 20 on google and can't figure this out.

I need to accept only alphabetical letters for the input on townName.

I've tried every way of looping (that I can think of or find). Also, I've read that isalpha() only works on characters. However, I've searched for and implemented ways to convert a string from input to characters, I'm just not getting anywhere.

This is my last attempt:

// Input, validate, and set string name of town 
cout << "Enter name of town: "; 
getline(cin, townName); 
cin >> townName; cin.ignore();

while (townName != isalpha()) {
    cout << "Enter the town name - alphabet only.";
    cin >> townName; }

I'm aware now that is not the proper use of isalpha. I've also tried isalpha(townName), using bools but I need to return a prompt to re-enter if it contains anything other than alpha/white space, and if it's only alpha to continue with main.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • You need a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). For now, you want to loop over the characters using `isalpha()` on every character and if one is not, ask the user again. – eesiraed Feb 24 '18 at 00:52

1 Answers1

1

You were somewhat on the right track. You need to check each character of your string with isalpha. You might even want to allow for spaces i.e. "New York" etc.? I recommend writing your own method to do this in a loop over your whole input string. Put the whole thing in a while loop and you should be all set to do what you want.

#include <iostream>
#include <string>
#include <cctype>

// check for only alphabetical letters in string (or spaces)
bool lettersOrSpaces(const std::string& str)
{
    for (size_t i = 0; i < str.size(); i++)
    {
        // make sure each character is A-Z or a space
        if (! std::isalpha(str[i]) && ! std::isspace(str[i]))
        {
            return false; ///< at least one "no match"
        }
    }
    return true;  ///< all characters meet criteria
}

int main()
{
    std::string townName;
    std::cout << "Enter name of town: ";
    while (std::getline(std::cin, townName) && !lettersOrSpaces(townName))
    {
        std::cout << "Enter the town name - alphabet only: ";
    }
    std::cout << "The name of town is: " << townName << std::endl;

    return 0;
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23
  • 1
    Any reason you are not using an [range based `for`](http://en.cppreference.com/w/cpp/language/range-for)? e.g. `for (auto& i : str)` and then `if (! std::isalpha(i) && ...`? Just curious. – David C. Rankin Feb 24 '18 at 03:41
  • No reason except that OP didn't specify if they were using C++11 or better. – Justin Randall Feb 24 '18 at 03:42
  • Makes sense, just thought I would check. – David C. Rankin Feb 24 '18 at 03:42
  • The `lettersOrSpaces` function and/or the call to it could also be replaced with `std::find_if(std::begin(townName), std::end(townName), [](char let) { return !std::isalpha(let) && !std::isspace(let); }) == std::end(townName)` with the same effect. – ShadowRanger Feb 24 '18 at 05:45