Click here to Skip to main content
15,400,390 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote my code perfectly but the output should be like this:


new player has been created
new player has been created
new player has been created
new player has been created
new player has been created
kill him boy!FINISH HIM!!
4
1
this player is died and removed from the server
this player is died and removed from the server
this player is died and removed from the server
this player is died and removed from the server
this player is died and removed from the server

instead it gives me this:

new player has been created
new player has been created
new player has been created
this player is died and removed from the server
new player has been created
this player is died and removed from the server
new player has been created
this player is died and removed from the server
kill him boy!FINISH HIM!!
1
4
this player is died and removed from the server
this player is died and removed from the server
this player is died and removed from the server
this player is died and removed from the server
this player is died and removed from the server

what's wrong?
( I need to use the COD_player(string Username,string Password) constructor )

What I have tried:

#include<iostream>
#include<string>
using namespace std;



class COD_player{
	
	string name,id,username,password;
	int level;
	bool gender;
	static int shots,online_players,died_players;
	public:
		void set_counters1(){
	int o;
	this->online_players=o;
		}
		void set_counters2(){
			int d;
			this->died_players=d;
		}
static int get_counters1(){
return online_players;

}
static int get_counters2(){
	return died_players;
}
COD_player()
{
	cout<<"new player has been created"<<endl;
	name="";
	id="";
    username="";
	password="";
	level=0;
	shots=0;
	gender=false;
	online_players++;

}

COD_player(string Username,string Password)
{
	cout<<"new player has been created"<<endl;
	this->username=Username;
	this->password=Password;
	online_players++;
}

 void fire()
{
	shots++;
	cout<<"kill him boy!FINISH HIM!!"<<endl;
	online_players--;
	died_players++;
}
 
~COD_player()
{
	cout<<"this player is died and removed from the server"<<endl;
	online_players--;
	died_players++;

}
};
int COD_player::online_players=0;
int COD_player::died_players=0;
int COD_player::shots=0;
int main()
{
	string x,y;
	COD_player p1=COD_player();
	COD_player p2=COD_player();
	COD_player p3=COD_player(x,y);
	COD_player p4=COD_player(x,y);
	COD_player p5=COD_player(x,y);
	p1.fire();
	cout<<COD_player::get_counters1();
	cout<<endl;
	cout<<p1.get_counters2();
	cout<<endl;
	return 0;
}
Posted
Updated 18-Nov-21 17:15pm

1 solution

I am really not sure why you see that output. I added a player number value to help see who is doing what. This is the code that resulted :
C++
class COD_player
{
    string name, id, username, password;
    int level       { 0 };
    int number      { -1 };
    bool gender     { false };

    inline static int shots             = 0;
    inline static int created           = 0;
    inline static int online_players    = 0;
    inline static int died_players      = 0;

public:
    COD_player()
    {
        ++created;
        ++online_players;
        number = created;
        cout << "player " << number << " was created" << endl;
}

    COD_player( string Username, string Password )
    {
        ++created;
        ++online_players;
        number = created;
        cout << "player " << number << " was created " << Username << endl;
        this->username = Username;
        this->password = Password;
    }

    ~COD_player()
    {
        cout << "player " << number << " has died" << endl;

        --online_players;
        ++died_players;
    }

    static int get_created()
    {
        return created;
    }

    static int get_counters1()
    {
        return online_players;
    }

    static int get_counters2()
    {
        return died_players;
    }

    void fire()
    {
        ++shots;
        cout << "kill him "  << number << " ! FINISH HIM!! : " << endl;
        --online_players;
        ++died_players;
    }
};

int main()
{
    const char * password = "password";
    COD_player p1=COD_player();
    COD_player p2=COD_player();
    COD_player p3=COD_player( "P3", password );
    COD_player p4=COD_player( "P4", password );
    COD_player p5=COD_player( "P5", password );

    COD_player p6( "P6", password );
    COD_player p7( "P7", password );
    {
		COD_player p8( "P8", password );
	}
	COD_player p9( "P9", password );

    cout << COD_player::get_created() << " players were created" << endl;
    p1.fire();
    cout << COD_player::get_counters1() << " players remain" << endl;
    cout << COD_player::get_counters2() << " players have died" << endl;
    return 0;
}
and this was the output I received :
player 1 was created
player 2 was created
player 3 was created P3
player 4 was created P4
player 5 was created P5
player 6 was created P6
player 7 was created P7
player 8 was created P8
player 8 has died
player 9 was created P9
9 players were created
kill him 1 ! FINISH HIM!!
7 players remain
2 players have died
player 9 has died
player 7 has died
player 6 has died
player 5 has died
player 4 has died
player 3 has died
player 2 has died
player 1 has died
Note that static member initialization requires use of the C++ 2017 standard. Also be aware that this code is not thread safe because the class has static variables with no access control. Since you are running everything in a single thread it is OK but if you were to use multiple threads it would be unsafe.

ETA : I added three more players constructed in a more traditional way and the output is as expected. The scoping operator was used as a lifetime check.
   
v6
Comments
Member 15420232 18-Nov-21 23:46pm
   
actually I use C++ 2010 so this code won't run
is there any other way I can organize the output
I think the problem is in
COD_player(string Username,string Password)
because it calls the destructor in the middle of the output
but I don't know why
Rick York 19-Nov-21 0:21am
   
If you change the static initializers it will run. I think it is very import to add the player number and the created counter so you can see who is doing what. Without them you can only guess. The created counter gives every player a unique number regardless of how many die. I recommend you adjust your code to be closer to what I did so you can see what's going on.

BTW - I adjusted the code to use an actual string and it gives the output I would expect.
Member 15420232 19-Nov-21 1:30am
   
actually I tried your code on my compiler but it gives error
" inline specifier allowed in function declaration only"
however, I tried my original code on an online compiler and it worked!
note: when I run my code on my compiler it says " your project is out of date"
I think that is the problem
thank you for your help (:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900