Merging vectors help?

This is a program where it merges 2 vectors into one. As you can see, the program prompts the user to input elements for each vector. However, when I input -1 in order to stop input, it doesn't want to go to the next prompt. Please help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <algorithm> 

using namespace std;

vector<int> merge(vector<int> a, vector<int> b)
{
    vector<int> c(a.size() + b.size());
    merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
    return c;

}

int main()
{
    vector<int> a;
    cout << "Enter numbers for vector a: " << endl;
    int number;
    cin >> number;
    while (number > -1)
        a.push_back(number);
    if (number = -1)
        cout << "Thank You!" << endl;
      
    vector<int> b;
    cout << "Enter numbers for vector b: " << endl;
    int num;
    cin >> num;
    while (num > -1)
        b.push_back(num);
    if (number = -1)
        cout << "Thank You!" << endl;
     

    vector<int> c = merge(a, b);
    int i;
    for (i = 0; i < c.size(); i++)
        cout << c[i] << endl;
    return 0;

}



here's the error I get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
undeadnexus@ubuntu:~$ ./a.out
Enter numbers for vector a: 
1
2
3
4
5
-1
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)
undeadnexus@ubuntu:~$ 2
2: command not found
undeadnexus@ubuntu:~$ 3
3: command not found
undeadnexus@ubuntu:~$ 4
4: command not found
undeadnexus@ubuntu:~$ 5
5: command not found
undeadnexus@ubuntu:~$ -1
-1: command not found
This loop is infinite if the first entered number was greater than -1.:)

1
2
3
    cin >> number;
    while (number > -1)
        a.push_back(number);


You are not changing the value of number inside the loop.

Here you are using the assignment operator (=) instead of the comparision opertaor (==)

1
2
    if (number = -1)
        cout << "Thank You!" << endl;
Topic archived. No new replies allowed.