Social Network in C++. Help!!

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

int menu1()
{
cout << endl;
cout << "Type: " << endl;
cout << "1: Register." << endl;
cout << "2: Log In." << endl;
cout << "3: Exit the program." << endl;
cout << endl;

int choice;
cin >> choice;
cin.ignore(256,'\n');

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
}
}

void menu2()
{
cout << endl;
cout << "1: View the requests." << endl;
cout << "2: View the friends." << endl;
cout << "3: Add friend." << endl;
cout << "4: Delete friend." << endl;
cout << "5: Log out." << endl;
cout << endl;

int a;
cin >> a;

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';

g.close(); //close the database file

cout << endl;
cout << "You have successfully registered!" << endl;
cout << "Type:" << endl;
cout << "1.Log In." << endl;
cout << "2.Exit the program." << endl;
cout << endl;

int a;
cin >> a;
cin.ignore();

switch (a)
{
case 1:
login();
break;
case 2:
exit();
break;
default:
cout << "Wrong choice!" << endl;
break;
}
}
}
}

void login()
{
ifstream f;
f.open( "database.txt" ); //accessing the database file

if (!f.is_open())
{
cout << endl;
cout << "You have to register first!" << endl;
menu1();
}

getline(f, name, '\n'); //reading the usernames in the database

getline(f, password, '\n'); //reading the passwords from the database

f.close();

cout << endl;
cout << "Write your username: " << endl;
getline( cin, logname ); //the username to login

cout << "Write your password: " << endl;
getline( cin, logpass ); //password to login

if ( logname == name && logpass == password )
{
cout << endl;
cout << "Successfully logged in! " << endl;
cout << "Welcome, " << endl;
cout << logname << endl;
menu2();
}
else if ( logname == name && logpass != password )
{
cout << endl;
cout << "Wrong password!" << endl;
cout << endl;
login();
}
else if ( logname != name && logpass == password )
{
cout << endl;
cout << "Wrong username!" << endl;
cout << endl;
login();
}
}

int exit()
{
cout << endl;
cout << "Press enter to exit the program! " << endl;

return 0;
}

void request()
{

}

void friends()
{

}

int main()
{
cout << "Welcome to the C++ social network!" << endl;
menu1();
}
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.

Hope this helps
Last edited on
Yeah i thought that might be the problem but i don't know how to read the whole file and compare the values of the variables.
Heres a quick example of some code that reads every line in the file and compares the username & passwords
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for( string username; getline( f, username, '\n'); )
{
     string password = getline(f,line,'\n');

     if(logname == username)
     {
         //Inputted username matches with another username in the file
         if(logpass == password)
         {
             //password also matches. login successful !
         }
    
     }
}
Last edited on
I don't understand the f, line. Should be declared as a number to give the total number of lines in the file?
The f is your ifstream ( i just used the same name as you gave it)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ifstream f;
f.open( "database.txt" ); //accessing the database file

for( string username; getline( f, username, '\n'); )
{
     string password = getline(f,line,'\n');

     if(logname == username)
     {
         //Inputted username matches with another username in the file
         if(logpass == password)
         {
             //password also matches. login successful !
         }
    
     }
}
Last edited on
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
Ohh yeah sorry the code should be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ifstream f;
f.open( "database.txt" ); //accessing the database file

for( string username; getline( f, username, '\n'); )
{
     string password;
     getline(f,password,'\n');

     if(logname == username)
     {
         //Inputted username matches with another username in the file
         if(logpass == password)
         {
             //password also matches. login successful !
         }
    
     }
}


dont know what i was thinking
Last edited on
Well i tried but the problem is not solved it only logs in the first name on the database.


void login()
{
ifstream f;
f.open( "database.txt" ); //accessing the database file

if (!f.is_open())
{
cout << endl;
cout << "You have to register first!" << endl;
menu1();
}

else
{
cout << "Write your username:" << endl;
getline( cin, logname );

cout << "Write your password:" << endl;
getline( cin, logpass );

for( string username; getline( f, username, '\n'); )
{
string password;
getline( f, password ,'\n' );

if( logname == username )
{
if( logpass == password )
{
cout << "Successfully logged in!" << endl;
}
else
{
cout << "Wrong password!" << endl;
}
}
else
{
cout << "Wrong username!" << endl;
}
}
}
}
I figured it out. I just tried it with a while loop instead of a for loop and worked. I really appreciate your help. Thank you!
Topic archived. No new replies allowed.