-2

Why won't it work?

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;


int main() {

    int in;
    cin >> in;

    vector<int> list;
    int number;
    while (cin >> number) {
    list.push_back(number);
    }
    
    vector<int> range = {1, 2, 3, 4, 5};

    for (auto item : range) {
        bool truth = false;
        for (auto thing : list) {
            if (item == thing)
                truth = true;
        }
        if (truth == false)
            cout << item << endl;
    }
    return 0;
}

I tried inputting "5" then under a new line "1 2 3 4" and there is no output. I expected an output of "5".

Here is the problem: https://cses.fi/problemset/task/1083

ior2 2
  • 1
  • 3
    Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Richard Critten Jan 14 '23 at 10:26
  • 1
    When do you think the loop `while (cin >> number)` is going to end? Hint it will not end just because you typed a new line. – john Jan 14 '23 at 10:31
  • This approach is not going to work with the given problem sizes. – molbdnilo Jan 14 '23 at 10:34
  • An early step when creating a [mre] should be to eliminate user input. For this code, that means eliminating `in` and `number`. Keep `list`, but initialize it directly to your example case: `vector list = {1, 2, 3, 4};`. Then proceed to `range` and the rest. If the problem does not disappear, you've made your code simpler and your problem easier to reproduce. If the problem does disappear, you can focus on user input (bring back what you dropped, then drop `range` and the rest, and add output of `list` to demonstrate it is not what you expect). – JaMiT Jan 15 '23 at 08:06

1 Answers1

1

Your input loop should look like this

for (int i = 0; i < in - 1; ++i) {
    int number;
    cin >> number;
    list.push_back(number);
}

You seem to be under the impression that input from cin will somehow stop just because you typed a new line, but that is not true.

There are other issues with your code, but the first task is to get the input working correctly.

john
  • 85,011
  • 4
  • 57
  • 81