c++ login system

I'm new to c++ and I'm trying to create a login system for my school project
this is my code:


#include <iostream>
#include<ctime>
#include<string>
#include<fstream>

using namespace std;

int
main ()
{
system ("ClS");
system ("Color F1");


int check1 = 0, invalid = 0;
string fname1, lname1, choose, username1[100], pass2[100], fname[100],
lname[100], user1, pass1, uname = " ", pass = " ";
string choose1, choose2, filename, txt = "-usernamefile.txt";
bool returntoSignupPage, HomePage, mainmenu = false, validLogin, redo2 =
false, redo3 = false;
fstream username ("user1.txt", ios::in | ios::out | ios::app),
password ("pass1.txt", ios::in | ios::out | ios::app);
fstream firstname ("firstname.txt", ios::in | ios::out | ios::app),
lastname ("lastname.txt", ios::in | ios::out | ios::app);
fstream myfile2 ("username.txt", ios::in | ios::out | ios::app),
myfile3 ("pass.txt", ios::in | ios::out | ios::app);




do
{
//----------------------->> HOMEPAGE <<-----------------------//
system ("CLS");
cout << "WELCOME TO 3J'S HOTEL!" << endl;
cout << endl;
cout << "[1]SIGNUP" << endl;
cout << "[2]LOGIN" << endl;
cout << "[3]EXIT" << endl;
cout << endl;
cout << "Choose the number you would like to do: ";
cin >> choose;
cout << endl;

//------------------------>> READING IN THE FILE <<------------------------//
int counter = 0, counter1 = 0, counter2 = 0, counter3 = 0;
ifstream myfile2;
myfile2.open ("username.txt");

if (myfile2.fail ())
{
cout << "Error Opening the file username.txt." << endl;
exit (1);
}

while (myfile2.good ())
{

myfile2 >> username1[counter];
counter++;

}
myfile2.close ();


ifstream myfile3;
myfile3.open ("pass.txt");

if (myfile3.fail ())
{
cout << "Error Opening the file pass.txt." << endl;
exit (1);
}

while (myfile3.good ())
{

myfile3 >> pass2[counter1];
counter1++;

}
myfile3.close ();

ifstream myfilefirstname;
myfilefirstname.open ("firstname.txt");
if (myfilefirstname.fail ())
{
cout << "Error Opening the file firstname.txt." << endl;
exit (1);
}

while (myfilefirstname.good ())
{
getline (myfilefirstname, fname[counter2]);
//myfilefirstname >> line3[counter2];
counter2++;

}
myfile3.close ();

ifstream myfilelastname;
myfilelastname.open ("lastname.txt");
if (myfilelastname.fail ())
{
cout << "Error Opening the file lastname.txt." << endl;
exit (1);
}

while (myfilelastname.good ())
{
getline (myfilelastname, lname[counter3]);
;
counter3++;

}
myfilelastname.close ();
//------------------------------------------------------------------//

if (choose == "1") //----------->> FOR SIGNUP <<-----------//
{
do
{
//----------------------->> SIGNUP PAGE <<-----------------------//
cin.ignore ();
system ("CLS");
cout << "====================SIGNUP====================" <<
endl;
cout << "FIRST NAME: ";
getline (cin, fname1);

cout << "LAST NAME: ";
getline (cin, lname1);

cout << "USERNAME: ";
cin >> uname;

for (int cun = 0; cun < counter; cun++)
{
if (uname == username1[cun])
{

system ("CLS");
cout << endl;
cout <<
" The username is aldready taken! Please try again."
<< endl;
cout << endl;
cout <<
"====================================================="
<< endl;
cout << endl;
cout << "Press 1 and enter to continue: ";
cin >> choose1;
returntoSignupPage = true;
break;

}
else
{
if (cun == (counter - 1))
{

if (username.is_open ())
{
username << user1 << endl;
}

if (firstname.is_open ())
{
firstname << fname1 << endl;
}

if (lastname.is_open ())
{
lastname << lname1 << endl;
}

cout << "PASSWORD: ";
cin >> pass1;

if (password.is_open ())
{
password << pass1 << endl;
}

cout << endl;
system ("CLS");
cout <<
"====================================================="
<< endl;
cout << endl;
cout <<
" You have succesfully created your account"
<< endl;
cout << endl;
cout <<
"====================================================="
<< endl;
cout << endl;
cout << "Press 1 and enter to go to Home Page: ";
cin >> choose1;
HomePage = true;
returntoSignupPage = false;
break;
}
returntoSignupPage = false;
}
}
}
while (returntoSignupPage);

}
else if (choose == "2")
{

//------------------>> LOGIN PAGE <<------------------//
do
{
system ("CLS");
cout << "====================LOGIN====================" << endl;
cout << "Username: ";
cin >> user1;

for (int check = 0; check < counter; check++)
{
if (user1 == username1[check])
{
//------->> This is to get the name of the user correcty<<-------//
if (check == counter - 1)
{
check1 = check;
}
else
{
check1 = check;
}

//------------------------------------------------------//
do
{
cout << "Password: ";
cin >> pass1;

if (pass1 == pass2[check])
{
cout << endl;
system ("CLS");
cout <<
"=============================================================="
<< endl;
cout << endl;
cout << "\t \t You have succesfully login.\t "
<< endl;
cout << endl;
cout <<
"=============================================================="
<< endl;
cout << "Enter any number to continue: ";
cin >> choose1;
cout << endl;
filename = user1 + txt;
validLogin = true;
redo2 = false;

}
else
{
system ("CLS");
cout << endl;
cout << "Incorrect Password! Please try again."
<< endl;
cout << endl;
cout <<
"==================================================="
<< endl;
redo2 = true;
invalid++;
cout << endl;

if (invalid == 3)
{
system ("CLS");
cout << endl;
cout <<
"You have reach the maximum retries!" <<
endl;
cout << endl;
cout <<
"==================================================="
<< endl;
cout <<
"Press 1 and enter to go to Home Page: ";
cin >> choose2;
redo2 = false;
invalid = 0;
}
}
}
while (redo2);
redo3 = false;
break;
}
else
{
if (check == (counter - 1))
{
system ("CLS");
cout << endl;
cout << "Username Not Found! Please try again." <<
endl;
cout << endl;
cout <<
"==================================================="
<< endl;
cout << "Enter any number to continue: ";
cin >> choose2;
redo3 = true;
invalid++;

if (invalid == 3)
{
system ("CLS");
cout << endl;
cout << "You have reach the maximum retries!" <<
endl;
cout << endl;
cout <<
"==================================================="
<< endl;
cout <<
"Press 1 and enter to go to Home Page: ";
cin >> choose2;
redo3 = false;
invalid = 0;
break;
}
}
}

}

}
while (redo3);

HomePage = true;

if (validLogin == true)
{
HomePage = false;





}
}
}

return 0;
}



