Initializing array using default constructor

Hey guys I've recently started programming and I'm having trouble with the concept of initializing an array.
I'm trying to make a program using only one array that reads 20 numbers. If the user already inputted this or the value is below 10||above 100 then it'll store it as 0.
Finally I display only unique values by displaying anything that isn't zero.

So the problem with my code is the output displays the values I don't want it too.
Not only that but the array isn't being initialized properly and I get these ridiculous -858993460 numbers.

Example:


input: 10 15 15 0 2 15
PROPER output: 10 15
my output: 10 15 15 15
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

//arrayinput.h
#include <array>
#include <string>

class arrayelimination
{
public:
    const static size_t limit = 20; 
    arrayelimination();
    void inputArray();
    void displayArray();

private:
    std::array < int , limit > store;
    int userinput;
};



//arrayinput.cpp
#include <iostream>
#include <array>
#include "arrayinput.h"
using namespace std;

arrayelimination::arrayelimination()
{
static array < int , limit> store;
}


void arrayelimination::inputArray()
{

for ( size_t i = 0; i < store.size(); i++)
{
    cout << "Enter number between 10-100 for array box [" 
        << i << "]: " ;
    cin >> userinput;
//check if numbers is between 10-100
        if (userinput >= 10 && userinput <= 100)
    {
        //MOST LIKELY ERROR check if number has previously been used.
            for ( int &check : store) 
            {
                   if ( check != userinput)
                {
                    store[i] = userinput;
                    break;
                }
                else
                    store[i] = 0;

            }
    }

    //output if number isn't between 10-100
else 
    store[i] = 0;

}
}

void arrayelimination::displayArray()
{
        cout << "Your unique array numbers stored include...\n";
    //output all the unique numbers that the user inputted.  
for ( size_t j = 0;  j < 20; j++)
{
//if the value is NOT 0, output.
    if (store[j] != 0)
    {
        cout << "array[ " << j << " ] = " << store[j] << "\n";
    }   
}
}


Any suggestions?
Last edited on
bump
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//arrayinput.h
#include <array>
#include <string>

class arrayelimination
{
    public:
        const static size_t limit = 7; // *** reduced to 7 for testing
        arrayelimination();
        void inputArray();
        void displayArray();

    private:
        std::array < int , limit > store;
        int userinput;
};



//arrayinput.cpp
#include <iostream>
#include <array>
#include "arrayinput.h"
using namespace std;

arrayelimination::arrayelimination() : store( {{0}} ) // **** initialise store to all zeroes
{
    //static array < int , limit> store; // **** remove this ****
}


void arrayelimination::inputArray()
{

    for ( size_t i = 0; i < store.size(); i++ )
    {
        cout << "Enter number between 10-100 for array box [" // **** avoid these magic numbers
        << i << "]: " ;
        cin >> userinput;
        //check if numbers is between 10-100
        if ( userinput >= 10 && userinput <= 100 ) // **** avoid these magic numbers
        {
            // *********************************************************
            bool duplicate = false ;
            for ( int check : store )
            {
                if ( check == userinput )
                    {
                        duplicate = true ;
                        break;
                    }
            }

            store[i] = duplicate ? 0 : userinput ;
            // *********************************************************
        }

        //output if number isn't between 10-100
        else
            store[i] = 0;

    }
}

void arrayelimination::displayArray()
{
    cout << "Your unique array numbers stored include...\n";
    //output all the unique numbers that the user inputted.
    for ( size_t j = 0;  j < store.size(); j++ )
    {
        //if the value is NOT 0, output.
        if ( store[j] != 0 ) // **** avoid these magic numbers
        {
            cout << "array[ " << j << " ] = " << store[j] << "\n";
        }
    }
}

int main()
{
    arrayelimination a ;
    a.inputArray() ;
    a.displayArray() ;
}

http://coliru.stacked-crooked.com/a/8d76fc9c941bb000
Thank you so much Borges! On a side note, I've also tried doing this with the sort and search algorithm...

1
2
3
4
sort(store.begin, store.end())
bool duplicate = binary_search(store.begin(), store.end(), userinput)

store [i] =  duplicate ? 0 : userinput;


However, I get a compiling error:

term does not evaluate to a function taking 1 arguments


Anything suggestions?
You forgot the parenthesis at your store.begin.

1
2
3
sort(store.begin, store.end())

sort(store.begin(), store.end()) // <-- Should look like this 

Alright thanks Neaj it compiles now!

But now I have a bug which I don't even know stems from what...

I get this now:

Testing using 10 digits.

input: 10 15 15 16 17 18 19 20 21 0
ouput: 10 16 18 21


Clearly
17 19 20
are unique but they are set to 0 anyways.
Last edited on
Topic archived. No new replies allowed.