Undeclared issue, not sure how to fix

Viewed 21

This program is meant to store a list of usernames, add usernames, and delete them as well. My code works as intended except for the first step. Whenever i enter a name to be stored nothing shows up on the list, and because of this nothing can be deleted. Im not sure where i messed up because i went over it and everything looks fine. I also do not get any error messages so i dont have any direction as to what i should be looking for. Any help would be great, thanks.

#include <iostream>
#include <string>
#include <vector>
using std::cout, std::cin, std::string, std::vector;

void printUserList(vector<string> listUser) {
    cout << "Current User List\n";

    for(int x = 0; x < listUser.size(); ++x) {
        cout << x << "" << listUser[x] << "\n";
    }
}

int main() {
    vector<string> addUser;
    vector<string> userBase;
    string userName;
    char choice;
    bool keepLooping = true;

    while(keepLooping) {
        cout << "User Options:\n"
             << "1. Add a username\n"
             << "2. List all username\n"
             << "3. Delete a username\n"
             << "X. Exit\n";
        cout << "Enter Option: ";
        cin >> choice;
        cin.ignore();

        switch(choice) {
        case '1': {
            cout << "Enter username: ";
            getline(cin, userName);
            cout << "\n";

            if(userName.size() > 0) {
                addUser.push_back(userName);
                cout << "User has been added\n\n";
            }

        } break;

        case '2': {
            printUserList(userBase);
        } break;

        case '3': {
            int index;
            printUserList(userBase);

            cout << "Enter the index of the user: ";
            cin >> index;
            cin.ignore();

            if(index < userBase.size()) {
                userBase.erase(userBase.begin() + index);

                cout << "User deleted";
            }

        } break;

        case 'X': {
            userBase.clear();
            keepLooping = false;
        } break;

        default: {
            cout << "Option not found, please try again.";
        }
        }
    }
    return 0;
}
2 Answers

It's because you add the user to a vector (named addUser) that you don't use for anything but adding users.

        case '1': {
            cout << "Enter username: ";
            getline(cin, userName);
            cout << "\n";

            if(userName.size() > 0) {
                addUser.push_back(userName);       //  <- here
                cout << "User has been added\n\n";
            }

        } break;

In all the other places you're using a different vector, userBase, so change the above line to

                userBase.push_back(userName);

You have 2 separate vector objects - addUser and userBase. You are adding all of your usernames to addUser only, but you are printing and deleting usernames from userBase instead. That is why you don't see anything.

Get rid of addUser altogether, you don't need it. Add your usernames to userBase instead.

Related