Hey everyone so here's a new code and it compiles but it won't let me separate the data and add more questions for the user. Please help me and thanks in advance!
I need it to enter 3 elements after list2

header file

#ifndef H_myArray
#define H_myArray

class myArray
{
public:
    myArray();
    myArray(int, int);
    ~myArray();
    int &operator[](int i);
    int fillArray();
    void setZero();
    int getSize();
    int getIndex();

private:
    int *location;
    int size;
    int start;
    int end;
};

#endif

cpp file

#include <iostream>
#include "myArray.h"

using namespace std;

//List 1 constructor
int &myArray::operator[](int i)
{
    cout << "Enter 5 integers: ";
    for (i = 0; i <= 10; i++)
    {
        cin >> i;
    }
    {

        cout << "After filling list1: " << i << endl; 
        cout << "List2: " << i << endl;
        cout << "Enter 3 elements: ";
        cin >> endl;
    }

    return location[i];
}

myArray::myArray(int index, int arraySize)
{
    size = (arraySize - index);

    for (int i = index; i < arraySize; i++)

        location = new int[size];

    start = index;
    end = arraySize;
}

myArray::~myArray()
{
    delete[]location;   //Deletes array
}

void myArray::setZero()          //Sets the array equal to zero
{
    for (int i = 0; i < size; i++)
    {
        location[i] = 0;
    }
}
int myArray::getSize()
{
    return size;
}
int myArray::getIndex()
{
    return start;
}

int main()
{
    myArray  list(0, 5);
    myArray  myList(2, 13);
    myArray  yourList(-5, 9);
    cout << "List 1: 0 0 0 0 0" << endl;

    list[] = 444;

    return 0;
}

Edited 1 Year Ago by pritaeas: Moved to CPP

Let's start with this:

cout << "Enter 5 integers: ";
for (i = 0; i <= 10; i++)
{
    cin >> i;
}

First, you loop for 10, not 5 integer values. Second, the terminating condition should not be <= but should be <. Third, you never add the input (i) to the array.

Score? 20%. You need to do serious review of your own code and understand the logic you are trying to express. So far, not good... In my class, you'd get an 'F'. :-(

Edited 1 Year Ago by rubberman

Ok I understand. Thanks but how do I fix it only reading the first number when I need it to read all 5?

Huh? Reading first but need to read all 5? Do you only need to read the first value, or do you need to read all 5? If all five, then the loop invariant would be for (i = 0; i < 5; i++). What specifically are you trying to say? Do you want to optionally take less than 5 values? That's another problem entirely.

Edited 1 Year Ago by rubberman

No it needs to read the five integers the user inputs then adds it to the list which reads back to you, but it only reads the first number. That's my problem now haha

Look at the code again. You are setting the variable 'i' to what the user inputs, so if it is > 5 (or >= 10 in your example) then it will break out of the loop. Sorry, but I missed that on the first pass! Use a local variable, and add that to the array. IE,

for (int i = 0; i < 5; i++)
{
    int foo = 0;
    cin >> foo;
    location[i] = foo;
}

This is not to say there are not other errors in your code. You still need to do some serious code inspection. :-)

Edited 1 Year Ago by rubberman

read the five integers the user inputs then adds it to the list which reads back to you ...

At first, just use an array of int to simulate your array class for testing and step by step development design ...

// takeIn5NumbersAndPrint.cpp //  // 2015-02-26 //

#include <iostream>

using namespace std;

const char* PAUSE = "\nPress 'Enter' to continue/exit ... ";


int main()
{
    int ary[5] = {0}; // get 'array' to hold 5 ints ...

    // take in loop ...
    for( int i = 0 ; i < 5 ;  )
    {
        cout << "Enter integer # " << (i+1)  << " (of 5) "<< ": ";
        if( cin >> ary[i] ) ++ i;
        else
        {
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream ...
            cout << "\nERROR! Only integers are valid input here ...\n\n";
        }
    }

    // display loop ...
    cout << "\nshowing the 5 values entered ... \n";
    for( int i = 0 ; i < 5 ; ++ i )
         cout << ary[i] << endl;


    cout << PAUSE << flush;
    cin.sync();
    cin.get();
}

