-1

I tried to build a Social Media type program in CLI using C++ in Visual Studio. I came across a problem where I need an attribute from base class (USN) which has some value set in Auth::login() and I can't access it in any other class. There is no ambiguity,inheritance does not work here for me, what is the actual problem?

classes.cpp
#include<iostream>
#include <Windows.h>
#include<conio.h>
#include<string>
#include<cstring>
#include <cstdio>
#include "queries.cpp"
#define clr cout<<"\033[2J\033[1;1H"  // to clear the screen



using namespace std;

class User {
public:
    string first_name;
    string last_name;
    string USN;   // this is the variable i need and this variable becomes empty when accessed from a different class other than Auth()
    string Deptt;
    string username;
    string gender;
    string _dob;
    string email;
    string password;
    string re_password;
    string password_enc;
    User() {};
    
    virtual void setUSN();    // i created this method so that the USN would store in the class itself,, tried cin>>USN also but that didnt work
    string getUSN();
};


class Auth :public User
{
public:
    Auth() {};
    
    
    void login();    // in login and signup i read USN from the user and it works only in the scope of Auth class;
    void signup(); 
    void enterPass1();
    void enterPass2();
    string encryptString(string S);

    
};

class HomePage:public User{
public:
    int choice;
    HomePage() {};
    void BuildLayout();

};
class ProfileHandler : public HomePage{
public:
    ProfileHandler() {};        /// for eg i tried to access the USN in this class (cout<<"usn: "<<USN;) that resulted in "usn: " only it didnt print the USN which was stored in the Auth class
    void viewProfile();
    //defined in reference.cpp
};

class Comment :public HomePage {
public:
    Comment() {};
    string comment_by;
    string comment_on;
    string comment_body;


};

class Post : public HomePage {
public:
    string posted_by;
    string post_title;
    string post_body;
    string posted_on;
    int likes_count;
    Post() {}

};


class MessageHandler {
public:
    MessageHandler() {};
};

The methods are defined in this code:

#pragma
#include "classes.cpp"
#include "dope_cpp.cpp"  // this is a custom header file for adding colours and ascii graphics to the code


void User::setUSN()
{
    cin >> USN;
}
string User::getUSN()
{
    return USN;
}

void Auth::login()
{
    Decor().green_black();
    cout << "\t\t\t\t           login            \n\n";
    char usn_str[] = " \n\t\t\tUSN     : ";
    char pwd_str[] = " \t\t\tPassword: ";
    //string user;
    //char password[16];
    //string password_enc = "";
    //string re_password;
    int p = 0;
    //cout << "\t\t\t\t\t\tLogin Page" << endl<<endl<<endl;
    Decor().d_lblue_black(usn_str);
    Decor().lblue();
    cout << " ";
    setUSN();
    string &usn = USN;
    cout << endl;
    Decor().d_lblue_black(pwd_str);
    cout << " ";
    Decor().lblue();
    enterPass1();
    Decor().reset();
    password_enc = "";
    password_enc = encryptString(password);
    cout << endl << endl;
    cout << "---->" << USN;// here USN prints 
    if (Queries().isAuthenticated(USN, password_enc) == true)
    {

        clr;
        Decor().yellow();
        cout << endl << endl << "Redirecting to Homepage...\n";
        Sleep(2000);
        HomePage().BuildLayout();

    }
    else {
        int choice;
        Decor().red();
        cout << endl << "\t\t\tUsername and /or Password does not match" << endl;
        Decor().purple();
        cout << "\t\t\tDo you want to:\n\t\t\t1.Signup for an Account\n\t\t\t2.Re-enter the credentials\n\t\t\t3.Exit\n";
        Decor().lblue();
        cin >> choice;
        Decor().reset();
        switch (choice)
        {
        case 1:
            clr;
            Decor().yellow();
            cout << "Redirecting to Signup...\n";
            Sleep(1000);
            signup();
            break;
        case 2:
            clr;
            Decor().kle_logo();
            login();
            break;
        case 3:
            clr;
            exit(0);
        default:
            Decor().red();
            cout << "Invalid Choice" << endl;
            Decor().reset();
            break;
        }
    }
}
string Auth::encryptString(string S)
{
    string result = "";
    for (int i = 0; i < S.length(); i++)
    {
        int temp = S[i];
        result += to_string(temp);
    }
    return result;
}
void Auth::enterPass1()

