Programme problem. Clases, gets and sets.

Hey guys, i have this code but i can't make it work as i want. I can't get past "Enter name: " . I need some kind of solution, can anyone help me?

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
#include <iostream>
#include <string>

using namespace std;

class Piece //base
{
public:
    Piece() : N (0), colorP (true) {}
    ~Piece(){}
    void setN(int _N) {N=_N;}
    void setcolorP (bool _colorP) {colorP= _colorP;}
    
    int getN() {return N;}
    int getcolorP () {return colorP;}
    
private:
    int N;
    bool colorP;
};

class Player
{
public:
    //constructor
    Player (string _name, bool _turn, bool _colorJ) : name (_name), turn (_turn), colorJ (_colorJ) {}
    
    //destructor
    ~Player(){}
    //set y get
    string getname() const {return name;}
    bool getturn () {return turn;}
    bool getcolorJ () const {return colorJ;}
    
    void setname (string _name) {name = _name;}
    void setturn (bool _turn) {turn=_turn;}
    void setcolorJ (bool _colorJ) {colorJ=_colorJ;}
    
private:
    //static int njugadores;
    string name;
    bool turn;
    bool colorJ;
};


int main()
{
    Player P1 ("Player 1", true, true), *pP1 (NULL);
    pP1=&P1;
    Piece *pE1;
    //name
    cout << endl << endl <<"Current name: " << pP1->getname() <<endl;
    string _name;
    cout << "Enter name: ";
    getline (cin, _name);
    pP1->setname(_name);
    cout <<endl<<"Your name is: " << pP1->getname() << endl;
    
    //color
    cout << endl << "¿Black or white?" <<endl <<"0/1" << endl;
    bool color;
    cin >> color;
    do{
        cout << endl << "Wrong number"<< endl<<endl<<"¿Black or white?" <<endl <<" true/false" << endl;
        cin >> color;
    } while (color != 0 && color != 1);
    pP1->setcolorJ (color);
    if (pP1->getcolorJ() == 0)
    cout <<endl<< "Current color: White "  <<endl;
    else cout <<endl<< "Current color: Black "  <<endl;
    
    //number N
    cout << endl << "Number of pieces: " ;
    int N;
    do {
    cin >> N;
    }while (N <= 0 || N > 16);
    pE1->setN (N);
}


edit: i dont know how to make booleans work, but i'll figure it out. Last part is not totally implemented. ty.
Last edited on
There are a few problems in your code.

First of all, the do-while loop always executes at least once, which is not what you seem to intend in this case. No matter what the user enters, you always print out the Wrong number message at least once.

Second of all, the condition you use in your loop will always evaluate to true. You compare color, a boolean value to 0 and to 1. You compare a bool to an int value, meaning the bool will be converted to an int. When converting a bool to an int, it will always be converted to either 0 (false) or 1 (true), meaning the expression will always compare to true. You can solve this by checking if cin failed reading a bool.

To solve the first two problems, you could change your loop to the following:

1
2
3
4
5
6
7
8
9
bool color;
cin >> color;
while(cin.failed())
{
    cout << "Wrong number" << endl;
    cin.clear(); //Clear the failed status
    cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); //Clear the buffer
    cin >> color; //Try again
}


Third of all, you declare a pointer to Piece: Piece* pE1, but never set the pointer. Later on you use the pointer: pE1->setN(N);. This causes undefined behavior. You could solve this by declaring your Piece as a normal variable instead of a pointer. Try using Piece E1 and using it as E1.setN(N);.
Thank you for everything, it has been very useful.

I have to say that for the last problem, my class Piece is the base of other classes which derivates from it (that is not in the code), so its an abstract class and i can't declare objects of it, but i'll set the pointer.
Topic archived. No new replies allowed.