I am teaching myself C++ and wrote a program that does the queue data structure. It runs corectly. I would like some feedback on how I can improve the program to make it run faster, use fewer lines of code, or be clearer to someone else. I haven't added my comments to the program so that is one area. For now, I want to concentrate on improving efficiency. Thanks in advance.

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

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

int    start() {
        cout << "Enter the desired activity.\n" << "1 for listing the queue items.\n" << "2 for adding an item to the queue.\n" << "3 for removing an item from the queue.\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;
        }
        return 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 queue is full.\n" << endl;}
        else {
            cout << "Enter the name to add to the queue.\n" << endl;
            cin >> s;
            int i = 0, top = 0;
               while (names [i] != "") {
                  top = i + 1;
                  i++;
               }
            names [top] = s;
            cout << "The name " << s << " has been successfully added to the queue.\n" << endl;
        }
        start();
}

void    remove_item() {
        if (names [0] == "") {cout << "The queue is empty.\n" << endl;}
        else {
        s = names [0];
        int i = 0;
           while (names [i] != "" && i < 5) {
               names [i] = names [i+1];
               top = i + 1;
               i++;
           }
        names [top] = "";
        cout << "The name " << s << " has been successfully removed from the queue.\n" << endl;
        }
        start();
}

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

Recommended Answers

All 4 Replies

Avoid use of global variables whenever possible.

start() is declared/defined with return type int, but the return value in main() is ignored.

start() is declared/defined with return type int, but the return value in main() is ignored.

You are right! I had initially made start() void and then changed it to return a value in a prior iteration of the program. I forgot to change it back to void after I made further modifications.

Thanks for pointing that out. I need to keep better track of the changes I make to a program as it evolves to its final version.

If you want to improve performance, you need to implement this as a ring buffer.

Also, your indentation scheme looks a little weird.

And, obviously, you shouldn't use global variables. Learn to create classes and use objects.

Finally, you include a header std_lib_facilities.hpp, I assume this header includes a number of standard C++ libraries and issues a using namespace std;. Both of these things are pretty bad. You should only include headers that you need, not all headers you could possibly need. Learn to use a minimal number of headers. And issuing a using namespace std; in the global scope is not recommended, learn to avoid doing that.

Thanks for your comments, Mike.

I took a look at ring buffers but that topic is too advanced for me right now. I will get back to it.

I am frustrated with the indentation thing. I continually find that the copy/paste procedure to post my code changes the indentation. I can't correct it all the time.

I am working on a linked list problem right now. I will incorporate classes into it so I can avoid using global variables.

I am learning from Stroustrup's book, (PPP C++), and he provides the header I am using from his website. I appreciate your point. I plan to concentrate on headers when I start working on multi-source file projects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.