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.
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 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.
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.