I'm getting this error :

main.cpp:367:11: error: expected ‘while’ before numeric constant
367 | return 0;
| ^
main.cpp:367:11: error: expected ‘(’ before numeric constant
367 | return 0;
| ^
| (
main.cpp:367:12: error: expected ‘)’ before ‘;’ token
367 | return 0;
| ~^
| )
Does anyone know how to fix it? and how to improve my login system.

welcome!
please put code tags around this, edit your post and highlite the code part then hit the <> on the sidebar or type code and /code inside [] brackets to block the code part off. It is too big for plain text viewing.

while(redo3); //infinite loop

I believe you have a do-while loop that is missing the while part.

the first thing to improve is to get rid of the redo 1/2/3 and stop repeating the same blocks of code. Loop 3 times (for loop), and if they fail 3 times, tell them to go away.
consider

bool loggedin{};
for(int try = 0; try < 3 && !loggedin; try++)
{
get user and password, see if it is correct
if correct, loggedin = true (or, slightly better if it is possible and readable, loggedin = (user==good && password == good) without the extra explicit if statement)
}

if(loggedin) //after the loop logic
{
allow access to something
}
else
{
tell them to go away, too many tries and blah blah.
}

if you want to get fancy, once you have this much working, you can try encrypting the file on the computer.
Last edited on
Topic archived. No new replies allowed.