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