Getting a 4-digit number from a keypad

Amalitus_I:
It works really fine, thank you very much :slight_smile:
Very interesting the way you make the four digit number by iteration multiplication :astonished:
Hope this code will not give me problems in the final program, I have to enter five 4-digit numbers that go to really different parts of the program...
Anyway I'm really pleased :slight_smile:

There are a number of ways to do that. The brute-force method would be to duplicate the code 5 times, using another variable to select each code chunk in turn, which would work, but would be ugly and ineffifient.

A few ideas for you.

  1. Place that code in a function that returns an int. You'll need to make your own loop within the function. When you have the desired number of digits entered, you return the int. In this way, you can call the function for any variable by using something like
int frabble = getInt();  // where getInt is the function
int dwibb = getInt();  // and so on
  1. You could enclose the code within a for loop that will cause you to execute an inner loop 5 times, and assign the result of the inner loop to 5 elements of an array of ints.

Another thought for you. about the function. You could make it with two parameters, like this:

int getInt ( char size, bool method ) {
   // your code goes here
}

Now, when you call it, you give it a char containing the number of characters you want to enter for the int, and set method to true. Or, you could give it a char containing a character to use as a delimiter (end of input), and set method to false.

flabber = getInt ( 4, true );
gribble = getInt ( 'A', false );

Of course you then need to look for the delimiter to decide when to return the int.

There's a rather nice site for learning more abut these concepts at http://www.learncpp.com/