LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2008, 06:38 PM   #1
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Rep: Reputation: 15
Problems with data validation and cin


I have the following code
Code:
cout<<"What is the hour of the activity (Standard hour 1-24)?";
      cin>>hour;
while(!(cin>>hour) || hour<0 || hour>24 )
	{
		cout<<"\nThe hour entered is not valid. Please try again: ";
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
	}
I need the user to enter an hour between 1 and 24 and make sure that the data enter is an integer, now I'm able to do this but when the user enter the time for the first time it doesn't want to read it unless is not a number , they have to press enter and enter it one more time for the program to read it. I'm using Microsoft Visual Studio 2005. Thanks
 
Old 09-14-2008, 08:14 PM   #2
elprawn
Member
 
Registered: Feb 2005
Distribution: Gentoo 2008
Posts: 138

Rep: Reputation: 15
Try deleting the first cin statement. You have two, one in and one outside the while statement so maybe that's why only the second one works.
 
Old 09-14-2008, 08:23 PM   #3
darkangel29
Member
 
Registered: Nov 2004
Location: Puerto Rico
Distribution: Ubuntu 10.04
Posts: 121

Original Poster
Rep: Reputation: 15
Thanks it works now. Aleluya!!!!!!!!!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: Problems comparing data from a DB with data from http request eantoranz Programming 3 08-14-2008 07:55 PM
cin.get() question JMJ_coder Programming 7 03-10-2008 02:54 PM
cin in cpp , milky.way Programming 4 12-08-2007 10:16 AM
mysql - data validation at the table-creation-time prabhatsoni Linux - Software 2 03-24-2006 12:13 AM
cin.getline adam_boz Programming 6 10-02-2002 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration