473,379 Members | 1,167 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,379 software developers and data experts.

user input

hi,
what function can be used to make sure that the user input must be seven digits?



Expand|Select|Wrap|Line Numbers
  1. int number;
  2. do
  3. {      cout<<"Enter NUMBER"<<endl;
  4.        cin>>number;
  5.        if ()
  6.        cout<<"Please enter NUMBER with seven digits"<<endl<<endl;
  7.  
  8. }
  9. while();
Apr 24 '07 #1
12 2753
weaknessforcats
9,208 Expert Mod 8TB
Enter tha data as an integer and check that it is >999999 and <10000000
Apr 24 '07 #2
Enter tha data as an integer and check that it is >999999 and <10000000
but what if i want the user to be able to enter number like 0000000, 0000125, 0123456.....
Apr 24 '07 #3
Savage
1,764 Expert 1GB
Then create function which will populate array of chars up to some limit,in this case it's seven,convert it to integer and return that number.So there won't be need for testing that number.(and also with this u can control that user inputs numbers only)

Savage
Apr 24 '07 #4
Savage
1,764 Expert 1GB
And if u don't know how to do it i'll be glad to help u on this one out while u are posting and asking in correspondance to posting guidelines.


PS:Sorry for double post


Savage
Apr 24 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
This won't do it:
Then create function which will populate array of chars up to some limit,in this case it's seven,convert it to integer and return that number.So there won't be need for testing that number.(and also with this u can control that user inputs numbers only)
You cannot control user input. The user can enter anything of any length in and any format but until the user presses enter, your C++ program isn't in control. When your program does receive control, the input buffer can contain anything.
Apr 24 '07 #6
Savage
1,764 Expert 1GB
This won't do it:


You cannot control user input. The user can enter anything of any length in and any format but until the user presses enter, your C++ program isn't in control. When your program does receive control, the input buffer can contain anything.
Yes we can:

1.Use getche.

2.If the char entered has value beetwen 48 and 57,inclusive put it in array of chars.

3.If enter is pressed or limit is crossed break out from loop and add '\0' char on the end.

4.return array.

5.Ta-dam.Done!

Savage
Apr 24 '07 #7
Yes we can:

1.Use getche.

2.If the char entered has value beetwen 48 and 57,inclusive put it in array of chars.

3.If enter is pressed or limit is crossed break out from loop and add '\0' char on the end.

4.return array.

5.Ta-dam.Done!

Savage
hey savage,
can help me out here, i really have no idea how to do it...
Apr 24 '07 #8
Savage
1,764 Expert 1GB
hey savage,
can help me out here, i really have no idea how to do it...
Do u know how to populate arrays and how to operate with pointers?

Savage
Apr 24 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
This quote:
1.Use getche.
Won't be called until the user has pressed enter. When that happens the input buffer can contain anything.

If you really want this:
user to be able to enter number like 0000000, 0000125, 0123456.....
Then you are better off using cin.getline() and parsing the buffer received. At least this way if the user screwed up the entry format, you can ask for the entire line to be re-entered.
Apr 24 '07 #10
Savage
1,764 Expert 1GB
[quote=weaknessforcats]This quote:

Won't be called until the user has pressed enter. When that happens the input buffer can contain anything.
QUOTE]

Function getch() reads a single char from keyboard directly and return character readed from keyboard.

getche() is just immediately echoing on stdout.

so if we have next situation:
char c;
c=getch();

we will then be able to display that char on stdout.And becasue it's returning infact ASCII decimal value we can make up a if-else chain like:

Expand|Select|Wrap|Line Numbers
  1. //start loop
  2.  
  3.        c=getch();
  4.        if(c>=48&&c<=57) put it in array of chars
  5.        else if(c==13) break from loop
  6.  
  7. //end loop
Savage
Apr 24 '07 #11
Banfa
9,065 Expert Mod 8TB
This quote:

Won't be called until the user has pressed enter. When that happens the input buffer can contain anything.
Function getch() reads a single char from keyboard directly and return character readed from keyboard.
This is true, it does however suffer from being a platform specific function so code using it will be non-portable.

However if that is acceptable then I would suggest using getch instead of getche. Then validate the character as a digit immediately and only if it is a digit output it to the screen and store it in your array.
Apr 24 '07 #12
Savage
1,764 Expert 1GB
This is true, it does however suffer from being a platform specific function so code using it will be non-portable.

However if that is acceptable then I would suggest using getch instead of getche. Then validate the character as a digit immediately and only if it is a digit output it to the screen and store it in your array.
Yes,and I was thinking of the getch,getche is typing error.

See my previous post

Savage
Apr 25 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
4
by: santa19992000 | last post by:
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)?. It displays to enter IP address, if user wants to change, then he enters, otherwise he hits keyboard,...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
9
by: MadingS | last post by:
Is there an HTML (or CSS) way to say "use the user's INPUT font, whatever that might be."? Most browsers have the ability for the user to pick his or her own preference for what font to use...
2
by: underground | last post by:
Hi, everyone I've been trying to figure out a way for a user to update there information. I'm using sections to identify the specific user..Here is the form <? include("include/session.php");...
5
by: no1zson | last post by:
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out. Same story, I am learning Java and have just written a CD...
1
by: kang jia | last post by:
hi when user entered particulars in signuppage and click" signup" button, i will direct them to do_signup.php. if say the NRIC is dupicate in datebase, i will redirect them back to signup page...
82
by: happyse27 | last post by:
Hi All, I modified the user registration script, but not sure how to make it check for each variable in terms of preventing junk registration and invalid characters? Two codes below : a)...
9
by: happyse27 | last post by:
Hi All, In perl script(item b below) where we check if html registration form are filled in properly without blank with the necessary fields, how to prompt users that the field are incomplete...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.