The specified vector does not pass through the functions

The specified vector for functions does not contain information in memory. Do I need to use pointers?

Thanks.

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

void menu();
void addvector(vector<string>& vec);
void subvector(vector<string>& vec);
void vectorsize(const vector<string>& vec);
void printvec(const vector<string>& vec);
void printvec_bw(const vector<string>& vec);

int main()
{
    vector<string> svector;

    menu();

    return 0;
}
//functions definitions

void menu()
{
    vector<string> svector;
    int choice = 0;

        cout << "Thanks for using this program! \n"
             << "Enter 1 to add a string to the vector \n"
             << "Enter 2 to remove the last string from the vector \n"
             << "Enter 3 to print the vector size \n"
             << "Enter 4 to print the contents of the vector \n"
             << "Enter 5 ----------------------------------- backwards \n"
             << "Enter 6 to end the program \n";
        cin >> choice;

        switch(choice)
        {

                case 1:
                    addvector(svector);
                    menu();
                    break;
                case 2:
                    subvector(svector);
                    menu();
                    break;
                case 3:
                    vectorsize(svector);
                    menu();
                    break;
                case 4:
                    printvec(svector);
                    menu();
                    break;
                case 5:
                    printvec_bw(svector);
                    menu();
                    break;
                case 6:
                    exit(1);
                default:
                    cout << "not a valid choice \n";

            // menu is structured so that all other functions are called from it.
        }

}

void addvector(vector<string>& vec)
{
    //string line;

     //int i = 0;
        //cin.ignore(1, '\n');
        //cout << "Enter the string please \n";
        //getline(cin, line);
        vec.push_back("the police man beard is half-constructed");    

}

void subvector(vector<string>& vec)
{
    vec.pop_back();
    return;
}

void vectorsize(const vector<string>& vec)
{
    if (vec.empty())
    {
        cout << "vector is empty";
    }
    else
    {
        cout << vec.size() << endl;
    }
    return;
}

void printvec(const vector<string>& vec)
{
    for(int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << endl;
    }

    return;
}

void printvec_bw(const vector<string>& vec)
{
    for(int i = vec.size(); i > 0; i--)
    {
        cout << vec[i] << endl;
    }

    return;
}

      

+2


a source to share


5 answers


Your problem is that each call to the menu () creates a new vector that hides the previous one, so it seems to you that they are empty. If you really want to recursively call the menu, pass it the vector reference you basically created.



All that said, menu systems are rarely recursive. You probably want to loop around your menu call () in the main loop until the user decides to exit.

+5


a source


Your function is menu

recursive.

This means that each new call menu

will create one of its own vector

and throw it away when done.



If you want to reuse svector

, you need to either use a simple loop for the menu, or pass the original svector

one created in main

to menu

using the -reference pass.

+4


a source


The problem is that you are calling another menu()

that allocates a new one svector

on the stack (at some part of the memory). The source code was probablyvoid menu(vector<string>&svector)

+1


a source


You need to either have to svector

be global (declared outside of any function), or pass it as a parameter to menu

. C ++ has no dynamic scope for functions.

EDIT: You can also wrap the whole thing in a class and call it again menu()

.

0


a source


svector

is shared (is that what it means s

?), and variables that are shared between different scopes in C ++ have to be declared extern

or you end up with two separate local variables.

int main()
{
    extern vector<string> svector;


void menu()
{
    extern vector<string> svector;

      

Just joking. svector

must be an argument. Or global is enough. But don't use globals like this.

0


a source







All Articles