-3

So I am pretty new to coding and am having some issues with storing user input into a vector using the push_back function. can some one tell me what I am doing wrong?

  vector<int> user_nums;

     switch(selection){
        case 'P':
        case 'p':
            if(user_nums.empty()){
                cout << "[]- list is empty" << endl;
            }else{
                for(auto nums: user_nums)
                    cout <<"[ " << nums << " ]" << endl;
            }
            break;
        case 'A':
        case 'a':
            int new_num;
            cout << "\nEnter a number you would like to add: ";
            cin >> new_num;
             user_nums.push_back(new_num);
            cout << new_num << " was added" << endl;
            break;

This is in a do while loop. The code executes just fine, the problem is when I prompt the user to add a new number the value does not store in the vector. So when the user makes the selection to print the numbers, the list still shows empty.

  • What specific issue are you having? Are you getting a compiler error? If so, could you [edit your question](https://stackoverflow.com/posts/58585911/edit) and add the compiler output? –  Oct 28 '19 at 04:52
  • Also, while you're at it, could you copy and paste your code as text? Pictures can be a lot harder to read. –  Oct 28 '19 at 04:53
  • 1
    Please see "[ask]" and the linked pages and "[mcve](https://stackoverflow.com/help/minimal-reproducible-example). Code MUST be the minimal example that demonstrates the problem in the body of the question, not as an image. – the Tin Man Oct 28 '19 at 04:57
  • 1
    The image is hard to read, but I see a Debugger option up there. Debuggers are the bomb. You can step through the program line by line looking for the unexpected. The unexpected is usually a bug. – user4581301 Oct 28 '19 at 05:00
  • I'm voting to close this question as off-topic because contains a picture of the code, not the code. – Omnifarious Oct 28 '19 at 05:20
  • @Chipster - Not just harder to read, but it kills searchability, which makes the question useless for everybody else. It's also horrible for blind people. Personally, I think questions that contain pictures of code instead of actual code should be deleted within seconds of being posted. – Omnifarious Oct 28 '19 at 05:23
  • @Omnifarious Yeah, but a new contributor might not understand those things. "Harder to read" gives a reason the the OP is more likely to understand. –  Oct 28 '19 at 05:26
  • @Omnifarious I have to agree. It takes more effort to take a screenshot, save it, upload it, and link it to a post, than to simply copy/paste the actual code text. http://idownvotedbecau.se/imageofcode – Remy Lebeau Oct 28 '19 at 06:46
  • 1
    I'm sorry this is my first time ever on here I didn't know. I will update the question and just copy the code. Sorry for the inconvenience – Patrick Gillen Oct 28 '19 at 13:35
  • @PatrickGillen - As soon as I notice that you've done it, I'll vote to re-open and answer the question as soon as I notice that I can. – Omnifarious Oct 28 '19 at 18:16

1 Answers1

0

So, you're missing the most relevant sections of your code here. You should really be posting a minimal, reproducible example. Often the process of creating one will cause you to find your problem.

In this case though, it's obvious enough. There's a loop wrapping all this code. The vector is declared inside the loop. That means, at the bottom of the loop the vector will be destroyed, and then as the loop is executed again, a new empty vector will be created.

The solution is to move the declaration of the vector outside the loop.

If I've guessed wrong about the structure of the code you haven't shown us, then please follow the guideline I linked to above.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • @PatrickGillen - While I don't care about the points, the polite thing to do is to accept what you consider the best answer by clicking the checkmark next to it. Also, do you understand what you were doing wrong and why you were getting the result you were getting? – Omnifarious Oct 30 '19 at 00:47
  • 1
    Sorry about that, I accepted it. Yes I do. I declared the vector inside my do-while loop so the input I was trying to store from the user would never actually store there because of the loop. When I declared it outside of the loop it was actually able to store in the vector because it was not being destroyed at the end of the loop. Thank you for the help and sorry again for the poor formatting of the question. – Patrick Gillen Oct 30 '19 at 04:11
  • @PatrickGillen - It's alright. You're learning. The point about pictures of code applies widely. So, even in places where code can't be formatted easily, link to a pastebin (like pastebin.com) instead of pasting a picture. – Omnifarious Oct 30 '19 at 13:14