Apr 4, 2015 at 11:04pm UTC
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 Apr 4, 2015 at 11:14pm UTC
Apr 5, 2015 at 7:43pm UTC
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
are unique but they are
set to 0 anyways.
Last edited on Apr 6, 2015 at 3:26am UTC