Not using repeating numbers

Feb 24, 2016 at 11:23pm
So I'm creating a code that requires the user to enter a password it has to be 6 digits and not have preceding zeros, the numbers have to be unique as well. I have the first two things down in my code, I need help with making sure that the user cannot repeat numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
int num=0;

cout << "Enter six digit code." << endl;
cin >> num;


while (num>999999 || num<100000){

      cout <<"code doesn't have six digits." << endl;
      cout <<"Enter six digit code." ;
      cin >> num;
}
cout << num << endl;

//Exit program
return 0;

}.
Feb 24, 2016 at 11:32pm
Have you considered using string to hold the "numbers"? It is much easier to insure a string has a certain length than it is with a number. Also you can then check that each digit is unique.
Feb 24, 2016 at 11:45pm
No, I haven't. I am a little confused at what you mean.
Feb 25, 2016 at 12:02am
I am a little confused at what you mean.

What exactly are you confused about?

Remember that not all "numbers" are really numbers.

Feb 25, 2016 at 2:29am
@KTlady
Do you know what strings are? I assume you do since you're including <string>.
Feb 25, 2016 at 4:24am
1. Read the code as a string (not a number).
2. Check that the first character is not '0'.
3. Sort it.
4. Look through it to see if there are any identical character values adjacent to each other.

Hope this helps.
Topic archived. No new replies allowed.