My program crashes!

So this is my program, what I want to do is create a character from a game with certain stats, strength, armor, critical chance, etc.

When I executed the program instantly crashes and says:

terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Process returned 3 (0x3)



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 #include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <map>
#include <stdlib.h>

using namespace std;

bool satisfied = false;

class new_player
{
public:
    void set_name();
    void create_character();
    void set_random_stats();
    void reset_values();

private:
    std::map <string, unsigned int> stats;
    std::string name_player = NULL;
};

void new_player::set_name()
{
    system("CLS");
    cout<< "Please set the character's name: ";
    cin>> name_player;
}

void new_player::reset_values()
{
    stats ["Strength"] = 8;
    stats ["Armor"] = 8;
    stats ["Health"] = 50;
    stats ["Crit Chance"] = 1;
    stats ["Dodge"] = 5;
    stats ["Level"] = 1;
    stats ["Experience"] = 0;
    stats ["Gold"] = 6;
}

void new_player::set_random_stats()
{
    system("CLS");
    srand(time(NULL));
    stats ["Strength"] += rand() % 13;
    stats ["Armor"] += rand() % (13-(stats ["Strength"] - 8));
    stats ["Crit Chance"] += rand() % (13-((stats ["Strength"] - 8) + stats ["Armor"] - 8));
    stats ["Dodge"] += 29 - (stats ["Strength"]+stats ["Armor"]+stats ["Crit Chance"]);

    //Show Stats function
    cout<<"Strength: "<< stats ["Strength"]
    <<"\nArmor: "<<stats ["Armor"]<<"\nHealth: "
    <<stats ["Health"]<<"\nCritical Chance: "<<stats ["Crit Chance"]
    <<"\nDodge: "<<stats ["Dodge"]<<"\nLevel: "<<stats ["Level"]
    <<"\nExperience: "
    <<stats ["Experience"]<<"\nGold: "
    <<stats ["Gold"];
}

void new_player::create_character()
{
    new_player::reset_values();
    new_player::set_random_stats();
}

int main()
{
    new_player Villaop;
    Villaop.reset_values();
    int decision = 0;
    cout<< "1. Set Name\n2.Roll for Stats\n\n";
    cin>> decision;
    if (decision == 1)
    {
        Villaop.set_name();
    }
    else
    {
        while (satisfied == false)
        {
            int satisfaction;
            Villaop.create_character();
            cout<< "\n\nAre you satisfied with your stats?";
            cin>> satisfaction;
            if (satisfaction == 1)
            {
                satisfied = true;
            }
        }
    }
}
Last edited on
Line 22: C++ is not Java. Strings cannot be null.
Damm it... k thanks! I'll just put a default name
One more question, how can I separate this program now into different .h and .cpp files? I've never done it :/
I've read it already, but it didn't work, could you separate this program in different files to see how to do it? If you don't have time tell me and I'll create a new post.

Thanks for your help anyway :)
closed account (SECMoG1T)
Line 65 66
1
2
3
4
5
new_player::set_random_stats();/// well this is not the right way new_player is not known
You can use this instead

  this-> set_random_stats ();/// such
I have NEVER understood how this pointer works... I mean when do I use it? what does it do exactly?
Maybe now that you have brought this pointer out you could help me :D
Lines 12-23 go in header file (player.h).
Lines 25-67 go in a .cpp file (player.cpp)
Lines 69-94 go in main.cpp (or whatever you want to call your program)

You'll need to repeat lines 1-8 in both your .cpp files and add an #include of your header file.

You also need to get rid of the global at line 10. It belongs as a local inside main().

Line 47: You don't want your call to srand() inside set_random_stats(). That will reinitialize the RNG each time you call set_random_stats(). Move it to line 70.

Line 65-66: You don't need the new_player:: scope qualifiers here, although they're harmless.


THANKS!!!! How did you guys learn so much of coding? I have read jumping into c++ and still struggle quite a lot :/

Thanks for all!!
@andy1192 - Yes new_player is indeed known. There's just no need for it here. Using the this-> pointer is not needed either since create_character() is a member of the new_player class.

@erik341 - The this pointer simply points to the current instance of the object. Not usually needed, but can be useful if you can be helpful if you have naming conflicts between arguments and members.

Consider if set_name() took an argument called name_player:
1
2
3
4
5
6
 
void new_player::set_name (string name_player) 
{  name_player = name_player;  //  The compiler is going to be confused here because it does know if you're referring to the argument or the member variable.

//  Instead: 
  this->name_player = name_player;  // ambiguity resolved 


this pointer can also be useful if you want to pass a pointer to the current object to a function that's not a member of the class.
Last edited on
My compiler (VS Express 2012) says for line 22:
Error C2864: 'new_player::name_player': only static const integral data members can be initiated within a class.


and IntelliSence reports:

data member initialization is not allowed


This seems to agree with your error message:
basic_string::_S_construct null not valid


Style issue:

If you have using namespace std;

in line 8, you don' t need the std:: prefix in lines 21 and 22. However, if you leave the namespace out of line 8, you'll need to add std:: before string, because string is in the std namespace. In other words, this:

1
2
3
4
using namespace std;

    map <string, unsigned int> stats;
    string name_player// = NULL; 

will work

Leave out the using statement:

1
2
    std::map <string, unsigned int> stats;
    std::string name_player// = NULL; 


is an error because string is in namespace std too.

1
2
    std::map <std::string, unsigned int> stats;
    std::string name_player// = NULL; 

Last edited on
Wow! You explained it better than any webpage I've read (AbstractionAnon)! Thank you very much! Can I private message you if I need help further on?
Last edited on
Topic archived. No new replies allowed.