Error after inputting seven numbers

This error always occur after I input 7 numbers. What could it be?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <limits>
#include <vector>

void checkingInput(int &);
void displayNumbers(std::vector<int> &);

int main()
{
    int input = 0;
    std::vector<int> myVector(0);

    std::cout << "Enter a number to find the answer of the universe! ";

    while(input != 42)
    {
        std::cin >> input;
        checkingInput(input);
        if(input == 42)
        {
            break;
        }
        else if(input > 99)
        {
            input = input % 25;
            myVector.push_back(input);
        }
        else
            {
                myVector.push_back(input);
            }
    }

    std::cout << std::endl << std::endl;

    std::cout << "These are the numbers that you inputted: ";

    displayNumbers(myVector);

    std::cout << std::endl;

    return 0;
}

void checkingInput(int &num)
{
    while(!(std::cin) || num < 0)
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "This is not a number or a positive number. ";
        std::cin >> num;
    }
}

void displayNumbers(std::vector<int> &myVector)
{
    for(unsigned int i=0; i < myVector.size(); i++)
    {
        std::cout << myVector[i] << " ";
    }
}
Last edited on
I decide to put this program in debug mode to find out what the error was and it turned out to be a segmentation fault at line 44.
Last edited on
The first problems I notice are in the following snippet:
1
2
    int numberCounter = 0;
    int numArray[numberCounter];


First array sizes must be at least 1, zero sized arrays are not allowed.

Second array sizes must be compile time constants.

Third, unless you have potential of overrunning your array in your data entry loop. You need to insure you don't try to enter more numbers than your array can hold.


Well here are a couple of problems. If I change the numberCounter to constant, then I won't be able to increment it. The reason I left it like that was because I don't know how many numbers the user is going to input(it could be 100 or 1000) before quitting. At this point, I think using vectors might be a better option.
At this point, I think using vectors might be a better option.

Vectors are far superior to arrays, IMO, when you don't know how many entries you will get.

Alright, fixed it with vectors. Hurrah for vectors.
Topic archived. No new replies allowed.