My program crashes after a few moments, it has no compiling errors

Hi,
I am totally new to C++ (2 weeks) and I've been doing this program (its my homework). The program is suppossed to build a database, so I'm in my first stages (this is the 5th program writen)

Right now this is what it does:
Asks for data to build the database

I want to continue the program because it's still unfinished but now i don't know what it's wrong

Here is the header
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef CRIMINAL_H
#define CRIMINAL_H
#include <string>
#include <iostream>

using namespace std;
using std::string;

class Criminal{
public:
    Criminal();
    ~Criminal();
    Criminal(short, string, string, string, string, string, char, short, short, short);
    virtual void Display() const=0;

    short getCriminalSerial() const {return itsCriminalSerial;}
    const string & getCrimeType() const {return itsCrimeType;}
    const string & getFirstName() const {return itsFirstName;}
    const string & getLastName() const {return itsLastName;}
    const string & getAddress() const {return itsAddress;}
    const string & getWorkspace() const {return itsWorkspace;}
    char  getGender() const {return itsGender;}
    short getAge() const {return itsAge;}
    short getWeight() const {return itsWeight;}
    short getHeight() const {return itsHeight;}

    void setCriminalSerial(short cSerial){itsCriminalSerial = cSerial;}
    void setCrimeType(const string & cType){itsCrimeType = cType;}
    void setFirstName(const string & fName){itsFirstName = fName;}
    void setLastName(const string & lName){itsLastName = lName;}
    void setAddress(const string & address){itsAddress = address;}
    void setWorkspace(const string & workspace){itsWorkspace = workspace;}
    void setGender(char gender) {itsGender = gender;}
    void setAge(short age) {itsAge = age;}
    void setWeight(short weight) {itsWeight = weight;}
    void setHeight(short height) {itsHeight = height;}

protected:
    short   itsCriminalSerial;
    string  itsCrimeType;
    string  itsFirstName;
    string  itsLastName;
    string  itsAddress;
    string  itsWorkspace;
    char    itsGender;
    short   itsAge;
    short   itsWeight;
    short   itsHeight;
};

Criminal::Criminal(short cSerial, string cType, string fName, string lName, string address, string workspace, char gender, short age, short weight, short height){}

void Criminal::Display() const
{
    cout << "\nCriminal Serial: " << itsCriminalSerial;
    cout << "\nCrime associated with? " << itsCrimeType;
    cout << "\nName: " << itsFirstName << " " << itsLastName;
    cout << "\nAddress: " << itsAddress;
    cout << "\nWorkplace: " << itsWorkspace;

    if (getGender() == 'M')
        cout << "\nSuspect is male.\n";
    else
        cout << "\nSuspect is female.\n";

    cout << "\nSuspect's Age is: " << itsAge;
    cout << "\nSuspect's Weight is: " << itsWeight;
    cout << "\nSuspect's Height is: " << itsHeight;
}

Criminal::~Criminal(){}

//-----------------------------------------------

class CriminalNode
{
public:
    friend class CriminalList;
    CriminalNode (Criminal*);
    ~CriminalNode();

    void SetNext(CriminalNode * node)
    {itsNext = node;}
    CriminalNode * getNext() const;
    Criminal * getCriminal() const;
private:
    Criminal *itsCriminal;
    CriminalNode * itsNext;
};

CriminalNode::CriminalNode(Criminal* pCriminal):
    itsCriminal(pCriminal),
    itsNext(0)
    {}

CriminalNode::~CriminalNode()
{
    delete itsCriminal;
    itsCriminal = 0;
    delete itsNext;
    itsNext = 0;
}

CriminalNode * CriminalNode::getNext() const
{
    return itsNext;
}

Criminal * CriminalNode::getCriminal() const
{
    if(itsCriminal)
        return itsCriminal;
    else
        return NULL;
}

//-----------------LISTA DE CRIMINALES --------------

class CriminalList
{
    public:
        CriminalList();
        ~CriminalList();
        void        Iterate(void (Criminal::*f)()const) const;
        Criminal*   Find(int & position, int CriminalSerial) const;
        Criminal*   getFirst() const;
        void        Insert(Criminal *);
        Criminal*   operator[] (int) const;
        int         getCount() const {return itsCount;}
        static      CriminalList& getGlobalCriminalList()
        {
            return GlobalCriminalList;
        }
private:
    CriminalNode * pHead;
    int itsCount;
    static  CriminalList GlobalCriminalList;
};

CriminalList CriminalList::GlobalCriminalList;

CriminalList::CriminalList():
    pHead(0),
    itsCount(0)
    {}

CriminalList::~CriminalList()
{
    delete pHead;
}

Criminal*   CriminalList::getFirst() const
{
    if (pHead)
        return pHead->itsCriminal;
    else
        return NULL;
}

Criminal * CriminalList::operator[](int offSet) const
{
    CriminalNode* pNode = pHead;

    if (!pHead)
        return NULL;

    if (offSet > itsCount)
        return NULL;

    for (int i=0; i<offSet; i++)
        pNode = pNode->itsNext;

    return pNode->itsCriminal;
}

