(C++) Not printing to output correctly?

I set my values I want to print out on line 54 but it doesn't print out my values and also never asks me to enter my name?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream> 
#include<string.h>
#include<new>
using namespace std; 

class Player {
	int health;
	int strength;
	int level;
	int exp;
	string name;
    public:
	void displayStats();
	void create(int,int,int,int,string,int*);
};

void Player::create(int h, int s, int l, int e,string n, int *playercount) {
	health = h;
	strength = s;
	level = l;
	exp = e;
	name = n;
	*playercount++;
}

void Player::displayStats() {
	cout << "Name: " << name << endl;
	cout << "HP: " << health << endl;
	cout << "Str: " << strength << endl;
	cout << "LVL: " << level << endl;
	cout << "EXP: " << exp << "/" << level*5 << endl;
}

int main(){
	Player *players;
 	try { 
    		players = new Player [10]; 
  	} catch (bad_alloc xa) { 
    		cout << "Allocation Failure\n"; 
    		return 1; 
  	} 
	char answer;
	int playercount = 0;
	Player Temp;
	cout << "Create new player? y/n" << endl;
	do {
	cin >> answer;
	} while(answer=='y' || answer=='n');
	if (answer == 'y') {
		if (playercount <= 9) {
			string n;
			cout << "Enter name: " << endl;
			cin >> n;
			players[playercount].create(100,1,1,0,n,&playercount);
		} else {cout<<"Reached max players" << endl;}
		
	} else if (answer=='n'){
	
	}

	players[0].displayStats();
	return 0;

}
Lotta stuff to unpack here.

#1: Your Player::create method is pretty much a constructor. Maybe make it into one?

#2: You leak an array of 10 players by never doing delete [] players; anywhere in your program. While not an issue for a small program like this, it should still be fixed. Please consider using smart pointers (or std::vector, especially here) in the future.

#3: answer=='y' || answer=='n' will be true when answer is either y or n, meaning you have this behavior exactly the opposite of what you want. When your loop exits, neither of the following if statements will run.

#4: So, I assume you want your program to be able to store up to 10 players. As it is now, though, it will only ever read 1 because most of it isn't inside a loop. Also, you only ever print out the stats of the first player.

#5: That said, it should still print out your 0th player's stats if you enter something that isn't y or n. They'll just look something like this:
Name: 
HP: 0
Str: 0
LVL: 0
EXP: 0/0

Do you not see that?

-Albatross
I appreciate your response, because some things as simple as they where I did not notice. I'm still new to programming with C++ and I appreciate your patience.

#1: I see that now, and I will make it into a constructor.

#2: I don't have much of an understanding on what you mean by delete [] players;, smart pointers, or vector. I will do some studying in these areas and correct my work.

#3: If they enter 'y' can I place a break; in the code to exit the while loop or is there another way I should go about this.

#4: Yes, just 10 players for now. I'm also aware it needs to be in a loop and that it only prints out the first player. I just have it that way to test what I had so far.

#5: I see that now.

Thanks for your time!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string> // *** <string>

// #include<new> // not required

class Player {

    // note: default member initialisers
    // http://www.stroustrup.com/C++11FAQ.html#member-init
    int health = 100 ;
    int strength = 1 ;
    int level = 1 ;
    int exp = 0 ;
    std::string name = "anonymous" ;

    public:

        // implicit default constructor (uses default member initialisers)

        void displayStats() const ; // *** const
        void rename( const std::string& player_name ) { name = player_name ; }
};

void Player::displayStats() const {

	std::cout << "    Name: " << name << '\n'
	          << "      HP: " << health << '\n'
	          << "Strength: " << strength << '\n'
	          << "   Level: " << level << '\n'
	          << "     Exp: " << exp << "/" << level*5 << '\n' ;
}

bool yes_to_create_player() { // return true if user answered yes

    std::cout << "create a player? y/n: " ;
    char answer ;
    std::cin >> answer ;

    if( answer == 'y' || answer == 'Y' ) return true ;
    else if( answer == 'n' || answer == 'N' ) return false ;

    // user did not enter either y or n
    std::cout << "invalid input '" << answer << "'. try again\n" ;
    return yes_to_create_player() ; // try again
}

int main() {

    const int MAX_PLAYERS = 10 ; // maximum possible number of players

    // simple array of players
    // (dynamic allocation is not required, should not be used)
    Player players[MAX_PLAYERS] ; // initially, all are default constructed Players

    int num_players = 0 ; // actual number of players (#items used in the array)

    while( num_players < MAX_PLAYERS && yes_to_create_player() ) {

        std::string name ;
        std::cout << "enter name (no spaces): " ;
        std::cin >> name ;
        players[num_players].rename(name) ; // reset the player's name

        ++num_players ; // increment number of players
    }

    std::cout << "\nlist of players\n--------\n\n" ;
    for( int i = 0 ; i < num_players ; ++i ) {

        std::cout << "player #" << i+1 << '\n' ;
        players[i].displayStats() ;
        std::cout << "\n-----------\n\n" ;
    }
}
Thanks @JLBorges
I didn't expect anyone to just fix all of it for me but thank you. I'll go over the code and see what you did differently.
Also, is it okay if I decide to use using namespace std;?
using namespace std; is not the worst sin. That is, until you put it in a header and have it affect every file that includes that header. I'd avoid using it outside of the bodies of functions, personally.

-Albatross
I can see that and I appreciate your help mate.
Topic archived. No new replies allowed.