Question about Program Not Properly Executing

So over the past week, I've been fine tuning this program that seems to properly compile/execute everywhere except my computer. I am using Visual Studio Community 2017, in an empty project. There are three sections, and I'm simply wondering if there's anything I can do to this to make it actually compile/execute properly? As when it runs, every program seems to work except the first, which is where you input a first/last name, and an address. Upon entering it, and hitting enter, the menu comes up to select another option, and then says "press any key to continue...", which obviously exits the program. Why is this? It works on other's computers, so why does mine not seem to work properly?

addressbook.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
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
  #include "addressBook.h"

#include <iostream>

using namespace std;

int counter = 0;

int getcount = 0;

using namespace std;

void printStruct(struct Person &print);

int main()

{

	char last[20];

	char first[20];

	char choice;

	AddressBook aBook;

	Person add;

	Person getStruct;

	cout << "Address Book" << endl << endl;

	while (true)

	{

		cout << endl << " Enter 1 to add to the address book. " << endl;

		cout << " Enter 2 to get a person. " << endl;

		cout << " Enter 3 to find a person by last name only." << endl;

		cout << " Enter 4 to find a person by last name and first. " << endl;

		cout << " Enter 5 to print Book." << endl;

		cout << " Enter any other key to quit. " << endl;

		cout << endl << "Please make a selection: " << endl;

		cin >> choice;

		switch (choice)

		{

		case '1':

			cout << "Enter First Name" << endl;

			cin.clear();

			cin.ignore(255, '\n');

			cin >> add.firstname;

			cout << "Enter Last Name" << endl;

			cin.clear();

			cin.ignore(255, '\n');

			cin >> add.lastname;

			cout << "Enter Address" << endl;

			// cin.clear();

			// cin.ignore(255,'\n');

			cin.getline(add.address, 40);

			// cin.clear();

			// cin.ignore(255,'\n');

			cin >> add.address;

			aBook.addPerson(add);

			continue;

		case '2':

			cout << endl << "Getting the next person in the address book: " << endl << endl;

			aBook.getPerson(getStruct);

			printStruct(getStruct);

			continue;

		case '3':

			cout << "Please enter the last name to search for: " << endl;

			cin.clear();

			cin.ignore(255, '\n');

			cin >> last;

			if (aBook.findPerson(last, add))

				printStruct(add);

			else

				cout << "Sorry could not find the name." << endl;

			continue;

		case '4':

			cout << "Please enter the last name and then the first name to search for: " << endl;

			cout << "Last name: ";

			cin.clear();

			cin.ignore(255, '\n');

			cin >> last;

			cout << "First name: ";

			cin.clear();

			cin.ignore(255, '\n');

			cin >> first;

			if (aBook.findPerson(last, first, add))

			{

				printStruct(add);

			}

			else

			{

				cout << "Sorry could not find the name." << endl;

			}

			continue;

		case '5':

		{

			aBook.printBook();

			continue;

		}

		default:

		{

			return 0;

			break;

		}

		}

		cin.clear();

		cin.ignore(255, '\n');

	}

	return 0;

}

void printStruct(struct Person &print)

{

	cout << print.firstname << " " << print.lastname << endl;

	cout << print.address << endl;

}

main.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
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
#include <iostream>

#include "addressBook.h"

#include <string.h>

using namespace std;

static int head = 0;

static int tail = -1;

AddressBook::AddressBook()

{

}

AddressBook::AddressBook(const Person &p)

{

	addPerson(p);

}

AddressBook::AddressBook(const Person p[], int size)

{

	for (int i = 0; i < size; i++)

		addPerson(p[i]);

}

AddressBook::AddressBook(char *fName, char *lName, char *address)

{

	Person tmp;

	strcpy_s(tmp.firstname, fName);

	strcpy_s(tmp.lastname, lName);

	strcpy_s(tmp.address, address);

	addPerson(tmp);

}

bool AddressBook::addPerson(const Person p)

{

	if (head < MAXIMUM)

	{

		person[head] = p;

		head++;

		if (tail == -1)

			tail++;

		return true;

	}

	return false;

}

