Home > Software design >  I am creating a password reset program using pass by reference. Once the newPassword is set, it does
I am creating a password reset program using pass by reference. Once the newPassword is set, it does

Time:04-19

Trying to create a password reset using a pass-by-reference reset() function:

returnType reset(input parameter)

parameters: reference (address) to a string variable return values: none, updates the variable in place

This is a pass by reference function since it alters the original value stored in a variable. This function prompts the user to input the updated value, inside the function. Then the original value is replaced by the new updated value entered by the user, permanently.

//call libraries
#include<iostream> //cin and cout library
#include<string> //string handling library


using namespace std;

void reset(string password); // Function prototype

int main() {
    //original username and password declared and initialized
    string userName = "Guesswho";
    string password = "Hmm...";
    //declare variables
    string inputUserName;
    string inputPassword;

    char answerP; //store user answer

    do //start a do-while loop
    {
        //prompt the user for the username
        cout << "Enter Your Username Please:" << endl;
        cin >> inputUserName;

        //prompt the user for the password
        cout << "Enter Your Password Please:" << endl;
        cin >> inputPassword;
        cout << "************************************" << endl;

        //evaluate the input values with the original values
        if (inputUserName != userName)  //Scenario 1: wrong user name. Resetting username is not allowed! try again
        {
            cout << "invalid user name" << endl; //allow the user to try again
            cout << "try again!" << endl;
            //counter  ;
        } else if (inputPassword != password) //Scenario 2: correct username, incorrect password
        {
            cout << "Did you forget your password?(y/n)" << endl; //allow user to reset the password
            cin >> answerP;
            if (answerP == 'y') //collect the new password
            { reset(password); }//call function to reset the password.
            else { cout << "try again!" << endl; }
        } else if ((inputUserName == userName) &&
                   (inputPassword == password)) //Scenario 3: both are correct, allow access
        {
            cout << "access granted!" << endl;
            cout << "Hi, " << inputUserName << " !" << endl;
            break;
        }
    } while (true); //infinite loop
    system("pause");
    return 0;
}

/********************************************************************************************
returnType reset(input parameter)

 parameters: reference (address) to a string variable
 return values: none, updates the variable in place

 This is a pass by reference function since it alters the original value stored in a variable.
 This function prompts the user to input the updated value, inside the function.
 Then the original value is replaced by the new updated value entered by the user, permanently.
 *************************************************************************************************/

void reset(string password) {
    string newPassword;

    cout << "Please enter your new password: "; cin >> newPassword; // get user inputted new password

    string z = password;
    password = newPassword;
    newPassword = z;

    // create user-interface
    cout << "Password successfully updated! Please try again." << endl;
}

CodePudding user response:

As Explained By Most Of The Comments What Your Code is doing is Pass By Value And Not Pass By Reference. To Use Pass By Reference You Need To Modify the following:

  1. Change The Prototype to void reset(string&);
  2. Change The Function Definition to void reset(string&password);
//call libraries
#include<iostream> //cin and cout library
#include<string> //string handling library


using namespace std;

void reset(string&); // Function prototype

int main() {
    //original username and password declared and initialized
    string userName = "Guesswho";
    string password = "Hmm...";
    //declare variables
    string inputUserName;
    string inputPassword;

    char answerP; //store user answer

    do //start a do-while loop
    {
        //prompt the user for the username
        cout << "Enter Your Username Please:" << endl;
        cin >> inputUserName;

        //prompt the user for the password
        cout << "Enter Your Password Please:" << endl;
        cin >> inputPassword;
        cout << "************************************" << endl;

        //evaluate the input values with the original values
        if (inputUserName != userName)  //Scenario 1: wrong user name. Resetting username is not allowed! try again
        {
            cout << "invalid user name" << endl; //allow the user to try again
            cout << "try again!" << endl;
            //counter  ;
        } else if (inputPassword != password) //Scenario 2: correct username, incorrect password
        {
            cout << "Did you forget your password?(y/n)" << endl; //allow user to reset the password
            cin >> answerP;
            if (answerP == 'y') //collect the new password
            { reset(password); }//call function to reset the password.
            else { cout << "try again!" << endl; }
        } else if ((inputUserName == userName) &&
                   (inputPassword == password)) //Scenario 3: both are correct, allow access
        {
            cout << "access granted!" << endl;
            cout << "Hi, " << inputUserName << " !" << endl;
            break;
        }
    } while (true); //infinite loop
    system("pause");
    return 0;
}

/********************************************************************************************
returnType reset(input parameter)

 parameters: reference (address) to a string variable
 return values: none, updates the variable in place

 This is a pass by reference function since it alters the original value stored in a variable.
 This function prompts the user to input the updated value, inside the function.
 Then the original value is replaced by the new updated value entered by the user, permanently.
 *************************************************************************************************/

void reset(string&password) {
    string newPassword;

    cout << "Please enter your new password: "; cin >> newPassword; // get user inputted new password

    string z = password;
    password = newPassword;
    newPassword = z;

    // create user-interface
    cout << "Password successfully updated! Please try again." << endl;
}

CodePudding user response:

As a quick overview in C when a function is called the variables in the call are copied.

void increment(int to_be_incremented){
       to_be_incremented;
}

int main(){
     int ret = 0;
     increment(ret);
     return ret;
}

Now as ret is copied the original is unaffected. This can be circumvented by passing a pointer to the object.


void increment(int* to_be_incremented){
       (*to_be_incremented);
}

int main(){
     int ret = 0;
     increment(&ret);
     return ret;
}

Now the address is copied into to_be_incremented, but as it still points to ret the value is changed. Now this can be laborious on both the caller's and the function's part since now we have to both pass and deal with int* instead of int. In C there are references, which solve this issue (I am unsure how it is implemented, but if I remember correctly these are just syntactic wrappers around pointers). References are denoted by & just like pointers have *. So our previous example becomes:


void increment(int& to_be_incremented){
       to_be_incremented;
}

int main(){
     int ret = 0;
     increment(ret);
     return ret;
}

The code is the same, yet now the value is incremented.

Note that there is more to references than this. For example increment(5) will not work (not that it would make sense), so creating functions that both modify parameters and produce return types cannot be written in this way or at least called like this. 5 here has type int&&, which is a different type of reference to int&. It is quite a useful part of the language especially when dealing with dynamic objects, but there are many great resources about that.

  • Related