LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 11-15-2003, 09:50 PM   #1
wood
Member
 
Registered: Aug 2003
Posts: 78

Rep: Reputation: 15
c++ problem. how to restrict input?


I am writing a program. It needs user to type in the input. eg. 2300.00

how can I block the character. In other words, the user cannot type in character and only accept numbers.
 
Old 11-15-2003, 09:51 PM   #2
Kurt M. Weber
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 335

Rep: Reputation: 36
You can't...you'll just have to let him input the data and then check whether it's valid or not.
 
Old 11-15-2003, 09:53 PM   #3
wood
Member
 
Registered: Aug 2003
Posts: 78

Original Poster
Rep: Reputation: 15
so how can i check it valid or not
 
Old 11-16-2003, 07:43 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
If you just want to ignore some characters, you can use the method:
istream& istream::ignore ([int N] [, int DELIM]).

For more info, type:
info iostream streams istream "Istream Housekeeping"

If you want to actually find the invalid char's, you'll have to read a string and scan it yourself using a loop. For reading strings choose a method from this info page:

info iostream streams istream "string input"

If the info-doc's are not on your system you can (depending on your linux distribution) install the "libstdc++2.10-dev" (for Debian-testing), or similar package.
 
Old 11-17-2003, 01:05 AM   #5
Onemessedupjedi
Member
 
Registered: Sep 2003
Location: Oregon
Distribution: Slackware 9.1
Posts: 194

Rep: Reputation: 30
is there a way to have it error and tell the user to try again instead of crashing when they enter an char for an int(cause in menu programs it gets annoying to go through the whole series of options to get back to where you want.
 
Old 11-17-2003, 03:38 AM   #6
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
Don't accept input as a number. Accept it as a string. Then you can check the characters in the string one by one against ASCII values. If all of the characters in the string have ASCII values of number 0 to 9, the input is valid and convert the whole string to the numeric and process it. Otherwise, give error message.
 
Old 11-17-2003, 04:28 AM   #7
nowonmai
Member
 
Registered: Jun 2003
Posts: 481

Rep: Reputation: 48
write your own edit/input class
 
  


Reply



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
Ctrl+Shift Unicode input gone, after installing Japanese Input Methodes polemon Linux - Newbie 1 09-20-2005 05:17 PM
Strange problem with input under X *Dark Dragon* Linux - Software 7 04-24-2005 04:53 PM
Sendmail: timeout waiting for input from local during Draining Input andrewstr Linux - Software 0 07-14-2004 01:43 PM
the chinese input problem vanbastern Linux - Newbie 3 04-04-2004 05:38 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM

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

All times are GMT -5. The time now is 04:50 AM.

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