Hello, I've just starting learning C++ and I'm having a lot of fun messing around with it and creating little command console applications and I am currently working on a mock "Sign-Up" and "Sign-In" program where the user enters a Username and Password during "Sign-Up" and they get stored in there own variables. I then want it to take them to a "Sign-In" section where they enter the Username and Password they just created to log-in. I am now trying to get the "Sign-In" portion set up in its own function.
I have finished the part where the user enters their Username, then I have an "If" statement set up that I would like to analyze the "username" variable in "main" and check to see if the username entered in the "Sign-In" portion matches and if not to output a text saying it is incorrect. However, I can not seem to find out how to create this cross-reference, I have looked all over Google to find a solution to solve it but I have yet to find one so I figured I would open my own forum post. I apologize in advance if someone has already asked this I have searches all over the forums for a situation like this but have yet to find one. Thanks in advance for the help.
#include <iostream>
#include <string> // pretty important for this
usingnamespace std;
void Login (const string & szUsername);
int main ()
{
string szUserName;
cout << "Enter user name: ";
// let's say they enter Bob
getline (cin, szUserName, '\n');
// szUsername == "Bob"
Login (szUsername);
cin.get();
return 0;
}
void Login (const string & szUsername)
{
string szTemp;
cout << "Double check to ensure you typed in the right user name: ";
getline (cin, szTemp, '\n');
// typed in Bob
// szTemp == "Bob"
if (szTemp == szUsername)
cout << "User names match.\n";
else
cout << "Error, incorrect information.\n";
}
Also,
1 2 3 4 5 6 7 8 9 10 11 12
void login(); // this is called a function prototype, now doesn't that look much neater? :)
int main ()
{
...
return 0;
}
void login() // Function definitions should go below your main function
{// also, if you aren't using the integer that the function returns, then the function should be type void
...
}
If you're going to use a int login () function, then there should be a reason for it
1 2 3 4 5 6 7
int a;
a = login (); // function returns a 0, therefore initializing 0 to integer a
// kind of looks like this, except it executes the code in your login () function
a = 0;
login (); // bad! don't do this
a = login (); // much better :)
Posted a few things - done editing my post now, so take a look. Let me know if you have any questions or if you need anything clarified.