Hi guys, I am trying to write a program that will ask for users name, birthdate and pet and from those info I need to use a random number generator to select 6 char from the name and animal and 2 numbers from the birthdate. This is my code to far.

#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>

using namespace std;
// **************************
// Purpose of this program is to
// ask the user there name,favorite animal
// and birthday and generate a password
// base off of that
// **************************

int main () {

const int MAX = 100;
char name[MAX];
char birth[MAX];
char pet[MAX];
int password;

password = rand() % 7;
cout <<"Please enter your name :";
cout <<"Please enter your birthday :";
cout <<"Please enter your favorite pet/animal :";
cout <<" password: ", password << '\n';

return 0;

Recommended Answers

All 6 Replies

And what problem are you having?

I'm confused on how to use the rand() on the array and only generate 6 digits from name and animal and 2 from birthdate.

Take it a step at a time...

How would you generate 6 digits from name and animal and 2 from birthdate?


you give option to change password. Edit profile

If ( yes )

Simple option give Edit passowrd link direct in confirmation mail.

If ( No )

then genrate passowrd from first 2 chr from evry details.

commented: Completely worthless. Doesn't follow the description at all. -3

So you want to get a string in which you'll store random characters from the name, pet and birth date rite?
I would suggest using instead of that char name[max] thing, strings, for an easy use.
Also, I would get the input from the user, and than form a string composed by the name and pet strings. On that string, I would randomly get characters using the rand() function giving as range, the lenght of that string. Also I would make some kind of validation, say if the name contains spaces (the user inputs multiple names eg. Peter Jackson), to not select as a password character the space character: ' '.
In all this mix I would also add some random characters from the birthdate string.

I'll post in pseudo code a function which will take as parameters 2 strings, first the original string, and the second string, got by reference, the password string in which the caracters will be stored:

subalg passwordMaker(string str, string &password)
    char c<-str[rand.range(0 to sizeof str)]
    if c=' ' then
        while c=' ' do
            c<-str[rand.rang(0 to sizeof str)]

In the main function I would make a loop, say a for loop, from 0 to 7. Than, with some if clause I would check is certain number is reached, and call the passwordMaker function with the birthdate as it's argument. In the rest I would call the passwordMaker function with the name+pet string in order to mix things up.
For example:

for i=0 to 7 do
    if i=2 or i=5 then
        passwordMaker(birthday, password)
    passwordMaker(name+pet, password)

If you have further questions, don't hesitate to post them.

Also, I would get the input from the user, and than form a string composed by the name and pet strings.

Yes. Then randomly shuffle the string and pick the first six chars.
Likwwise, for the string containing digits (pick the first two after a shuffle).
Randomly shuffle the eight chars to form the password.
Something like:

std::string text = get_text( "your name" ) ;
std::string numbers = get_date( "your date of birth" ) ; // say yyyymmdd
text += get_text( "your favorite animal" ) ;

std::srand( std::time(nullptr) ) ;
enum { NCHARS = 6, NDIGITS = 2 } ;

std::random_shuffle( text.begin(), text.end() ) ;
std::random_shuffle( numbers.begin(), numbers.end() ) ;

std::string pword = text.substr(0,NCHARS) + numbers.substr(0,NDIGITS) ;

std::random_shuffle( pword.begin(), pword.end() ) ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.