Let's be positive and assume that the CIA has a powerful computer so I would try a brute force approach first.
Use a for loop between 123456 and 987654 and test if the number matches criteria 2-4.
If the number matches then ?? No instruction but you can write them to a file.
1. accept number from std::cin
2. check it's within upper and lower bounds, if so then ...
3. split number into individual digits http://stackoverflow.com/questions/4261589/how-do-i-split-an-int-into-its-digits
4. check the numbers are unique http://en.cppreference.com/w/cpp/algorithm/unique
5. then check if the sum of digits is prime - many programs available online to check if a number is prime
6. finally, the left digit would be index [0] of the container holding the digits - use vector - then sort the vector and the lowest digit would be the new index [0]
edit: you can also do some input validation at stage 1 that the console entry is actual an integer, no spaces, other characters etc
Creating all possible passwords from a set of rules
That is a tough job. But you don't need std::cin or anything.
1. The password must be an integer greater than or equal to 100,000 and less than 1,000,000 (6 digits).
You need a for loop for this.
2. All the digits in the number have to be unique.
Use std::stringstream, then std::count to check if the number has unique digits.
3. The sum of the digits must be a prime number.
You make an isPrime() function then you sum up all digits and check.
4. The permutation of the left-most digit (n) and the smallest digit (k) must be odd—for example: in 645923, the left-most is 6 and the smallest digit is 2.
I don't know what that means, could you please explain? Thanks.
Your way will be getting no where. Firstly, write multiple functions and assign each function to a specific task.
If you want to generate all possible passwords, you only need a for loop and you don't need to input unless there is something the assignment stated.
1 2 3 4 5 6
a = password/100000;
b = password/10000 % 10;
c = password/1000 % 10;
d = password/100 % 10;
e = password/10 % 10;
f = password % 10;
This is complex and very potentially error-prone. It also does not work for all numbers.
You can use the function itoa to convert a number to string. You can use std::stringstream if you can feel like it.
1 2
char num_str[11];
itoa(number, num_str, 10);
Or :
1 2 3 4
std::string num_str;
std::stringstream ss;
ss << number;
ss >> num_str;
It is better you move the code to a function and let the function do the first task. Don't forget to print out the result (for testing purpose) if your number is correct as a string. Let me know if you are on the right track.
Edit : std::to_string may be very useful since you only need a line for this :
Here is another delete OP and he emptied his posts without saying a thing.
samander8 wrote:
This is what I have so far..right now I am working on how to get all
the numbers unique in each password. If anyone has any advice let me
know any help is appreciated!
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace std;
int main(void)
{
int password = 0, a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, unqiue
= 0, sum;
cout << "Enter password" << endl;
cin >> password;
for (password; password >= 100000 && password < 1000000;
password++)
{
a = password/100000;
b = password/10000 % 10;
c = password/1000 % 10;
d = password/100 % 10;
e = password/10 % 10;
f = password % 10;
//all the digits in the number have to be unique
if (a != b && a != c && a != d && a != e && a != f && b !=c && b
!= d && b != e && b != d && b != e && b != e && c != d && c != e && c
!= f && d != e && d != f && e != f)
{
unqiue = 1; //means that all the numbers are unique
}
//sum of the digits must be a prime number
sum = a + b + c + d + e + f;
int primeCandidate;
int isPrime = 1;//assume is a prime number
for (int i=2; i<primeCandidate; i++)
{
if (primeCandidate%i == 0)
{
isPrime = 0; //not prime
}
{
}
cout << "Your passwords are: " << endl;
cout << a << b << c << d << e << f <<endl;
}
return 0;
}}