Console applications keep crashing.

Hello,
Any time I run my program after I make a selection from the menu the output is printed on the console screen then immediately a windows screen comes up saying project.exe has stopped working. Any and all thoughts/suggestions are welcomed. Below is my code, and I suspect the error has something to either A) due with how I'm calling the method or B) how I have the method coded.

Main
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
#include <iostream>
#include <fstream>
#include <string>
#include "telephone.h"
#include "tbook.h"

using namespace std;
int main()
{

    int menuSelection; // variable for user input for the rolodex program

    Tbook tbookObject;
    Telephone telebookObject;


    // below is listing the menu options for rolodex program
    cout << "Welcome to the Rolodex-9000, please select from the following choices" << endl;
    cout << "\t To Lookup a name press 1 " << endl;
    cout << "\t To add a new entry press 2" << endl;
    cout << "\t To print the Rolodex-900 press 3" << endl;
    cout << "\t To exit, press 4 " << endl;

    cin >> menuSelection;

    // prompting user with menu options
    switch(menuSelection)
    {

    case 1:

    case 2:
    tbookObject.isFull();

    case 3:
    tbookObject.print();

    case 4:
    tbookObject.write();

    default:
        cout << "Error, invalid selection. Please try again";
    }

}




Tbook.h

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
#ifndef TBOOK_H_INCLUDED
#define TBOOK_H_INCLUDED

#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
[code]
class Tbook
{

public:
    //constructors
    Tbook();
    Tbook(int rolodex[],int maxNumberEntries,int numberEntries);

    //deconstructor
    ~Tbook();

    //Acessors
    string write();
    string print();
    bool isFull();
    string lookup();
    string add();

private:
    int rolodex [];
    int maxNumberEntries;
    int numberEntries;

};

#endif // TBOOK_H_INCLUDED

Tbook
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
#include <iostream>
#include <iomanip>
#include <string>
#include <exception>
#include <fstream>
#include "tbook.h"
#include "telephone.h"
using namespace std;

//----------------------------------
//Name: Tbook
//Purpose: default constructor
//Parameters: none
//Return: nothing
//----------------------------------
Tbook :: Tbook()
{

int rolodex [9];
int maxNumberEntries = 9;
int numberEntries = 0;
};

//----------------------------------
//Name: Tbook
//Purpose: default constructor
//Parameters: rolodex, maxNumberEntries, numberEntries
//Return: nothing
//----------------------------------
Tbook :: Tbook(int rolodex[],int maxNumberEntries,int numberEntries)
{
    this -> rolodex[9] = rolodex[9];
    this -> maxNumberEntries = maxNumberEntries;
    this -> numberEntries = numberEntries;
};

//----------------------------------
//Name: ~Tbook
//Purpose: default deconstructor
//Parameters: none
//Return: nothing
//----------------------------------
Tbook :: ~Tbook()
{
};

//----------------------------------
//Name: write
//Purpose: write the contents to the rolodex
//Parameters: none
//Return: nothing
//----------------------------------
string Tbook :: write()
{
    fstream fin;

    //opening rolodex.txt
    fin.open("rolodex.txt");

    // error checking if rolodex.txt exists
    if (fin.is_open())
    {
        cout << "File opened successfully" << endl;
    }
    else
    {
        cout << "Error, file does not exist" << endl;
    }




  fin.close(); // closing the file
};


//----------------------------------
//Name: print
//Purpose: print contents from rolodex file
//Parameters: none
//Return: nothing
//----------------------------------
string Tbook :: print()
{
    fstream fin;    
    string contents;   rolodex.txt

    fin.open("rolodex.txt");

    while (fin.good())
    {
        getline(fin,contents);
        cout << contents << endl;
    }



  fin.close(); // closing the file
}
//----------------------------------
//Name: isFull
//Purpose:  checking if rolodex is full
//Parameters: none
//Return: nothing
//----------------------------------
bool Tbook :: isFull()
{
   fstream fin;
   string contents;

   fin.open("rolodex.cpp");

       while (fin.good())
    {
        getline(fin,contents);
        cout << contents << endl;
    }

    if (rolodex[9] == rolodex[maxNumberEntries])
    {
        cout << "\tThe rolodex is full" << endl;
        return false;
    }
    else
    {
        cout << "The rolodex is not full, add more contacts" << endl;
        return true;
    }
}

Last edited on
The first thing that I noticed (and I don't have time rightnow to look deeper) is that you are missing break statements in your switch control.
Inside the Tbook constructor you create three local variables, leaving the data members of Tbook uninitialized.

Line 28 in Tbook.h is an error. You must specify the array size.

this -> rolodex[9] = rolodex[9];
This will set the ninth element in the array, and not the whole array. Not sure if that is what you want.
Thank you Peter87. I have changed the initial declaration in Tbook.h to the following:

 
int rolodex [9];


However the application is still crashing.
Last edited on
@johnnydiamond08

Remember, an array, such as int rolodex[9];, accepts numbers into rolodex[0] thru rolodex[8], only. So trying to access rolodex[9], is accessing an out-of-bounds value, and you never know what that may be. It could really screw up your program.
Topic archived. No new replies allowed.