Question

If I have an array of size MAX_SIZE and only have 20 index occupied how do you make it so that it stops printing 0s after itemList[20]? (I am reading in from a text file)

const int MAX_SIZE = 1000;
item itemList[MAX_SIZE];

for(int i= 0; i<MAX_SIZE;i++)
{
itemList[i].Print(); //prints members in item

  if(i==19) // I used this just to see what I was printing properly
   {         //I know it is bad practice so I would like an alternative.
       break; //Also, it is only possible if you have access to the text file.
   }
}
Was it helpful?

Solution

You can perform a basic check:

if(itemList[i].Function() == 0) break;

OTHER TIPS

As an alternative to break, you can use a while loop:

int i = 0;
while (itemList[i] != 0 && i < MAX_SIZE)
{
    itemList[i].Print();
    i++;
}

Replace itemList[i] != 0 with whatever expression you're using to determine whether the element is occupied or not.

Alternatively, keep track of how many elements there are as you build up the array from the file, and only loop that many times.

Better still, remember that you're using C++, not C. Add elements from the file to a container such as std::vector instead of a raw array, then just loop through the whole thing. This also fixes a serious bug in your code; namely, that you will have undefined behaviour when there are more than 1000 entries in the file.

const int MAX_SIZE = 1000;
item itemList[MAX_SIZE];

for (int i = 0; i < sizeof(aitemList / sizeof(*itemList)); i++)
{
    if (itemList[i] != 0)
        itemList[i].Print();
}

or use a vector

std::vector<int> itemList;

for (int i = 0; i < itemList.size(); i++) 
{
    if (itemList[i] != 0)
    {
        // do stuff
    }
}

A short lesson in:

  • correctly iterating through a null-terminated pointer array
  • cleaning up arrays of pointers in an exception safe way
  • solving your problem correctly

.

#include <iostream>
#include <algorithm>
#include <memory>

using namespace std;

struct Oper {
    void exec() const { cout << "Hello\n"; }
    ~Oper() { cout << "deleted\n"; }
};


int main()
{
    Oper* myArray[1000];
    fill(begin(myArray), end(myArray), nullptr);

    // make a sentinel to ensure that the array is cleaned up if there is an exception
    std::shared_ptr<void> sentinel(&myArray, [&](void*) {
        // clean up array
        for (auto p = begin(myArray) ; p != end(myArray) ; ++p) {
            delete *p;
        }
    });

    myArray[0] = new Oper;
    myArray[1] = new Oper;
    myArray[2] = new Oper;
    myArray[4] = new Oper; // note: missed 3

    for(auto p = begin(myArray) ; p != end(myArray) && *p ; ++p) {
        (*p)->exec();
    }

   return 0;
}

Output:

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1

Executing the program....
$demo 
Hello
Hello
Hello
deleted
deleted
deleted
deleted
for(int i= 0; i<MAX_SIZE && i<20;i++)

it looks better because using of break is a bad pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top