Lab Social Network in C++

2019

Social Network in C++

Git
Social Network in C++

A simple C++ example of a social network.

How to run the program (Ubuntu)

  1. Open terminal.
  2. Install gcc (g++), the C++ compiler.
    $ sudo apt install g++
    Or install build-essential, which also installs libraries as well as the gcc compiler.
    $ sudo apt-get install build-essential
  3. Check the gcc version.
    $ gcc --version
  4. Go to the folder of the program.
    $ cd /path_to_folder/folder
  5. Compile the C++ program (main.cpp).
    $ sudo g++ main.cpp
  6. Run the C++ program.
    $ ./main

Folders and Files

Folders

The only folder that exists is the folder of the header files (header_files). More details about the header files later.

User files

Each user file has as its file name the name of the corresponding user, and contains the other users with whom they are friends. For example, the file named kostas contains all friends of user kostas.

The following files have already been created for easier understanding and use of this social network:

  • kostas
  • maria
  • tom
  • george
  • anna
  • john

Network files

  • conversations — Contains conversations between users and their friends.
    Logged_in_user : post_of_logged_in_user
                     reply_from_friend_user (name_of_friend)
  • database — Contains pairs of usernames and passwords from all registered users in the Social Network.
    user1
    password_of_user1
    user2
    password_of_user2
  • likes — Contains posts of users and likes from friends and their names.
    Logged_in_user : post_of_logged_in_user
                     LIKE! (name_of_friend)
  • posts — Contains posts from all registered users in the Social Network.
    user_name_1
    post_of_user_name_1
    user_name_2
    post_of_user_name_2
  • Requests — Contains friend requests that have been sent from a user to another user with whom they are not friends.
    user_sent_request -> user_to_accept/reject_request
  • TempReq — A temporary file used in the program.
  • users — Contains only the names of all registered users in the Social Network.

Basic header files

The main header files used in the program are:

iostream, fstream, sstream, unistd.h

Local header files

The header files contained in the header files folder, used in the program, are:

  • mainmenu.h
  • registration.h
  • login.h
  • menu2.h
  • viewposts.h
  • viewfriendposts.h
  • menu3.h
  • friendrequest.h
  • friends.h
  • deletefriends.h
  • listusers.h
  • goback.h
  • exxit.h

mainmenu.h

The very first menu that the user sees. The user can select one of the 3 available choices by pressing the corresponding number:

  1. Register
  2. Log In
  3. Exit the program

If the user presses a number different from the given ones, he goes back to the first menu.

registration.h

The user types their desired username and password. If either already exists, the user is asked to repeat the process. If the process is done correctly, he can choose from 3 options:

  1. Log In
  2. Return to main menu
  3. Exit the program

login.h

Initially a check is made whether the user is already registered or not. Then the user enters his username and password and goes to the second menu.

menu2.h

After the user logs in successfully, he enters the Social Network menu where he can choose one of the following options:

  1. My wall
  2. Friend’s wall
  3. Send friend request
  4. Accept/Reject friend request
  5. My friends
  6. Delete friend
  7. Back
  8. Log out
  9. Exit the program

viewposts.h

By choosing My wall from the above menu, the user can see one of the following:

  1. His own posts
  2. His friends’ posts
  3. Conversations with friends
  4. Likes from friends on his own posts

Finally he is asked whether to return to the Social Network menu or not.

viewfriendposts.h

By selecting Friend’s wall from the menu, the user can choose one of the following options:

  1. Post a message on his wall
  2. Reply to a post
  3. Leave a Like
  4. Main Menu
  • If he chooses the first one (i), he can write the post that he wants, and this will be added to the posts file at the end after the login name is added above.
  • If he chooses the second option (ii), he can choose one of his friends, view the friend’s posts, and choose the post he wants to reply to. The user then writes the reply, which is stored in the conversations file as a reply to the friend’s post, saved with his name in brackets at the end. Finally, the logged-in user can reply to another post of the same friend if he wants.
  • If he chooses the third option (iii), the process is exactly the same as before, but instead of a reply, the user leaves a LIKE! which is stored in the likes file under the selected friend’s post, before the older likes.
  • If he chooses the 4th option (iv), he returns to the Social Network menu.

menu3.h

By selecting Send friend request, the logged-in user can send a friend request to one of the registered users with whom he is not already friends. By pressing the number that corresponds to the desired friend-user, the request is saved in the Requests file as:

user_to_sent_request -> user_to_accept/reject_request

Finally, the user can also choose whether to send a friend request to other non-friend users or not.

friendrequest.h

By selecting Accept/Reject friend request, the logged-in user can check all the requests he has received and press the number corresponding to the name of the user whose request he wants to accept. When a request is accepted, the two users are added to each other’s file as friends, and the request is deleted from the Requests file. If there are additional friend requests but the user exits the Accept/Reject friend request menu, this is equivalent to rejecting the rest of the requests.

friends.h

By selecting My friends, the logged-in user can check the other registered users with whom he is friends, and then return to the Social Network menu if he wants.

deletefriends.h

By selecting Delete friend, the logged-in user can see a list of his friends and, if he wants, delete one of them by typing the number corresponding to that friend. The two involved users are then removed from each other’s file. Finally, the user can delete more friends if he wants.

listusers.h

Counts the number of all registered users by reading the pairs (username, password) from the database file.

goback.h

By selecting Back, the logged-in user can return to the menu or leave the Social Network and the program. If the user selects the Log out option, he returns to the first menu of the program.

exxit.h

By selecting Exit the program, the logged-in user leaves the Social Network. The program terminates and a message appears, like the following:

Program termination
Thanks for your visit logged_in_user_name !

Frequently Asked Questions

What can users do in the social network?

Register and log in, post on their own wall, view a friend’s wall, reply to posts and leave a LIKE, send, accept or reject friend requests, view and delete friends, and see how many users are registered.

Where is the data stored?

In plain text files rather than a database. Each user has a file listing their friends, and shared files hold the accounts, usernames, posts, conversations, likes and pending friend requests.

How do I compile and run it?

Compile main.cpp with g++ (for example on Ubuntu with build-essential installed) and run ./main. Six sample users are already created: kostas, maria, tom, george, anna and john.

Is it safe to use with real passwords?

No. It is a learning project and the passwords are stored as plain text in the database file.

Badge