Help - Container Class Program makes sense but not working

Here is my program...When I run it with the loop it works, but when I insert individually it works until the fourth insert and then gives me gives me gibberish. It should work until the 5th insert and then print "the container is full.....". Hopefully somebody will see what I am doing wrong.

#include <iostream>
#include <iomanip> // for setw()
#include <assert.h>
using namespace std;

const int CAPACITY = 5; // set the storage capacity of the container to 5
class container {
public:
// constructor
// Post-condition: data array elements are initialized to 0; count is set to -1
container()
{
count = 0;
}

// Pre-condition: an integer value is passed to the function
// Post-condition: if data array is not full, increment count by 1 and insert the value in the array
// Otherwise, display "Container full! No insertion is made." //
void insert(int n)
{
if(!full())
{
count++;
data[count] = n;

//count++;
//cout << count << '\t' << data[count] << '\t' << n << endl;
} else{
cout << "Container full! No insertion is made" << endl;
}
}

// Post-condition: if data array is not empty, remove the data[count] element in data array and decrement count by 1;
// otherwise, display a message "Container empty! Nothing is removed from it."
void remove()
{
if(!empty())
{
int n = data[count];
count--;
}else{
cout << "Container empty! Nothing is removed from it" << endl;
}
}

// Post-condition: if the array is not empty, displays all values in data array similar to the sample output;
// Otherwise, display the message “Container is now empty!"
void display()
{
if (!empty())
{
for(int i =1;i <= count; i++)
{
cout << data[i] << '\t'; // << endl;
}
}else{
cout << "Container is now empty" << endl;
}

}
private:
bool empty()
{
return (count ==0);
}
// Post-condition: returns true is the array is empty, otherwise returns false
bool full()
{
return (count == CAPACITY);
}

// Post-condition: returns true is the array is full, otherwise returns false
int data[CAPACITY]; // statically allocated array used to store or contain the inserted integer values
int count; // indicates how many values have been inserted
};

// The main driver function to be used to test your implementation
int main()
{
container c; // Empty container


/*
for (int i = 1; i <= 5; i++)
{
c.insert(i);
cout << "After inserting " << i << ", the \"container\" contains:" << endl;
c.display();
cout << endl;
}
cout << "\nContainer is now full; intentionally attempt to insert value 6:" << endl;*/

c.insert(6);
c.display();
cout << endl;
c.insert(7);
c.display();
cout << endl;
c.insert(8);
c.display();
cout << endl;
c.insert(9);
c.display();
cout << endl;
// c.insert(10);
//c.display();
//cout << endl;
//c.insert(11);
//c.display();
//cout << endl;


//cout << endl << endl;
for (int i = 1; i <= 5; i++)
{
c.remove();
cout << "After removing a value from it, the \"container\" contains:" << endl;
c.display();
cout << endl;
}

cout << "Attempt to remove a value from an empty \"container\":" << endl;
c.remove();
cout << endl;

cin.get();

return 0;
}







Classical off-by-one and buffer overflow errors. Fix:
1
2
3
4
5
6
7
8
9
void insert(int n)
{
    if(!full()) {
        count++;
        data[count++] = n;
    } else{
        cout << "Container full! No insertion is made" << endl;
    }
}

1
2
3
4
5
6
7
8
9
void remove()
{
    if(!empty()) {
        int n = data[--count];
        count--;
    }else{
        cout << "Container empty! Nothing is removed from it" << endl;
    }
}

1
2
3
4
5
6
7
8
9
void display()
{
    if (!empty()) {
        for(int i = 0;i < count; ++i) {
            cout << data[i] << '\t'; // << endl;
         }
    }else{
        cout << "Container is now empty" << endl;
    }
Last edited on
Thanks for the quick response..But could you explain why were there no errors when I ran using the loop to insert 5 values? If you uncomment the loop, the program runs fine.
That is simple: when you insert your fifth value you insert it at the position data[5] which is one past the end of array acually — out of bounds. Your compiler just happens to place count variable here. You are overwriting count variable with garbage data. It is likely become > 5 that why CAPACITY == count check in isfull() is not working (BTW this need fixing too). It is happened to work with your loop because you tried to overwrit count which was 5 at the time with the value of 5, making program run as expected.

You can see here prime example of undefined behavior: accessing out of bounds, and buffer overflow attack (printing stack content)
Last edited on
Thanks!!
Topic archived. No new replies allowed.