So basically i have a c++ project and my teacher has asked me to write a program that not only can do the log in and register part but even put the friend requests and friends or followers on the game.
I have tried to create the log in and register part but i have a problem. Every time i register a username it adds to the database file, but when i try to log in with the last account that i have registered it says process returned (0) and closes the program. But if i try to log in with the first ever account created it gives me a successfully log in message.
Plus there are some problems with the switches i think, since when i type to exit after i have logged in tries to register another account and will not exit the program.
Hopefully some one can help me fix these problems, i am not a beginner though but not too advanced in c++. If you have any ideas about the friends and requests part through the arrays please let me know it would help me so much.
Grateful to anyone that could give the smallest hint possible.
Here is my code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string regname, regpass; //name and password to register
string logname, logpass; //name and password to log in
string name, password; //name and password existing in the database
int menu1(); //main menu
void menu2(); //menu after the successful login
void menu3(); //menu to add and delete the friends
int reg(); //the registration process
void login(); //login process
int exit(); //to exit the program
void request(); //view the requests
void friends(); //view the friends
switch (choice)
{
case 1:
reg();
break;
case 2:
login();
break;
case 3:
exit();
break;
default:
cout << "Wrong choice.!" << endl;
break; //not sure if this is the right thing to do in a switch
}
}
switch (a)
{
case 1:
request();
break;
case 2:
friends();
break;
case 3:
menu3();
break;
case 4:
menu3();
break;
case 5:
menu1();
break;
default:
cout << "Wrong choice!" << endl;
break;
}
}
void menu3()
{
}
int reg()
{
cout << endl;
cout << "Write the desired username: " << endl;
getline( cin, regname ); //get the username as a input to add it to the database
cout << "Write the desired password! " << endl;
getline ( cin, regpass ); //gets the password to add it to the database
if ( regpass.length() < 6) //if the password is less than 6 chars try the reg process again
{
cout << endl;
cout << "The chosen password is too short!" << endl;
cout << "The password needs to be at least six characters long." << endl;
cout << endl;
reg();
}
else
{
ofstream g( "database.txt", ios::app ); //creating the database file and the ios::app lets you
//write in the same file without deleting the content of the database file
if (!g.is_open())
{
cout << "Problem accessing the database." << endl;
return 0;
}
else
{
g << regname; //if the name and the pass are okay add them at the database
g <<'\n';
g << regpass;
g <<'\n';
g <<'\n';
It's because in your login method you only read the two first lines of the file (Which is the first registered accounts username & password)
And so when you compare the inputted username & password to the ones you read from the file it will only be true when you login with the first account you registered.
You'll probably want to read the whole entire file and then compare every username & password in it with the inputted username & password.
Yeah i understand the f is just a name but the the line has the compilator says that line was not declared in this scope and idk how to declare it as a int or string