mardi 29 décembre 2020

I'm getting an error in my c++ program in which I have separated a class program into three parts (one .h and two .cpp files)

I have written a program to insert the first name, last name and the status of a user. I am getting no errors in my files but still I'm getting the wrong output. I'm writing my program using Visual Studio Code. Here's my code for my main.cpp file.

#include <iostream>
#include <string>
#include "user.h"

int main()
{
    User user;
    std::cin >> user;
    std::cout << user << std::endl;
}

Here's the code for my user.cpp file.

#include <iostream>
#include <string>
#include "user.h"

        int User::get_user_count()
        {
            return user_count;
        }
        std::string User::get_status()
        {
            return status;
        }
        void User::set_status(std::string status)
        {
            if (status == "Gold" || status == "Silver" || status == "Bronze")
            {
            this->status = status;
            }
            else
            {
                this->status = "No status";
            }     
        }
        User::User()
        {
            user_count++;
        }
        User::User(std::string first_name, std::string last_name, std::string status)
        {
            this->first_name = first_name;
            this->last_name = last_name;
            this->status = status;
            user_count++;
        }
        User::~User()
        {
            user_count--;
        }
        std::ostream& operator << (std::ostream& output, const User user);
        std::istream& operator >> (std::istream &input, User &user);

int User::user_count = 0;

std::ostream& operator << (std::ostream& output, const User user)
{
    output << "First name: " << user.first_name << "\nLast name: " << user.last_name << "\nStatus: " << user.status;
    return output;
}

std::istream& operator >> (std::istream &input, User &user)
{
    input >> user.first_name >> user.last_name;
    return input;
}

And lastly, Here's the code for my user.h file

#ifndef USER_H
#define USER_H

class User
{
        static int user_count;
        std::string status = "Gold";

        public:
        static int get_user_count();
        std::string first_name;
        std::string last_name;
        std::string get_status();
        void set_status(std::string status);
        User();
        User(std::string first_name, std::string last_name, std::string status);
        ~User();
        friend std::ostream& operator << (std::ostream& output, const User user);
        friend std::istream& operator >> (std::istream &input, User &user);
};
#endif

The output is coming "Gold" when I'm telling the program that it is "Silver". The first and the last name are coming correctly in the output of the program so please help me with my question.

Aucun commentaire:

Enregistrer un commentaire