Now you can work on your array class.

Edited 1 Year Ago by David W

//Ch13 Ex11 myArrayList written by
//Jazmine Nicholson
#include <iostream>
#include "myArray.h"


using namespace std;

//List 1 constructor
const char* PAUSE = "\nPress 'Enter' to continue/exit.";

int main()
{
    int list1[5] = { 0 };//hold five integers
    for (int i = 0; i < 5;)
    {
        cout << "Enter five integers " << (i);//Integers: 8,3,10,4,15
        if (cin >> list1[i])++i;
        else
        {
            cin.clear();
            cin.sync();
            cout << "After filling list1: ";
            for (int i = 0; i < 5; ++i)
                cout << list1[i] << endl;

            cout << PAUSE << flush;
            cin.sync();
            cin.get();

            cout << "list2 : ";
            for (i = 0; i < 5; i++)
                cout << list1[i] << " ";
            cout << endl;
        }
        cout << "Enter 3 elements: ";

        for (i = 0; i < 3; i++)
            cin >> list1[i];
        cout << endl;
        cout << "First three elements of list1: ";
        for (i = 0; i < 3; i++)
            cout << list1[i] << " "; cout << endl;//3 elements: 12,20,19
        cout << "list3: " << (0);
        cout << "list3: " << list1[i] << endl; // 7,0,54,0,15,0,8,0
    }
}

{   
myArray list3(-2, 6);       
    cout << "list3: ";
    for (i = -2; i < 6; i++)
        cout << list3[i] << " ";
    cout << endl;

    list3[-2] = 7;
    list3[4] = 8;
    list3[0] = 54;
    list3[2] = list3[4] + list3[-2];

    cout << "list3: ";
    for (i = -2; i < 6; i++)
        cout << list3[i] << " ";
    cout << endl;
    system("pause");

    return 0;
}

So I need the program to read the "list2" and so on. What am I doing that it cancels it?

You seem more than just a little lost :)

Programming can become enjoyable when you understand how to break up a problem into steps and get a working solution for each step.

The example code I gave you above ... demo's ONLY a 'first step' ...

a student way to take in valid int's, one at a time, each int being individually prompted for input.

If you wish to take in 5 int's, all on one line, all at once, that is a different problem.

You best get that part working and understood before you try to code an array class.

Note the loop if you wish to take in VALID int's, ONE AT A TIME:

// take in loop ...
for( int i = 0 ; i < 5 ; )
{
    cout << "Enter integer # " << (i+1) << " (of 5) "<< ": ";
    if( cin >> ary[i] ) ++ i; // since good input here, can ++i //
    else
    {
        cin.clear(); // clear error flags
        cin.sync(); // 'flush' cin stream ...
        cout << "\nERROR! Only integers are valid input here ...\n\n";
    }
}

Edited 1 Year Ago by David W

But ...

If you wished to take in ONLY 5 VALID int's,
ALL AT ONCE,
ALL on one line ...
you could code something like this:

// take in loop ...
for( ; ;  )
{
    cout << "Enter 5, space separated integers, all on one line: ";
    if( cin >> ary[0] >> ary[1] >> ary[2] >> ary[3] >> ary[4] && cin.get() == '\n' ) break;
    else
    {
        cin.clear(); // clear error flags
        cin.sync(); // 'flush' cin stream ...
        cout << "\nERROR! Only FIVE integers are valid input here ...\n\n";
    }
}

Understanding how this works:

cin >> someIntVar;

is key here!!!

cin will skip over any/all leading whitespaces until it finds a non-whitespace char

if that char is a valid int char, it will accept it and keep taking further valid int char's until a non-int char is reached, which non-int char then will be left in the cin stream

but if that first non-whitespace char was NOT a valid int char, an error flag will be set, etc...

thus ... for invalid int input, can clear error flags and 'flush' cin stream and output some error message and loop again for valid input.

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.