skip to Main Content

I am very new to C++ and programming in general, I am still learning the very basics of how to program but I want to improve. I will attach the full code below.

I have two questions:

  1. I tried to run debugging in Visual Studio and what I found out is that my return statements are deleting the values of my userName and userPass, can someone please explain why?

  2. How is the structure of my code and my functions? Am I doing it right by putting most of the logic inside of the setName and setPw functions or should I do something different, perhaps it is better to put the std::cin input code outside of functions? I understand there are many ways you can go around programming something but I just wonder if I’m doing it right or not structure-wise.

Expected Result:

  • Console outputs username which the user inputs
  • Console outputs password which the user inputs

Problem:
Whenever I input the values of userName and userPass the console output always comes back empty.

Here’s my code:

#include <iostream>

//Program idea: The user inputs their username and password and the system outputs it back on the console

class userRegistration
{
public:
    std::string setName(std::string userName);
    std::string setPw(std::string userPass);
};

std::string userRegistration::setName(std::string userName) {

    std::cin >> userName;
    
    return userName;
}

std::string userRegistration::setPw(std::string userPass) {

    std::cin >> userPass;

    return userPass;
}

int main()
{
    std::string userName = "";
    std::string userPass = "";

    userRegistration u;

    //Username Section
    std::cout << "Please enter your username: n";
    u.setName(userName);

    //Password Section
    std::cout << "Please enter your password: n";
    u.setPw(userPass);

    //Output
    std::cout << "Your username is: " << userName << "n";
    std::cout << "Your password is: " << userPass << "n";
}

3

Answers


  1. You are not using the values returned from the function. You got confused by two totally different object having the same name existing in different scopes. I removed most of it:

    std::string userRegistration::setName(std::string userName) {
        std::cin >> userName; 
        return userName;
    }
    
    int main()
    {
        std::string userName = "";
        userRegistration u;
    
        u.setName(userName);
    
    }
    

    userName in main is one string. When you call u.setName(userName) then that string is copied. Insided the function you have a second string called userName. That string is distinct from the one in main. You read user input in the userName in the function and return it. Though, in main you ignore the return value. If you use the return value you’d see the change also in main:

    userName = u.setName(userName);
    

    However, you seem to be conflating two concepts of getting stuff out of a function.

    You should either simply use the value returned from the function. In that case there is no need to pass an empty string to the function:

    std::string userRegistration::getName() {
        std::string x;
        std::cin >> x; 
        return x;
    }
    
    int main()
    {
        
        userRegistration u;
    
        std::string userName = u.getName();
    
    }
    

    Or, alternatively you let u.setName(userName) write to the string it gets passed. In that case the string has to be passed by reference and there is no point to return something:

    void userRegistration::setName(std::string& x) {
        std::string x;
        std::cin >> x; 
    }
    
    int main()
    {
        
        userRegistration u;
        std::string userName;
        u.setName(userName);
    
    }
    

    However, you should prefer to return values rather than using out-reference parameters.

    Login or Signup to reply.
  2. Two way you can solve this issue
    First, take another string variable to avoid same name confusion.

    //Username Section
        std::cout << "Please enter your username: n";
        std::string getuserName = u.setName(userName);
    
        //Password Section
        std::cout << "Please enter your password: n";
        std::string getuserPass = u.setPw(userPass);
      
       std::cout << "Your username is: " << getuserName << "n";
       std::cout << "Your password is: " << getuserPass << "n";
    

    Second, pass userName,userPass as reference in function

    void userRegistration::setName(std::string &userName) {

        std::cin >> userName;   
        
    }
    
    void userRegistration::setPw(std::string &userPass) {
    
        std::cin >> userPass;
        
    }
    
    Login or Signup to reply.
  3. As is very common with newbies you’ve got yourself confused over parameters, return values and calling functions. There is no need for setName to take a parameter, it returns a value, it does not accept a value.

    Here’s how it should look using a local variable instead of a parameter

    std::string userRegistration::setName() {
        std::string userName; // local variable
        std::cin >> userName;
        return userName;
    }
    

    The second issue is that you must use the return value in your calling code.

    userName = u.setName();
    

    A couple of other points, the function setName does not set anything at all, getName or inputName would be better names.

    The class userRegistration has no purpose at all. It should be removed and your functions can simply be written as global functions. Here’s how that would look

    std::string setName() { // global function
        std::string userName; // local variable
        std::cin >> userName;
        return userName;
    }
    
    userName = setName(); // call global function
    

    Note that in the code above there are two variables called userName. That is not a requirement for this code to work. You could rename either variable and the code would still work fine (newbies often get confused over this point as well).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search