how to access and use user input variables into another function

Hello, I'm new to c++ I'm trying to create a reservation system for my school project. My question is how do I can access variables from other functions because I need to check if the username is already taken.

[void login()
{
int count=0;
string user, pass, u, p;
system("cls");
cout << "\t\t\t Please enter the following details";
cout << "\n\t\t\t+ ================================== +";
cout<<"\n\t\t\t| |";
cout<<"\n\t\t\t| ================================== |\n";

cout << "\t\t\t\tUSERNAME :";
cin >> user;
cout<<"\n\t\t\t| |\n";

cout << "\t\t\t\tPASSWORD :";
cin >> pass;
cout<<"\n\t\t\t| ================================== |";
cout<<"\n\t\t\t| |";
cout<<"\n\t\t\t+ ================================== +";

ifstream input("user'sdata.txt");
while (input >> u >> p)
{
if (u == user && p == pass)

{
count = 1;
system("cls");
}
}
input.close();
if (count == 1)
{
cout << "\nHello " << user << "\n<LOGIN SUCCESSFUL>\nThanks for logging in..\n";
cout<<"Press any key to continue:";
cin.get();
cin.get();
services();
}
else
{
cout << "\nLOGIN ERROR\nPlease check your username and password\n";
main();
}
}]

[/void registr()
{

string reguser, regpass, ru, rp, user;
system("cls");
cout << "Enter the username :";
cin >> reguser;
cout << "\nEnter the password :";
cin >> regpass;


if (reguser == user)
{

system("CLS");
cout << endl;
cout << " The username is aldready taken! Please try again." << endl;
cout << endl;
cout << "=====================================================" << endl;
cout << endl;
registr();
}




ofstream reg("user'sdata.txt", ios::app);
reg << reguser << ' ' << regpass << endl;
system("cls");
cout << "\nRegistration Sucessful\n";

main();


}]
Last edited on

jeyc (3)
Hello, I'm new to c++ I'm trying to create a reservation system for my school project. My question is how do I can access variables from other functions because I need to check if the username is already taken.


you don't call main.
code tags are the word code in [] brackets on this site, or the <> button on the sidebar.

you share variables via parameters:

1
2
3
4
5
6
7
8
9
10
11
12
13
void foo(int &x) //the & means if x changes, the original changes.  If you remove it, the original is protected. 
{
   cout << x << endl; //you can use x here, it has the passed in value
   x = 314;
}

int main()
{
    int y{42};
    foo(y);
    cout << y << endl; //y is 314
}
Last edited on
can you please elaborate it more base on my code sorry because I'm quite dumb and haven't slept for a while I already tried using code tags still cant' manage to do it
your code is a trainwreck, so I ignored it to give you a simple example.
you have freestanding code that looks like it should be in main, and main is an empty line off by itself, you have random [ ] around stuff, ... it needs some work.

lets take one idea though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void registr(string &reguser_p)
{
     ... code
     cin >> reguser_p;
    ...  more code
}

void login(string &reguser_p)
{
    if( reguser_p == something)
    //whatever, use the variable
}

int main()
{
   string reguser;
   registr(reguser);
   login(reguser);   
}


that is the format. you can send as many parameters to a function as you need to with commas: void foo(int &x, double &d);

the idea is that the 'data' really lives in main, in the 'reguser' variable.
you pass that to registr, and it gets read via cin, then when that is done, the value in main has been updated. Now you send that updated value to login, which does something else with it, and so on.
the names of the parameters can BE the same as the name in main. I put that _p on it to tell it apart on purpose. If you choose to use the same names, it will be hard to debug and you will confuse yourself if you are not careful.
Last edited on
Topic archived. No new replies allowed.