Lets start with the warnings (you should always listen to them because they will give critical clues to many problems).
If you don't see warnings, try to google how to set it to the maximum level in your specific compiler.
In member function 'char welcome::newHere(char)':
19:9: warning: no return statement in function returning non-void [-Wreturn-type] |
I think this is simple enough for you to understand, you should always call a
return /*insert value here*/;
inside your function because you defined your function to return something, if you don't want it to return anything, return replace the char in the function definition with a void.
In function 'int main()':
25:10: warning: 'newUser' is used uninitialized in this function [-Wuninitialized] |
This is a critical clue to you, why is newUser uninitialized?
C++'s compiler is very smart, no matter how many hoops you go through, if a variable is not used, then the compiler will tell you, I should note it not something you should depend on since if you made newUser initialized with a value no warning would happen in the first place, but you should never ignore it them when you do see them, and if you don't understand something you should google it even if it seems unlikely to get help from that.
I should also point out that the class you created has absolutely no purpose. Perhaps in the future you could throw some variables that would've been in the global scope which is good, but for this context, it is best to be clear cut with with your intentions, because your function is no different than a global function.
As a solution there are 2 possibilities, I feel like you accidentally tried to do both at the same time, but you seem to not get it. Way #1 is to make your
(char makeAcc)
to
(char& makeAcc)
, Way #2 is to return the value.
I assume you are have habits from another language like Java, js, lua, and probably many others. In C++ a value put in a parameter is ALWAYS copied. All the way down to the core where even pointers or references are copied, and you have to learn that copying an integer or double is cheaper than copying & de-referencing a pointer since a pointer is no different than a number, internally. While also understanding what data is expensive or cheap to copy (because beginners commonly make the mistake of copying things in the parameters).
I'm going off at a tangent here, but when you make it to the point of trying to learn C-Strings (AKA:
char* magic = "how can this be a pointer and a string at the same time? MAGIC";
), it may confuse you that changing a single pointer does not change other pointers (like in other languages), so if you put a C-String into a function parameter and you want to dynamically change it's size (which requires pesky manual memory management work), you to have a pointer to a pointer in that case, because every pointer you create also has it's own pointer in memory which like any integer or primitive would have.
I personally believe people fall for this since people write code like:
1 2
|
char *c = "abc";
c = "defg";
|
but that falls apart when you try to do:
1 2 3 4
|
void change_str(char *c)
{
c = "defg";
}
|
Mainly because the value "defg" is immediately deleted when the function ends, since it is created on the stack, which if you don't know, stack memory looks like a directory tree, when you go in a directory you can access all the files from the directory and the directories that leads up to the path (that's not how directories work, but this is an analogy), but you cannot access or keep ownership of any other files outside the directories that are open, and all these files have a fixed or maximum size, unless you hold that memory in the heap like how std::list, vector, etc work. Also remember that heap vs stack memory is hard to compare, since a value may look like it is on the stack, like in a class, but it sometimes it's not (depending on how you allocate the class).
Meanwhile in C++ you have things like std::string which will be completely copied (expensive) if you don't make the parameter have the reference operator (for strings in particular use
const std::string &
because it allows more flexible ways of entering values).
Thus, connecting this to your problem, your function
newHere
has a parameter
makeAcc
, but it is a copy which does not change the orginal
newUser
, which causes the warning of
newUser
being uninitialized.
But I should remind you, it is kinda weird to get a value from a reference of a function, it makes more sense (in my opinion) to return the char, or return a boolean.
If you want to retry after entering an invalid answer, you will require probably a
while(true)
loop inside your function, and calling return when you want to exit out, and continuing the loop when default happens.
It also makes more sense to move the print
"Are you new here? (y/n)"
to be inside the function (in my opinion), since if you wanted to change "y/n" to "yes/no" the code is decoupled to easily change the question with the input. And you could perhaps explain what the function does with a comment.
I covered more than what I needed to give, but these are the pillars that most beginners need to understand.