bool AddressBook::getPerson(Person &p)

{

	if (tail >= 0)

	{

		if (tail >= MAXIMUM)

			tail = 0;

		p = person[tail];

		tail++;

		return true;

	}

	return false;

}

bool AddressBook::findPerson(char *lName, Person &p)

{

	for (int i = 0; i < head; i++)

	{

		if (!strcmp(person[i].lastname, lName))

		{

			p = person[i];

			return true;

		}

	}

	return false;

}

bool AddressBook::findPerson(char *lastName, char *firstName, Person &p)

{

	for (int i = 0; i < head; i++)

	{

		if (!strcmp(person[i].lastname, lastName) && !strcmp(person[i].firstname, firstName))

		{

			p = person[i];

			return true;

		}

	}

	return false;

}

void AddressBook::printBook()

{

	for (int i = 0; i < head; i++)

	{

		cout << person[i].firstname << "\t" << person[i].lastname << "\t" << person[i].address << endl;

	}

}

void AddressBook::sort()

{

	Person temp;

	for (int i = 0; i < head; i++)

	{

		for (int j = 0; j < head; j++)

		{

			if ((person[j + 1].lastname, person[j].lastname) < 0)

			{

				temp = person[j];

				person[j] = person[j + 1];

				person[j + 1] = temp;

			}

		}

	}

}

ostream& operator<<(ostream& output, const Person& p) {

	output << "(" << p.firstname << ", " << p.lastname << ")";

	return output;

}

Person& AddressBook::operator() (int i)

{

	return person[i];

}

addressBook.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
#pragma once
#ifndef addressBook
#define addressBook
#include <string>
const int MAXIMUM = 10;
struct Person
{
	char firstname[20];
	char lastname[20];
	char address[50];
};
class AddressBook
{
private:
	Person person[MAXIMUM];
	int count;
public:
	AddressBook();
	AddressBook(char* fName, char *lName, char *address);
	AddressBook(const Person &p);
	AddressBook(const Person p[], int size);
	bool addPerson(const Person p);
	bool addPerson(const char *fName, const char *lName, const char *phone);
	bool getPerson(Person &p);
	bool getPerson(char *fName, char *lName, char *Phone);
	bool findPerson(char *lastName, Person &p);
	bool findPerson(char *lastName, char *firstName, Person &p);
	void printBook();
	void sort();
	Person& operator() (int i);
};
#endif
Last edited on
The following version compiles, but I haven’t tested it carefully.

main.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
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
#include "addressBook.h"
#include <iostream>
#include <limits>

void waitForEnter();

int counter = 0;
int getcount = 0;

void printStruct(struct Person &print);

int main()
{
    AddressBook aBook;
    Person add;
    Person getStruct;
    std::cout << "Address Book\n\n";
    int choice { 1 };
    while (0 < choice && choice < 6)
    {
        std::cout << "\nEnter 1 to add to the address book."
                     "\nEnter 2 to get a person."
                     "\nEnter 3 to find a person by last name only."
                     "\nEnter 4 to find a person by last name and first."
                     "\nEnter 5 to print the book."
                     "\nEnter any other key to quit."
                     "\nPlease make a selection: ";
        std::cin >> choice;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        char last[20];
        char first[20];
        switch (choice)
        {
        case 1:
            std::cout << "Enter First Name: ";
            std::cin >> add.firstname;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Enter Last Name: ";
            std::cin >> add.lastname;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Enter Address: ";
            std::cin.getline(add.address, 49, '\n');
            aBook.addPerson(add);
            break;
        case 2:
            std::cout << "\nGetting the next person in the address book:\n\n";
            aBook.getPerson(getStruct);
            printStruct(getStruct);
            break;
        case 3:
            std::cout << "Please enter the last name to search for: ";
            std::cin >> last;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            if (aBook.findPerson(last, add)) { printStruct(add); }
            else  { std::cout << "Sorry could not find the name.\n"; }
            break;
        case 4:
            std::cout << "Please enter the last name and then the first name "
                         "to search for:\n";
            std::cout << "Last name: ";
            std::cin >> last;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "First name: ";
            std::cin >> first;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            if (aBook.findPerson(last, first, add)) { printStruct(add); }
            else { std::cout << "Sorry could not find the name.\n"; }
            break;
        case 5:
            aBook.printBook();
            break;
        default:
            break;
        }
    }
    waitForEnter();
    return 0;
}