{
    char ch;
    string pass;
    ch = _getch();
    while (ch != '\r') {
        pass.push_back(ch);
        cout << '*';
        ch = _getch();
    }
    password = pass;

}
void Auth::enterPass2()
{

    char ch;
    string pass;
    ch = _getch();
    while (ch != '\r') {//character 13 is enter
        pass.push_back(ch);
        cout << '*';
        ch = _getch();
    }
    re_password = pass;

}
void Auth::signup()
{
    clr;
    Decor().kle_logo();
    string month;
    string year;
    string day;
    char first_n_str[] = "\n\t\t\tEnter your First name : ";
    char last_n_str[] = "\n\t\t\tEnter your last name : ";
    char email_str[] = "\n\t\t\tEnter your email : ";
    char USN_str[] = "\n\t\t\tEnter your USN : ";
    char birth_date_str[] = "\n\t\t\tEnter your birthdate:";
    char birth_year_str[] = "\n\t\t\tEnter the year of your birth(YYYY): ";
    char birth_month_str[] = "\n\t\t\tEnter the month of your birthdate(MM): ";
    char birth_day_str[] = "\n\t\t\tEnter the day of your birth(DD): ";
    char dept_str[] = "\n\t\t\tEnter your respective department: ";
    char gender_str[] = "\n\t\t\tEnter your gender(Male/Female/Other): ";
    char pass_str[] = "\n\t\t\tChoose password(upto 16 characters): ";
    char pass_re_str[] = "\n\t\t\tEnter password again: ";
    Decor().green_black();
    cout << "\t\t\tNice choice!\n\n";
    cout << "\t\t\tSo, let's get you an account\n";
    int p = 0;
    bool at_found = false;

start:
    // first name
    Decor().reset();

    Decor().blue();
    cout << first_n_str;
    cin >> first_name;
    cout << endl;
    if (first_name != " ")
    {
        Decor().blue();
        cout << last_n_str;
        cin >> last_name;
        if (last_name != " ")
        {
            cout << endl;
        email:
            Decor().blue();
            cout << email_str;
            cin >> email;
            for (int i = 0; i < email.size(); i++)   ///todo;;;;;;
            {
                if (email[i] == '@')
                {
                    at_found = true;
                    goto usn;
                }

            }
            at_found = false;
            if (at_found == true)
            {
                goto usn;
            }
            else
                Decor().red();
            cout << endl << "\n\t\t\tThere is no '@' in your email, please check and try again... " << endl;
            Decor().reset();
            goto email;
        usn:
            Decor().blue();
            cout << USN_str;
            Decor().reset();

            cin >> USN;
            if (USN.length() == 12)
            {
                username = USN;
                cout << endl;
                Decor().reset();

                Decor().blue();
                cout << birth_date_str;
                cout << "\n";
                Decor().green();
                cout << birth_year_str;
                Decor().reset();

                cin >> year;
                Decor().green();
                cout << birth_month_str;
                Decor().reset();

                cin >> month;
                Decor().green();
                cout << birth_day_str;
                Decor().reset();

                cin >> day;
                _dob = year + "-" + month + "-" + day;
                cout << endl;
                Decor().blue();
                cout << dept_str;
                Decor().reset();

                cin >> Deptt;
                Decor().blue();
                cout << gender_str;
                Decor().reset();

                cin >> gender;
            pswd:
                Decor().yellow();
                cout << pass_str;
                Decor().reset();

                enterPass1();
                Decor().yellow();
                cout << pass_re_str;
                Decor().reset();

                enterPass2();
                if (password.compare(re_password) != 0)
                {
                    Decor().red();
                    cout << "\n\n\t\t\tPassword mismatch....\n\t\t\tRe-Enter the passwords\n";
                    Decor().reset();

                    goto pswd;
                }
                if (password.length() < 8)
                {
                    Decor().red();
                    cout << "\n\t\t\tPassword must be atleast 8 characters long" << endl;
                    Decor().reset();

                    goto pswd;
                }
                else if (password.length() > 16)
                {
                    Decor().yellow();
                    cout << "Password character limit is 16, you have entered more than that\n";
                    cout << "Choose password again\n";
                    Decor().reset();

                    goto pswd;
                }

                password_enc = encryptString(password);
            }

            else if (USN.length() != 12) {
                Decor().red();
                cout << "\nUSN must be of 12 characters\nSee your Id card for USN\n\nStart again\n";
                cout << endl << endl;
                Decor().reset();

                goto start;
            }
        }
        else goto start;

    }
    else goto start;
    Decor().green();
    cout << "\n\t\t\tGreat!\n";
    Decor().reset();

    Sleep(1000);
    Decor().yellow();
    cout << "\n\t\t\tSaving your information to the Database\n";
    Decor().reset();

    if (Queries().saveToDb_user(first_name, last_name, USN, Deptt, username, gender, _dob, email, password_enc) == true)
    {
        Decor().green();
        cout << "\n\t\t\t[0]Account created succesfully\n";
        Decor().reset();

        Sleep(2000);
        clr;
        Decor().yellow();
        cout << "Redirecting to Login Page...\n";
        Sleep(2500);
        clr;
        Decor().kle_logo();
        login();
    }
    else {
        Decor().red_black();
        cout << "\n\t\t\tThere was some problem while saving your information to cloud\n";
        Decor().reset();
        Decor().red();
        cout << "\n\t\t\tCheck your internet and try again...\n";
        Sleep(1000);
        goto start;
    }
    //user  -> (first_name, last_name, _dob, follower_count, username, password_enc, email, USN, Deptt)

}
void HomePage::BuildLayout()
{
    int choice;
    clr;      // clr is to clear the screen
    cout<<"usn::"<<USN;   // here the USN is empty
    Decor().kle_logo();
    Decor().yellow_black();
    cout << "\t\t\t\t\tHome Page" << endl;
    Decor().reset();
    Decor().blue();
    cout << "(1) PROFILE";
    Decor().lblue();
    cout << "\t\t\t\t\t\t\t\t\t\t\t\t(2) Messages";
    Decor().red();
    cout << endl << "(3) LOGOUT";
    Decor().purple_black();
    cout << "\t\t\t\t\t\t\t\t\t\t\t\t(4) Post";
    Queries().display_post();

    Decor().reset();
    cout << endl;

    cin >> choice;
    switch (choice)
    {
    case 1:
        clr;
        cout << endl << endl;
        Decor().yellow();
        cout << "Redirecting to profile page...";
        Sleep(2000);
        clr;
        Decor().kle_logo();
        ProfileHandler().viewProfile();
        break;
    case 2:
        clr;
        cout << endl << endl;
        Decor().yellow();
        cout << "Redirecting to Messages...";
        Sleep(2000);
        clr;
        Decor().kle_logo();
        //MessageHandler* obj;
        break;
    case 3:
        clr;
        char logout;
        Decor().red();
        cout << endl << endl;
        cout << "Do you want to logout(y/n)? : ";
        cin >> logout;
        Decor().reset();
        if (logout == 'y')
        {
            clr;
            cout << endl << endl;
            Decor().yellow();
            cout << "Logging you out...";
            Sleep(1000);
            clr;
            cout << endl << endl;
            cout << "Logout Successfull...\n\nRedirecting to landing page\n";
            Sleep(2000);
            clr;
            Decor().kle_logo();
        }
        break;
    case 4:
        Decor().kle_logo();
        Decor().green_black();
        cout << "\n\t\t\t\t    New Post    \t\t\t\t";
        Decor().reset();


    default:
        break;


    }
}
void ProfileHandler:: viewProfile()
{
    int choice;
    string* info = new string[100];
    info = Queries().displayProfile(first_name, last_name, USN, Deptt, username, gender, _dob, email, password_enc);
    cout << "usn: " << USN;
        first_name = info[0];
        last_name = info[1];
        USN = info[2];
        username = info[3];
        email = info[4];
        Deptt = info[5];
        gender = info[6];
        _dob = info[7];
        password_enc = info[8];
        Decor().blue_black();
        cout << "\n\t\t\tFirst name:          \n";
        Decor().reset();
        Decor().yellow();
        cout << first_name << endl;
        Decor().blue_black();
        cout << "\n\t\t\tLast name:           \n";
        Decor().reset();
        Decor().yellow();
        cout << last_name;
        Decor().blue_black();
        cout << "\n\t\t\tUSN:                 \n";
        Decor().reset();
        Decor().yellow();
        cout << USN << endl;
        Decor().blue_black();
        cout << "\n\t\t\tUsername:            \n";
        Decor().reset();
        Decor().yellow();
        cout << username;
        Decor().blue_black();
        cout << "\n\t\t\tEmail:               \n";
        Decor().reset();
        Decor().yellow();
        cout << email << endl;
        Decor().blue_black();
        cout << "\n\t\t\tDate of Birth:       \n";
        Decor().reset();
        Decor().yellow();
        cout << _dob;
        Decor().blue_black();
        cout << "\n\t\t\tDepartment:          ";
        Decor().reset();
        Decor().yellow();
        cout << Deptt << endl;
        Decor().green_black();
        cout << "\n\t\t\tGender:              ";
        Decor().reset();
        Decor().yellow();
        cout << gender;
        Decor().red_black();
        cout << "\n\t\t\tEncrypter Password:  ";
        Decor().reset();
        Decor().yellow();
        cout << password_enc;
        cout << endl << endl;
        Decor().red();
        cout << "Enter 44 to go back: ";
        cin >> choice;
        if (choice == 44)
        {
            clr;
            cout << endl << endl;
            Decor().yellow();
            cout << "Redirecting to Home page...";
            Sleep(2000);
            HomePage().BuildLayout();

        }
}



User* item()
{
    return nullptr;
}

Class diagram

0 Answers0