//Create a random 10 character password with the first character being a upper case letter. The remaining characters are random selections of Upper case
//letters, lower case letters, 9 digits and/or special characters (found only on the top row of your keyboard). Please exclude 0 and O's from being used.After generating the password, display the password to the user and ask them to enter the password until it is entered correctly. Once the password is entered correctly please display the message "Sign On now successful!".

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

static const char alphanum[]= "123456789"
                              "!@#$%^&*"
                              "ABCDEFGHIJKLMNPQRSTUVWXYZ"
                              "abcdefghijklmnpqrstuvwxyz";

int size = sizeof(alphanum) - 1;
char password=alphanum[rand() % size];
int main()

{
    //password size
    int length = 3;

    cout << "Your Password is ";

    srand(time(0));
    for (int i = 0; i < length; i++)

{
        cout << alphanum[rand() % size];
}

{
        cout << endl;
}

    {
    cout << "enter the provided password " << endl;
    cin >> password;

    if ((password = alphanum[rand() % size]))

    cout << "works" << endl;
        }

    return 0;
}

Dani AI

Generated

Good progress so far — moved from the broken one-char approach to storing the generated value in a std::string, correctly pushed the idea of keeping that buffer for comparison, and flagged the classic single-= vs == pitfall. Below are a few compact, practical refinements and troubleshooting notes that will make the program more robust and easier to maintain.

Prefer C++11’s <random> over rand() for clearer intent and better quality (and to avoid modulo bias). To guarantee the first character is uppercase, pick that character from an uppercase-only set, then fill the remaining slots from the full pool. Seed the generator once (outside any loop) and reserve the string size before building it to avoid repeated reallocations. Example (illustrative — focus on the selection idea, not the full pool text):

std::string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<std::size_t> d(0, upper.size()-1);
char first = upper[d(gen)];
// append 'first' to the password, then draw remaining chars from your combined pool

Practical troubleshooting checklist:

  • Do not regenerate the password when checking the user input; keep the generated password in a variable and compare against it.
  • Use == (not =) for string equality checks; use std::getline if you expect pasted input or spaces (operator>> stops at whitespace).
  • Seed once; do not call srand(time(0))/seed inside a character loop.
  • If you ever use C-style buffers, remember length+1 and the null terminator.

Notes on real-world use: printing a generated password and accepting it plaintext is fine for an exercise but never for production. For cryptographic-grade secrets use a platform CSPRNG and follow secure-credential handling practices. If you need composition rules (at least one digit/symbol/etc.), generate required characters first, fill the rest from the pool, then shuffle the string so positions are randomized.

Recommended Answers

All 11 Replies

I would encourage you to fill a buffer with the user's password first, then display it - and in that local buffer you can do a strcmp.

Just my preferred way of solving your problem.

Otherwise... you have uncompleted code here.. not sure what part of it you need help with since there is no clear thing you want help with (and I'm not sure it works at all the way you intend... so... I don't even know where to begin).

i have it where it generates the PW and displays it, it also ask the user to enter the password, i cant get the validation to work for the PW, just started out coding. the password is supposed to be random. hope that can help

How do you expect to compare what the user has entered if you don't keep reference to the password you generated?

I stand by my original comment: start by filling a buffer with your created password, then use that to both output the password as well as compare it to what they input.

can you help me understand how to get it to be stored so it can be refrenced later?

Line 40 has another issue. I see a single equal sign. Do you know why that matters?

This is beyone what ryantroop is commenting about.

char caFoo[11]; // 10 characters + null terminator
memset(caFoo, 0, 11); //set all 11 buckets to null for ease of use

start a loop: 
    for (int ilup = 0; ilup < 10; ilup++)
        Generate a random character
            if ilup == 0, make sure it's a cap value, so either keep generating, or modify your generated char from a subset of available chars
            else, caFoo[ilup] = generated char

when this loop is all done, caFoo will have all the characters available to you in the form of a null terminated "string"

you can then std::cout(caFoo); and you can see what is in the buffer.
you can then compare using strcmp(password, caFoo) and if it is equal to 0, then they match, otherwise they don't.

Get into the habit of giving your variables meaningful tips so you know what they are. I used iLup to denote an "int" value, "ca" for character array. There are other standardizations, but pick one and stick with it. When your code gets super long and you have to scan 10,000 lines youll be happier.

ok i did some reworking

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()

{
    static char alphanum[]= "123456789"
    "!@#$%^&*"
    "ABCDEFGHIJKLMNPQRSTUVWXYZ"
    "abcdefghijklmnpqrstuvwxyz";
    int size = sizeof(alphanum) - 1;
    string password, inputPassword;
    int length = 10; //password size

    srand(time(0));

    cout << "Your Password is ";

    for (int i=0; i<length; i++)
{
        password+=alphanum[rand() % size];
}

    cout << password << endl;

    cout << "enter the provided password " << endl;
    cin >> inputPassword;

    while (!(inputPassword==password))
    {
        cout << "Please enter the correct Password" << endl;
        cin >>inputPassword;
    }

    cout << "sign on successful!" << endl;

    return 0;
}

my question now is how do i get the first character of the pw to always be a capital

got it guys! i made another static char with just the captials and added another lenght and size

I tried your assignment just to do it (because... why not, right?)

The way I solved your issue is I put all 4 lines in their own arrays, and made an array of arrays as the parent (so a 2D array, top level if 4 members deep, and the sub arrays are their respective lengths.

From there, you can do most of the work in a single for loop to fill the buffer. You can control which bucket you pull from (all caps is just using the 3rd bucket), otherwise you can randomly choose between 0-3 then 0-the respective size of the text block.

Im not sure which one gives you higher entropy, since your data set is longer you have more to pick from - where mine has 2 random ops to get the result you get from 1.

Over all, it was a fun exercise. Cool to see you figured a way to solve it on your own. Great work! :)

Be a part of the DaniWeb community

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