How can my program recieve exactly 8 digit input??

Sep 25, 2012 at 7:51am
Hey guys..what can i do if i want my program to take 8 digit input number ,no less no more than it.. i try to ask my frind but its hard to get..
(but not using getche()8x)

so example i have to input number ID..and if i input just 2 digit or more than 8 digit it shouldn't response or said that the input is not valid..
Last edited on Sep 25, 2012 at 7:55am
Sep 25, 2012 at 9:31am
The last post there will guarantee only a full integer is accepted from user (no garbage chars)
http://www.cplusplus.com/forum/beginner/13044/#msg62827

As for keeping it 8 digits you can make a simple test
1
2
3
4
5
cin n;
if(n > 99999999 || n < 10000000)
{
  //handle bad input
}
Last edited on Sep 25, 2012 at 9:31am
Sep 25, 2012 at 10:11am
@soranz.. you made lots of mistake.
n>99999999 it's eight times nine..it should be seven times..
n<10000000 it's one followed by 7 zeros.. it should be 8 zeros.
and you used || between two condition that will accept all input greater that eight times nine(as you write but actual one should have to seven times nine)..
Last edited on Sep 25, 2012 at 10:11am
Sep 25, 2012 at 10:43am
thaks guys..it's really help me..
Sep 25, 2012 at 3:29pm
HiteshhVaghani1:
@soranz.. you made lots of mistake.
n>99999999 it's eight times nine..it should be seven times..
n<10000000 it's one followed by 7 zeros.. it should be 8 zeros.
and you used || between two condition that will accept all input greater that eight times nine(as you write but actual one should have to seven times nine)..


Last time I checked Neo frost wanted 8 digits, and 99999999 is the largest number you can have with only 8 digits (excluding decimals) and 1000000 is the smallest number you can have with 8 digits (including decimals).
So as a conclusion: No you are wrong and soranz was right, when trying to correct someone choose one who isn't smarter than you.
Sep 25, 2012 at 7:02pm
Thanks Latrakx :)

We all make mistakes ~
Sep 25, 2012 at 7:14pm
Well, there are numbers and there are numbers. For example a serial number or an account number etc. could have some leading zeros. In the general case the entire number could consist of zeros. The only thing we know is that each position contains a digit (0-9) and there are eight of them.

That makes the full range 00000000 to 99999999.

It may be necessary or useful to treat the input as a character string, rather than an integer.
Last edited on Sep 25, 2012 at 7:16pm
Sep 25, 2012 at 7:37pm
You're right Chervil.
OP should be more clear so we don't have to guess what he means by 'number' ;)
Topic archived. No new replies allowed.