void printStruct(struct Person &print)
{
    std::cout << print.firstname << ' ' << print.lastname << '\n';
    std::cout << print.address << '\n';
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


AddressBook.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
#ifndef ADDRESSBOOK
#define ADDRESSBOOK

constexpr int MAXIMUM = 10;

struct Person
{
    char firstname[20] {};
    char lastname[20] {};
    char address[50] {};
};

class AddressBook
{
private:
    Person person[MAXIMUM];
    int count;
public:
    AddressBook();
    AddressBook(char* fName, char *lName, char *address);
    AddressBook(const Person &p);
    AddressBook(const Person p[], int size);
    bool addPerson(const Person p);
    bool addPerson(const char *fName, const char *lName, const char *phone);
    bool getPerson(Person &p);
    bool getPerson(char *fName, char *lName, char *Phone);
    bool findPerson(char *lastName, Person &p);
    bool findPerson(char *lastName, char *firstName, Person &p);
    void printBook();
    void sort();
    Person& operator() (int i);
};

#endif // ADDRESSBOOK 


AddressBook.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
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
#include "AddressBook.h"
#include <cstring>
#include <iostream>

static int head = 0;
static int tail = -1;

AddressBook::AddressBook() {}

AddressBook::AddressBook(const Person &p) { addPerson(p); }

AddressBook::AddressBook(const Person p[], int size)
{
    for (int i = 0; i < size; i++)
    addPerson(p[i]);
}

AddressBook::AddressBook(char *fName, char *lName, char *address)
{
    Person tmp;
    std::strncpy(tmp.firstname, fName, 19);
    tmp.firstname[19] = '\0';
    std::strncpy(tmp.lastname, lName, 19);
    tmp.lastname[19] = '\0';
    std::strncpy(tmp.address, address, 19);
    tmp.address[49] = '\0';
    addPerson(tmp);
}

bool AddressBook::addPerson(const Person p)
{
    if (head < MAXIMUM)
    {
        person[head] = p;
        head++;
        if (tail == -1) { tail++; }
        return true;
    }
    return false;
}

bool AddressBook::getPerson(Person &p)
{
    if (tail >= 0)
    {
        if (tail >= MAXIMUM) { tail = 0; }
        p = person[tail];
        tail++;
        return true;
    }
    return false;
}

bool AddressBook::findPerson(char *lName, Person &p)
{
    for (int i = 0; i < head; i++)
    {
        if (!strcmp(person[i].lastname, lName))
        {
            p = person[i];
            return true;
        }
    }
    return false;
}

bool AddressBook::findPerson(char *lastName, char *firstName, Person &p)
{
    for (int i = 0; i < head; i++)
    {
        if (   !strcmp(person[i].lastname, lastName)
            && !strcmp(person[i].firstname, firstName))
        {
            p = person[i];
            return true;
        }
    }
    return false;
}

void AddressBook::printBook()
{
    for (int i = 0; i < head; i++)
    {
        std::cout << person[i].firstname << '\t' << person[i].lastname << '\t'
                  << person[i].address << '\n';
    }
}

void AddressBook::sort()
{
    Person temp;
    for (int i = 0; i < head; i++)
    {
        for (int j = 0; j < head; j++)
        {
            if (person[j + 1].lastname < person[j].lastname)
            {
                temp = person[j];
                person[j] = person[j + 1];
                person[j + 1] = temp;
            }
        }
    }
}

std::ostream& operator<<(std::ostream& output, const Person& p)
{
    output << '(' << p.firstname << ", " << p.lastname << ')';
    return output;
}

Person& AddressBook::operator() (int i) { return person[i]; }


It's the very first time I meet I code where operators are overloaded, but std::strings and std::vectors are rejected...
Topic archived. No new replies allowed.