I'm currently working on my final project of my first semester of C++ (and programming in general). What my professor wants us to do is to make a mock social media program that has some basic functions such as:
- Sign up
- Sign in
- Follow user
- Write a post
- View Activity – (Activity from friends)
- View Profile – (Username, followers, people you follow, posts from you)
- Sign Out
- Exit
On the prompt it says that he:
"expects to see knowledge of the material, in particular Object Oriented Design - Classes".
I assume that means that we should be using classes properly.
That brings me to my general question about the code I have, which is whether or not I'm fulfilling that "requirement" of demonstrating knowledge of using classes (or objects or whatever he's probably expecting).
project02.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "project02.h"
using namespace std;
ofstream fout;
ifstream fin;
void LoginScreen()
{
cout << "Please select from the following options by entering the corresponding number:" << endl << endl;
cout << "1. Sign up" << endl;
cout << "2. Log in" << endl;
cout << "3. User list" << endl;
cout << "4. Exit" << endl << endl;
cout << "Please make your selection (enter 1, 2, 3, or 4): ";
}
void UserInfo::signUp()
{
int offset;
string line;
bool isTaken;
string fName;
string lName;
string bYear;
string screenName;
cout << "Enter your first name: ";
cin >> fName;
setFirstName(fName);
cout << "Enter your last name: ";
cin >> lName;
setLastName(lName);
cout << "Enter your birth year: ";
cin >> bYear;
setBirthYear(bYear);
cout << "Create your screen name (no special characters or spaces): ";
do
{
cin >> screenName;
isTaken = false;
fin.open("RegisteredUsers.txt");
while (!fin.eof())
{
getline(fin, line);
if ((offset = line.find(screenName, 0)) != string::npos)
{
isTaken = true;
cout << "The username " << screenName << " is already taken, please choose a different username: ";
}
}
fin.close();
} while (isTaken = true && isTaken != false);
setScreenName(screenName);
cout << endl;
cout << "Registered with the following information:" << endl;
cout << endl;
cout << "Full name: " << getFirstName() << " " << getLastName() << endl;
cout << "Birth year: " << getBirthYear() << endl;
cout << "Screen name: " << getScreenName() << endl;
cout << "You may now log in to your newly created account." << endl;
cout << endl;
string infoFileName = screenName + "_info.txt";
string followerFileName = screenName + "_follow.txt";
string activityFileName = screenName + "_activity.txt";
registerUser(fout, screenName);
fout.open(infoFileName.c_str());
writeUserInfo(fout, fName, lName, bYear, screenName);
fout.close();
}
void writeUserInfo(ofstream & fout, string & first, string & last, string & year, string & screen)
{
fout << first + " " + last + " " + year + " " + screen + " ";
}
void registerUser(ofstream & fout, string screen)
{
fout.open("RegisteredUsers.txt", ios::app);
fout << " " + screen;
fout.close();
}
void listUsers()
{
ifstream fin;
string screen;
cout << "The following users are registered: " << endl << endl;
fin.open("RegisteredUsers.txt");
while(!fin.eof())
{
fin >> screen;
cout << screen << endl;
}
fin.close();
}
void UserInfo::signIn()
{
int offset;
string line;
bool exists;
string screenName;
cout << "Please enter your screen name to sign in: ";
do
{
cin >> screenName;
exists = true;
fin.open("RegisteredUsers.txt");
while (!fin.eof())
{
getline(fin, line);
if ((offset = line.find(screenName, 0)) == string::npos)
{
exists = false;
cout << endl;
cout << "User " << screenName << " does not exist!" << endl << endl;
cout << "Please enter an existing username to sign in: ";
}
}
fin.close();
} while (exists = false || exists != true);
cout << "You are now logged in as " + screenName << endl;
}
void UserInfo::displayProfile()
{
}
void UserInfo::setFirstName(string fName)
{
_firstName = fName;
}
string UserInfo::getFirstName()
{
return _firstName;
}
void UserInfo::setLastName(string lName)
{
_lastName = lName;
}
string UserInfo::getLastName()
{
return _lastName;
}
void UserInfo::setBirthYear(string bYear)
{
_birthYear = bYear;
}
string UserInfo::getBirthYear()
{
return _birthYear;
}
void UserInfo::setScreenName(string screenName)
{
_screenName = screenName;
}
string UserInfo::getScreenName()
{
return _screenName;
}
project02.h
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
void LoginScreen();
void writeUserInfo(ofstream & fout, string & first, string & last, string & year, string & screen);
void registerUser(ofstream & fout, string screen);
void listUsers();
class UserInfo
{
public:
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
string getBirthYear();
void setBirthYear(string year);
string getScreenName();
void setScreenName(string sn);
void signUp();
void signIn();
void displayProfile();
private:
string _firstName;
string _lastName;
string _birthYear;
string _screenName;
};
#endif // PROJECT02_H
project02main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "project02.h"
using namespace std;
int main()
{
char input;
UserInfo inputInfo;
cout << "Welcome to MyFace, a social media network where you can post your thoughts and see what your friends are up to." << endl << endl;
do
{
LoginScreen();
cin >> input;
cout << endl;
if (input == '1' && input != '2' && input != '3')
{
inputInfo.signUp();
}
else if (input == '2' && input != '3')
{
inputInfo.signIn();
}
else if(input == '3')
{
listUsers();
cout << endl;
system("PAUSE");
cout << endl;
LoginScreen();
}
else if (input != '1' && input != '2' && input != '3' && input != '4')
{
cout << "Invalid choice! Redirecting to login page..." << endl << endl;
}
else if (input == '4')
{
cout << "Thank you for using MyFace." << endl;
}
} while (input != '4');
return 0;
}
As you can see, it doesn't yet include all the functionalities and signIn()
doesn't actually do anything yet as that's where I left off. Anyway, I'm still just a beginner but please let me know if you see any potential problems or if I'm not using classes properly, or if you just have any general comments!
I don't actually have a whole lot of time to finish this so if something doesn't NEED to be fixed then I probably won't want to fix it, especially if it involves heavy restructuring or is otherwise time consuming.
string::npos
is just a simplestatic const size_type npos = -1;
. You already declaredifstream fin;
, use it instead of declaring again this (obviously, you must close the stream each time).while (exists = false || exists != true);
has no sense - it means:while (exists = false || exists = false);
. Same thing for the conditionisTaken
in the otherwhile
loop. Don't usesystem()
functions, it brings your application less portable. Use'\n'
instead ofstd::endl
. In some case you can usesprintf
withstd::string.c_str()
. You should also handle the exceptions. \$\endgroup\$ – user91060 Dec 7 '15 at 11:02=
and equality comparison==
operators there. \$\endgroup\$ – πάντα ῥεῖ Dec 7 '15 at 20:58