four digit code in C++

Hi

I am trying to find an example of code for a four digit number eg 5556 that will be entered by the user as a code that will pull information from an array. this number also needs to be validated. can anyone help with such code?

it would be grealy appreciated as its a first year C++ assignment
You're not really giving much information. In which way will '5556' pull information from an array? As index? And how exactly is it valid? Does it need to be in a certain range? Does it have to be even? Does it have to be a Tribonacci number?
its a four digit code which the user has to enter (has to be four digits!) the programme is for video shop, the code will pull the dvd name and price from the array.

i hope this makes more sense
it does not have to be even, can be 1234, it has to be four digits at the moment it works with 3 digits as well, not sure how to write to code to validate that it has to be 4 digits
I'm still not sure how you're linking the number to array values (simply looping through? Seems inefficient, but it works).

Checking for 'exactly 4 digits' is easy. Any number between ]999,9999] has exactly 4 digits.
closed account (1vRz3TCk)
array -> map
http://www.cplusplus.com/reference/stl/map/
thanks @gaminic will try that,,,seems so simple
thanks again
closed account (1vRz3TCk)
Checking for 'exactly 4 digits' is easy. Any number between ]999,9999] has exactly 4 digits.
0001 also has four digits.
see now how do you write code for that?
closed account (1vRz3TCk)
I would probably read the number as a string, check that the length id four and each position isdigit(). You can then convert it to an int or use it however you want to 'look up' the data.
@CodeMonkey: true, missed that. Then you could still simply check for <10000, as there is no point in making a difference between 1 and 0001, if all you're doing is reading in numbers and checking them versus other numbers.
closed account (1vRz3TCk)
I guess, it would depend on what the requirements are. Is it important that the user has entered four digits even if the value is less than four digits? personally I would say, for a 'four digit code', 0001 != 1.
Topic archived. No new replies allowed.