Why does this keep crashing?

This is my phonebook program, whenever the user goes to the main listing and the program prints out all the people and their info the program crashes. I suspected it was because I was printing too much at one time, so I bumped the number of listings down from 100 to 50 and its STILL crashing.

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

using namespace std;
//-------------------------------------------------------------------------
void mainListing();
void listingSearch();
void enterListings();
//-------------------------------------------------------------------------
struct phoneBook
{
    string name;
    string address;
    int phoneNumber;
};

phoneBook listing[50];
//-------------------------------------------------------------------------

int main()
{
    int option;

    system("TITLE, Phonebook");
    system("CLS");
    cout << "[1] Main phonebook listing" << endl;
    cout << "[2] Look up individual listings" << endl;
    cout << "[3] Enter new listings" << endl;

    cin >> option;

    switch(option)
    {
        case 1:
        mainListing();
        break;
        case 2:
        listingSearch();
        break;
        case 3:
        enterListings();
        break;
        default:
        main();
        break;
    }


}
//-----------------------------------------------------------------------------------
void mainListing()
{
    string option;
    system("CLS");
    for(int i = 1; i <= 50; i++)
    {
        cout << i << "." << " Name = " << listing[i].name << " Address = " << listing[i].address << " Phone number = " << listing[i].phoneNumber << endl;
    }

    cout << endl;
    cout << "Return to main menu? (Yes or No)" << endl;
    cin >> option;

    if(option=="Yes")
    main();
    else
    cout << "Goodbye";
}
//-----------------------------------------------------------------------------------
void listingSearch()
{
    string option;
    int listNumber;
    system("CLS");

    cout << "Listing number = ";
    cin >> listNumber;
    cout << endl;
    cout << "Name = " << listing[listNumber].name << " Address = " << listing[listNumber].address << " Phone number = " << listing[listNumber].phoneNumber << endl;
    cout << endl;
    cout << "Enter a new listing? (Yes or No)" << endl;
    cin >> option;

    if(option=="Yes")
    listingSearch();
    else
    main();

}
//-----------------------------------------------------------------------------------
void enterListings()
{
    string option;
    int i;

    system("CLS");
        cout << "Enter number of listing = "; 
        cin >> i;
        cout << "Name = ";
        cin.ignore();
        getline(cin, listing[i].name);
        cout << "Address = ";
        cin.ignore();
        getline(cin, listing[i].address);
        cout << "Phone number = ";
        cin >> listing[i].phoneNumber;
        cout << endl;
        cout << "Enter another listing? (Yes or No)" << endl;
        cin >> option;

        if(option=="Yes")
        enterListings();
        else
        main();

}
//-----------------------------------------------------------------------------------






You cannot call main - it is forbidden by the standard. You do so on lines 115, 88, 66 and 45.

For an array with number of elements n, valid indices are 0 to n-1. For the array listing which has 50 elements then, the valid indices are 0 to 49. However, on line 56 you use indices 1 to 50. Obviously that last is out of range.



Topic archived. No new replies allowed.