Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to validate if all characters enetred in a string are alphabets and then reprompt user

#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;
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: how to link to page elements html 
Typescript :: typeorm where in example 
Typescript :: how to use array pop in typescript 
Typescript :: ring Composition and Returning Objects and Lists by Reference 
Typescript :: como acessar um elementRef de um componente 
Typescript :: how to find muti column name exists in sql server 
Typescript :: if confidence level increases what happens to width 
Typescript :: which of the following are elements associated with the html table layout? 
Typescript :: why are lower case alphabets more preffered in html 
Typescript :: typescript cast to parent type 
Typescript :: use curly brackets in latex 
Typescript :: bullmq 
Typescript :: typescript allow object subset of interface 
Typescript :: typescript make every property of an object nullable 
Typescript :: nativescript alert 
Typescript :: aws elastic web python stops ajax requests if new request is made 
Typescript :: bibtex remove brackets in note field 
Typescript :: Ionic toast animation 
Typescript :: exits adn copy file in java 
Typescript :: matplotlib eats all memory when saving fig 
Typescript :: add and edit in ionic page 
Typescript :: test events where not received 
Typescript :: running same tests against different data 
Typescript :: real time charts in flutter 
Typescript :: What was in Rome that helped Renaissance artists achieve their goal of Humanism? 
Typescript :: yarn create react app typescript 
Cpp :: qt get hexa value from qstring 
Cpp :: unreal engine delay c++ 
Cpp :: how to use winmain function 
Cpp :: C++ red text output 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =