Click here to Skip to main content
15,881,413 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionUser Input revisted 2 [modified] Pin
Harold_Wishes12-Jul-06 5:02
Harold_Wishes12-Jul-06 5:02 
QuestionRe: User Input revisted 2 Pin
David Crow12-Jul-06 5:17
David Crow12-Jul-06 5:17 
AnswerRe: User Input revisted 2 [modified] Pin
Harold_Wishes12-Jul-06 5:39
Harold_Wishes12-Jul-06 5:39 
GeneralRe: User Input revisted 2 Pin
David Crow12-Jul-06 5:53
David Crow12-Jul-06 5:53 
GeneralRe: User Input revisted 2 [modified] Pin
Harold_Wishes12-Jul-06 6:12
Harold_Wishes12-Jul-06 6:12 
GeneralRe: User Input revisted 2 Pin
David Crow12-Jul-06 6:33
David Crow12-Jul-06 6:33 
GeneralRe: User Input revisted 2 Pin
Zac Howland12-Jul-06 10:12
Zac Howland12-Jul-06 10:12 
GeneralRe: User Input revisted 2 [modified] Pin
earl12-Jul-06 8:52
earl12-Jul-06 8:52 
Harold, what I would do is:
<br />
char buff[1024];<br />
unsigned int value;<br />
int charsRead;<br />
<br />
bool validEntry = false;<br />
while (!validEntry){ <br />
   cout << "Please enter the number of subunits: ";<br />
   cin.getline(buff, 1023, '\n');<br />
   <br />
   if (sscanf(buff, "%u%n",&value,&charsRead)==1 && value >=1 && value <= 12)<br />
      validEntry = true;<br />
}<br />


Otherwise, if you want to use c++ style stuff (I rarely do because I mostly work with windows programs and text strings), you could do sth like this:

<br />
	int test;<br />
	std::string dump;<br />
<br />
	bool valid = false;<br />
	while(!valid) {<br />
		cout << "Please enter a number: ";<br />
		cin >> test;<br />
<br />
		if (!cin.fail())<br />
			valid = true;<br />
		else {<br />
			cin.clear();<br />
			cin >> dump;  //clear input buffer<br />
		}<br />
<br />
		if (valid && (test < 1  ||  test > 12)) {<br />
			valid = false;<br />
			cout << "Out of range" << endl;<br />
		}<br />
			<br />
	}<br />
	cout << "You entered " << test << endl;<br />


PS: how does everyone get code to not remove all spacing?

earl

-- modified at 14:53 Wednesday 12th July, 2006
GeneralRe: User Input revisted 2 [modified] Pin
Harold_Wishes12-Jul-06 10:04
Harold_Wishes12-Jul-06 10:04 
Questionpthread Pin
mehmetned12-Jul-06 4:55
mehmetned12-Jul-06 4:55 
AnswerRe: pthread Pin
toxcct12-Jul-06 4:58
toxcct12-Jul-06 4:58 
GeneralRe: pthread Pin
led mike12-Jul-06 5:02
led mike12-Jul-06 5:02 
AnswerRe: pthread Pin
David Crow12-Jul-06 4:58
David Crow12-Jul-06 4:58 
AnswerRe: pthread Pin
led mike12-Jul-06 5:05
led mike12-Jul-06 5:05 
AnswerRe: pthread Pin
Jun Du12-Jul-06 5:08
Jun Du12-Jul-06 5:08 
Questionoverzealous protection rules Pin
Dave Schumann12-Jul-06 4:53
Dave Schumann12-Jul-06 4:53 
AnswerRe: overzealous protection rules Pin
toxcct12-Jul-06 4:56
toxcct12-Jul-06 4:56 
GeneralRe: overzealous protection rules Pin
Dave Schumann12-Jul-06 5:02
Dave Schumann12-Jul-06 5:02 
AnswerRe: overzealous protection rules Pin
Dave Kerr12-Jul-06 4:59
Dave Kerr12-Jul-06 4:59 
GeneralRe: overzealous protection rules Pin
Christian Graus12-Jul-06 5:00
protectorChristian Graus12-Jul-06 5:00 
GeneralRe: overzealous protection rules Pin
toxcct12-Jul-06 5:05
toxcct12-Jul-06 5:05 
GeneralRe: overzealous protection rules Pin
Dave Kerr12-Jul-06 5:08
Dave Kerr12-Jul-06 5:08 
GeneralRe: overzealous protection rules Pin
Dave Schumann12-Jul-06 5:29
Dave Schumann12-Jul-06 5:29 
GeneralRe: overzealous protection rules Pin
Dave Schumann12-Jul-06 5:04
Dave Schumann12-Jul-06 5:04 
AnswerRe: overzealous protection rules Pin
Maximilien12-Jul-06 4:59
Maximilien12-Jul-06 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.