search and didnt find anything that was close to this. so my assignment is to simulate a simple authentication, where the user enters their original pin or password(numbers only). it must use arrays to assign the password a pseudo-random assignment of 1-3 for digits 0-9. after that the program clears the screen and asks the user to reenter the password using the only 1-3 according to the reference table displayed. then the program is supposed to output if the new entry was correct or not.

most of the program works logically at least but my problem is idk how to compare the stored random assignments against the original password. here's what i've got so far there's a big comment line where im having problems in the code.

[

#include <iostream>
#include <stdlib.h> 
#include <stdio.h>
#include <cstdlib>
#include <time.h>

using namespace std;

void random_numbers(int random_number[]) //randomly generate numbers from 1-3 for each digit.
{
    srand ( time(NULL) );
    for (int i = 0; i < 10; i++)
        {
        random_number[i] = (rand()% 3) + 1;
        }
    cout << "Random #'s:\t";


    for (int i = 0; i < 10; i++)
        cout << random_number[i] << " ";
    cout << endl << endl;
}

void password_reference()
    {
        cout << "Reference #'s:\t";
        for ( int i = 0; i < 10; i++)
            {
            cout << i << " ";
            }
        cout << endl;
    }

void pw_storage(int stored[])
{
    // Input the user's entry
    cout << "Please store your 8 digit password number in to program first: "<<endl;
    cin >> stored[8];
//    system ("cls"); //clear the screen to hide the password for security concern

}

bool isValid(int actual_PW[], int entered_PW[], int random_Nums[])
    {
        bool valid = false;
//*****************************This is where i'm having trouble**********************************
        for (int index = 0; (!valid) && (index < sizeof(actual_PW)); index++)
            {
                random_Nums[index] = actual_PW[index];
                if (entered_PW[index] != )
                    {
                    valid = false;
                    }
            }
        return valid;
    }


int main()
{
    int stored_PW[8], random_nums[10], encoded_PW[8];
    bool valid = false;

    pw_storage(stored_PW);
    password_reference();
    random_numbers(random_nums);

    cout << "Enter password corrsponding to the reference table:\n";

    for(int i = 0; i < 8; i++)
        cin >> encoded_PW[i];
    isValid(stored_PW, encoded_PW, random_nums);

    if(valid == true)
        cout << "Correct! You may now proceed" << endl;
    else
        cout << "Error, invalid password entered" << endl << "Enter your password again." << endl;
}

Recommended Answers

All 4 Replies

bool isValid(int actual_PW[], int entered_PW[], int random_Nums[])
    {
        bool valid = false;
//*****************************This is where i'm having trouble**********************************
        for (int index = 0; (!valid) && (index < sizeof(actual_PW)); index++)
            {
                random_Nums[index] = actual_PW[index];
                if (entered_PW[index] != )
                    {
                    valid = false;
                    }
            }
        return valid;
    }

#1) If you are trying to compare the password entered (entered_PW[]) with the actual password (actual_PW[]), why is random_Nums[] needed to check validity?
#2) What is the purpose of random_Nums[index] = actual_PW[index]; ?
#3) What is the obvious comparison for entered_PW[index] in the IF ?
#4) If you only set valid to FALSE, how can it return TRUE?

well i tried comparing the (entered_PW[]) to (actual_PW[]) but they are always different because the program should assign each character in the pw to 1-3 randomly.

the random_Nums[index] was left there from something else that i tried, but forgot to delete.

the obvious would be to compare the entered_PW to the actual_PW but looking back over the rest of the code i think i forgot to assign each digit in the actual_PW to the random 1-3. how could i do this? or is

actual_PW[index] = random_Nums[index];

sufficient?

in this iteration it couldn't return true, and that is mostly because i hadnt fully figured out what i needed to compare to get the true.

heres an update on that function

bool isValid(int actual_PW[], int entered_PW[], int random_Nums[])
    {
        bool valid = false;
//*****************************This is where i'm having trouble**********************************
        for (int index = 0; (!valid) && (index < sizeof(actual_PW)); index++)
            {
                actual_PW[index] = random_Nums[index];

                if (entered_PW[index] == actual_PW[index])
                    {
                    valid = true;
                    }
                else
                    valid = false;
            }
        return valid;
    }

the obvious would be to compare the entered_PW to the actual_PW but looking back over the rest of the code i think i forgot to assign each digit in the actual_PW to the random 1-3. how could i do this? or is

actual_PW[index] = random_Nums[index];

sufficient?

That's seems to be exactly what the function is for, so yes.

in this iteration it couldn't return true, and that is mostly because i hadnt fully figured out what i needed to compare to get the true.

heres an update on that function

bool isValid(int actual_PW[], int entered_PW[], int random_Nums[])
    {
        bool valid = false;
//*****************************This is where i'm having trouble**********************************
        for (int index = 0; (!valid) && (index < sizeof(actual_PW)); index++)
            {
                actual_PW[index] = random_Nums[index];

                if (entered_PW[index] == actual_PW[index])
                    {
                    valid = true;
                    }
                else
                    valid = false;
            }
        return valid;
    }

Your thinking is backwards. If any values are TRUE, this function returns TRUE.

When creating a function that returns an all-or-nothing comparison, like this function, it's best to start by setting the response to 'all' (TRUE). Then during the the testing, if any value fails, a single FALSE comparison sets the result to FALSE.

The way you have it, the result assumes FALSE. Then during the the testing, if any value succeeds, that single TRUE comparison sets the result to TRUE. Clearly not what you want.

Think of this as an ANDing function. Any fail returns FALSE, All successes return TRUE. Yours is an ORing function.

But to make matters worse, because of the ELSE clause, the only comparison that matters is the very last. You will return T/F based on that comparison alone.

thank you i figured it out, also i was missing something elsewhere that my friend from class pointed out to me

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.