• 04-01-2003
volk
How do make sure someone doesn't enter the same number more than once?

This code doesn't work. I'm not sure what I'm doing.

Code:

```#include <iostream> using namespace std; int main() {         int number;         cout << "Enter five numbers, and don't enter the\n"                 << "same number more than once" << endl;         for (int i = 0; i < 5; i++)         {                 cin >> number;                                 for (int repeat_number = 0; repeat_number < number; repeat_number++)                 if (repeat_number == number)                 {                         cout << "You've already entered that number" << endl;                         i--;                 }         }         return 0; }```
• 04-01-2003
XSquared
Just use an array, store the values in it, and check whether the value is in the array.
• 04-01-2003
SeanMSimonsen
Your code doesn't work because you are only checking repeat_number against number, which is the last entered number, each time you go through the loop. What you need to do is check it against all previously entered ones. To get around this, you would want to consider using an array of numbers, and checking each entry against that array, then adding it to the array. If you don't know arrays yet, just use five different variables, and compare all of them to each other.
• 04-01-2003
volk
:confused:
• 04-01-2003
alpha
here, take a look at this:

Code:

```#include <iostream> int main() {         int number, repeat, i, j;         int array[6];                 std::cout << "Enter five numbers." << std::endl;                 for(i = 0; i < 5; ++i) {                        std::cout << "Enter number " << i + 1 << ": ";                 std::cin >> number;                         repeat = number;                 array[i] = number;                                 if(i > 0) {                         for(j = 0; j < i; ++j) {                                 if(array[j] == number) {                                         std::cout << "Number repeated." << std::endl;                                         exit(1);                                 }                         }                         array[i] = number;                 }         }                 for(i = 0; i < 5; ++i) {                 std::cout << "Number " << i + 1 << " = " << array[i];                 std::cout << std::endl;         }                 return 0; }```