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;
}

Edited 4 Years Ago by Nathaniel10: code tags

I have partly solved problem 2. The output window was closing immediately because the command to keep the window open, keep_window_open(), was not being applied. When I added it to each function, they worked correctly. However, I would like the program to prompt for further activities, list the stack again, add another item or remove another item. I need to call the start() function again after each the activity functions have completed their tasks. I am having difficulty doing that.

put lines 44-54 in a while loop and use whatever technique you're comfortable with keeping it going/stopping when you want to.

Lerner, your suggestion worked after some trial and error. Here is my updated code. It does everything it is supposed to do. Unfortunately, it also does some things it is NOT supposed to do. Still, I consider the program a success and I am on to queues. Will probably post about that soon.

#include "../../std_lib_facilities.h"

        int top;
        int act;
        string s;
        string names [6] = {"James", "John", "Jerrold", "Jennifer", "", ""};

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;
        }
}

void    list_items() {
           for (int i = 0; i < 6; i++) {
               cout << i + 1 <<", " << names[i] << endl;
           }
        start();
}

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;
        }
        start();
}

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;
        }
        start();
}

int main()
{
act = 0;
while (act != 4) {
   start();
   if (act == 1) {list_items();}
   if (act == 2) {add_item();}
   if (act == 3) {remove_item();}
   if (act == 4) {keep_window_open();}
}
return 0;
}

Edited 4 Years Ago by Nathaniel10: code tags

This question has already been answered. Start a new discussion instead.