Using User- Inputted Variables in Main in another Function

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdlib.h>
using namespace std;
//Sign-In Portion
int login(){
    cout << "---------------------------------------- \n";
    cout << "Welcome to the sign-in page \n";
    cout << "Please sign in below to continue \n \n";
    cout << "Please enter Username: ";
    cin >> username;
    if(//variables match){
    //continue
    }
    else(//No match){
    cout << "Your username did not match";
    login();
    }
   return 0;
}
//Sign-Up Portion
int main()
{
    string username;
    string password;
    int log;

    cout << "\n Welcome to Account Setup! \n";
    cout << "Follow instructions to set-up your account! \n \n";
    cout << "----------------------------------------------------- \n";
    cout << "Please enter your Username: ";
    cin >> username;
    cout << "Please enter your Password: ";
    cin >> password;
    cout << "\n Thank You for signing up \n";
    cout << "Please Press '1' to proceed to Log-In \n Please Press '2' to RESTART \n";
    cin >> log;
    if(log == 1){
        system("CLS");
        login();
    }
    else if(log == 2){
        system("CLS");
        main();
    }

    return 0;
}
Last edited on
Post your code (remember to use [ code ] [ / code ] tags) and please, try to split up your paragraph so it's easier to read.
Last edited on
Ok, so here's one way to check strings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
string szInput;
cout << "Enter account name: ";
getline (cin, szInput, '\n');

for (unsigned int i = 0; i < szInput.length(); i++)
    szInput[i] = toupper (szInput[i]);

if (szInput == "PHIL123")
    cout << "Account name recognized.\n";

cout << "Enter password: ";
getline (cin, szInput, '\n');

for (unsigned int i = 0; i < szInput.length(); i++)
    szInput[i] = toupper (szInput[i]);

if (szInput == "SUPER SECRET PASSWORD")
    cout << "Account name recognized.  Password verified.  Granting access.\n";
...


Here's another way (that's more applicable in your case)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string> // pretty important for this
using namespace 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.
Last edited on
Thanks alot that clarified quite a bit of stuff. I appreciate the help.
Topic archived. No new replies allowed.