I am teaching myself C++ and amd now trying to simple programs related to data structures. I wrote this code for a simple stack. It is not working. I am frustrated and tired so I cannot think through the issue.
There are two main problems with which I need help.
1.
While my code compiles and runs, there are two warnings:
Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
Loaded 'C:\WINDOWS\system32\kernel32.dll', Cannot find or open the PDB file
I don't know what those warnings mean. I am using MS VC++ 2010 Express.
2.
The code does not run correctly. Option 1, (list the stack), causes the program to immediately stop and the output window to close. Option 3, (remove an item), does the same thing. Option 2, (add an item), correctly asks for a name to add but closes the output window after the name is input. Only option 4, exit the program, works correctly.
Here is my code. Thanks in advance.
#include "../../std_lib_facilities.h"
int top;
int act;
string s;
string names [6] = {"James", "John", "Jerrold", "Jennifer", "", ""};
void list_items() {
for (int i = 0; i < 6; i++) {
cout << i + 1 <<", " << names[i] << endl;
}
}
void add_item() {
if (names [5] != "") {cout << "The stack is full.\n" << endl;}
else {
cout << "Enter the name to add to the stack.\n" << endl;
cin >> s;
int i = 0;
while (names [i] != "") {
top = i;
i++;
}
names [top + 1] = s;
cout << "The name " << s << " has been successfully added to the stack.\n" << endl;
}
}
void remove_item() {
if (names [0] == "") {cout << "The stack is empty.\n" << endl;}
else {
int i = 0;
while (names [i] != "") {
top = i;
i++;
}
s = names [top];
names [top] = "";
cout << "The name " << s << " has been successfully removed from the stack.\n" << endl;
}
}
void start() {
cout << "Enter the desired activity.\n" << "1 for listing the stack items.\n" << "2 for adding an item to the stack.\n" << "3 for removing an item from the stack.\n" << "4 to exit the program.\n" << endl;
cin >> act;
while (act != 1 && act != 2 && act!= 3 && act != 4) {
cout << "Please enter a choice 1 through 4.\n" << endl;
cin >> act;
}
if (act == 1) {list_items();}
if (act == 2) {add_item();}
if (act == 3) {remove_item();}
if (act == 4) {
keep_window_open();
}
}
int main()
{
start();
return 0;
}