Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

c++ - Undeclared issue, not sure how to fix

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

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

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

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

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

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

";
            }

        } 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;
}
question from:https://stackoverflow.com/questions/65894880/undeclared-issue-not-sure-how-to-fix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...