Criminal* CriminalList::Find(int & position, int CriminalSerial) const
{
    CriminalNode * pNode = 0;
    for (pNode = pHead, position = 0;
        pNode!=NULL;
    pNode = pNode->itsNext, position++)
    {
        if(pNode->itsCriminal->getCriminalSerial() == CriminalSerial)
            break;
    }
    if (pNode == NULL)
        return NULL;
    else
        return pNode->itsCriminal;
}

void CriminalList::Iterate(void (Criminal::*func)()const) const
{
    if (!pHead)
        return;
    CriminalNode* pNode = pHead;
    do
        (pNode->itsCriminal->*func)();
    while (pNode = pNode->itsNext);
}

void CriminalList::Insert(Criminal* pCriminal)
{
    CriminalNode * pNode = new CriminalNode(pCriminal);
    CriminalNode * pCurrent = pHead;
    CriminalNode * pNext = 0;

    int New = pCriminal->getCriminalSerial();
    int Next = 0;
    itsCount++;

    if(!pHead)
    {
        pHead = pNode;
        return;
    }
    if(pHead->itsCriminal->getCriminalSerial() > New)
    {
        pNode->itsNext = pHead;
        pHead = pNode;
        return;
    }

    for(;;)
    {
        if(!pCurrent->itsNext)
        {
            pCurrent->itsNext = pNode;
            return;
        }

        pNext = pCurrent->itsNext;
        Next = pNext->itsCriminal->getCriminalSerial();
        if(Next > New)
        {
            pCurrent->itsNext = pNode;
            pNode->itsNext = pNext;
            return;
        }
        pCurrent = pNext;
    }
}

class CriminalCatalog : private CriminalList
{
public:
    void Insert(Criminal *);
    int Exists(int CriminalSerial);
    Criminal * Get(int CriminalSerial);
    void operator+(const CriminalCatalog &);
    void ShowAll() {Iterate(&Criminal::Display);}
private:
};

void CriminalCatalog::Insert(Criminal * newCriminal)
{
    int CriminalSerial = newCriminal-> getCriminalSerial();
    int offset;

    if (!Find(offset, CriminalSerial))
        CriminalList::Insert(newCriminal);
    else
    {
        cout << CriminalSerial << " was the ";
        switch (offset)
        {
            case 0:     cout << "first " ; break;
            case 1:     cout << "second " ; break;
            case 2:     cout << "third " ; break;
            default:    cout << offset+1 << "th ";
        }
        cout << "entry. Rejected!\n";
    }
}

int CriminalCatalog::Exists(int CriminalSerial)
{
    int offset;
    Find(offset, CriminalSerial);
    return offset;
}

Criminal * CriminalCatalog::Get(int CriminalSerial)
{
    int offset;
    return (Find(offset, CriminalSerial));
}

#endif 

Here is the cpp
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
#include <iostream>
#include <fstream>
#include "Criminal.h"

using namespace std;

int main()
{
    CriminalCatalog pc;
    Criminal * pCriminal = 0;
    short  cSerial;
    string cType;
    string fName;
    string lName;
    string address;
    string workspace;
    bool gender;
    short age;
    short weight;
    short height;

    cout << "New Criminal Serial?: ";
    cin >> cSerial;
    cout << "Suspect's crime type? \nMurder     Rape              Aggravate Assault\nBurglary   Larceny Theft     Motor Vehicle Theft\nArson      Guns and Crimes   Workplace Violence\n";
    cin >> cType;
    cout << "\nSuspect's First Name?: ";
    cin >> fName;
    cout << "\nSuspect's Last Name?: ";
    cin >> lName;
    cout << "\nSuspect's Address?: ";
    cin >> address;
    cout << "\nSuspect's Workspace Address?: ";
    cin >> workspace;
    cout << "\nSuspect's Gender?: ";
    cin >> gender;
    cout << "\nSuspect's Age?: ";
    cin >> age;
    cout << "\nSuspect's Weight?: ";
    cin >> weight;
    cout << "\nSuspect's Height?: ";
    cin >> height;

    pc.Insert(pCriminal);
    pc.ShowAll();
    return 0;
}


Any help is appreciated
Are you doing anything when it crashes? Does it actually crash or simply close?
Hi LB,
Yes, sorry about that:
I input the next: cSerial, cType, fName, lName, address.
After the address before asking me the workspace address, that's when it crashes
Maybe i should change the cins used to getstring... what do you think?
I don't know if this'll work, but get rid of the '\n' on your cout's and, instead, add a << endl to the end of each one.

Make it cout << "Suspect's Address?: " << endl; for all the questions (from the serial to the height). I say to get rid of the '\n' simply because endl also starts a new line. However, if you're just testing to see if this solves the problem, forget about the formatting and just go adding the << endl;s.

endl flushes the stream, which might be the source of your problem, though I've never seen this problem before.
Hi wasabi,
Thanks a lot, your comment really got me going to try new stuff. I removed every "\n" from my program, it didn't help but it feels more clean, i'm kind of obssesive.

I found the issue (haven't fixed it though). The issue is that I'm using a lot of types of short, bool, and string combined with cin.

But seriously, thanks man, I was totally burned out, if it wasn't for you I wouldn't even started looking for the answer.

The problem is that cin >> doesn't get along with spaces and different types, and since I have ints and strings in the same input stream my program crashes

so I'm going to change every cin to getline() then I'm going to convert the string to use getline for ints


Topic archived. No new replies allowed.