I have an assignment that requires the program to read in 20 numbers from a user input into an array. The conditions require the value to be in between 10-100 and non repeated. However, it's not supposed to prompt the user and simply doesn't store the value; finally the program has to print out the user's values that were unique. Proper results For example:
input = 9 10 15 15 15 0
output = 10 15
//this is a small example with a 6 element array instead of 20
When I test my program I only get
input: 9 10 15 15 15 0
output: 10 15 15 15
//this is a small example with a 6 element array instead of 20
I wrote the code using a range-based loop to check the values and set the value to 0 if it doesn't fulfill the conditions. So anything that isn't zero wouldn't be printed out. I've gone through all the questions on stack overflow and I couldn't find an answer to my specific problems:
- How to initialize all array element to zero using class constructors.
- Making it "static" so that when I run another function previous array values are global and maintained from the user input.
Something seems wrong about the loop I created but it looks perfect. I checked with my classmates and they agreed too.
//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()
{
array < int , limit> store = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
}
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";
}
}
}
When I test my program I only get
input: 10 15 15 15 2 0 0 0 0 0 0 0 ... 0
output: 10 15 15 15
The concept of setting it to zero works but the repeated values aren't unique.
I have to use an object-oriented design as a requirement to this assignment. I'm near a dead-end I really don't know how this work. Please help me.
Aucun commentaire:
Enregistrer un commentaire