473,418 Members | 3,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Input should allow only alphabetical letters, regardless of case

Hello, in my program I need to ask the user to input some alphabetical
letters. Case should not matter. Any input that isn't an alphabetical letter
should be rejected and the user prompted to try again.
I came up with:

std::cout << "Type a letter: ";

char c = '\0';

while(!(std::cin >> c) || !std::isalpha(c))
{
std::cerr << "That wasn't a valid alphabetical letter, please try again:
" << std::flush;

if(!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');
}
}
It seems to work, but I was wondering if it needs more robustifying without
going to ridicolous lengths to achieve it?

/ E
Jul 23 '05 #1
1 5062
Eric Lilja wrote:
Hello, in my program I need to ask the user to input some alphabetical
letters. .... while(!(std::cin >> c) || !std::isalpha(c))
Use could instead try:
string tmp;
getline (cin, tmp);
// parse tmp ...
{
std::cerr << "That wasn't a valid alphabetical letter, please try again:
" << std::flush;

if(!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsiz e>::max(), '\n');
}
}

It seems to work, but I was wondering if it needs more robustifying without
going to ridicolous lengths to achieve it?


What do you expect from a language like C++?

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
1
by: Sudhakar Doddi | last post by:
Greetings, I am a novice in Javascript and I am not able to succeed finding right regular expression for my requirement. Input in HTML form should be in the format of row.rack.bin i.e three...
4
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. ...
7
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
2
by: bildad | last post by:
The following 'book example' of validating input seems to be incomplete. Since it is a beginner's book it may be intentional for simplicity. But I would like to know how to make this program work...
2
by: Richard Keller | last post by:
How can I only allow user to enter upper case letters into a text box, and if he enters a lower case letter, change it to upper case. in vb6 I would use the keypress event and change the key to...
2
by: David Veeneman | last post by:
Is there any way to change the default view in Visual Studio's property editors from categorical to alphabetized? I'm getting awfully tired of clicking the A-Z icon every time I open a collection...
26
by: Bert | last post by:
Hi, I'm unhappy: why doesn't this work? char enc; char temp; for(int i=0;i<10000;i++){ fscanf(in,"%s",&temp); if(temp=='#')break; else
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.