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:
-
I tried to run debugging in Visual Studio and what I found out is that my
return
statements are deleting the values of myuserName
anduserPass
, can someone please explain why? -
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
andsetPw
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
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:
userName
inmain
is one string. When you callu.setName(userName)
then that string is copied. Insided the function you have a second string calleduserName
. That string is distinct from the one inmain
. You read user input in theuserName
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 inmain
: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:
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:However, you should prefer to return values rather than using out-reference parameters.
Two way you can solve this issue
First, take another string variable to avoid same name confusion.
Second, pass userName,userPass as reference in function
void userRegistration::setName(std::string &userName) {
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
The second issue is that you must use the return value in your calling code.
A couple of other points, the function
setName
does not set anything at all,getName
orinputName
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 lookNote 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).