Hello, our tutor set us a task to do , which was make a vector out of a class. So I've implemented a basic one but it doesnt work well... its runs ect ... but when you re print the entered elements it doesnt print the correct ones:

#include <iostream>

using namespace std;

class vector
{
public:
   vector(int ); //constructor
   ~vector(); //destructor
   void insertAt(  int value);
 int pop();
   int getmaxSize();
   int gettop();


private:

   int *theArray;
   int maxsize;
   int first;
};

vector::vector(int max)
   {
   theArray = new int[max];
   maxsize = max;
   first = -1;
   }

vector::~vector()
   {
   delete[] theArray;
   }

int vector::gettop()
   {
   return first;
   }

int vector::getmaxSize()
 {
    return maxsize;
 }

void vector::insertAt( int value)
{
   if( first >=0)
   {
   maxsize++;
   theArray[maxsize] = value;
   }
}

int vector::pop()
   {
   if(maxsize >=0)
      {
      int temp = theArray[maxsize];
      maxsize--;
      return maxsize;
      }
   else
      {
      cout << "Stack empty" << endl;
      return 0;
      }  
   }


int main()
{
   int sizeOfVector;
   int number;
   int i;

   cout << "Hello and welcome to my class vectors...." <<endl;
   cout << "Please enter the size of the vector" <<endl;

   cin >>sizeOfVector;

   vector v1(sizeOfVector);

   cout << "You've created an vector with the array size of : " << v1.getmaxSize() <<endl;

   cout << "The first element is: " << v1.gettop() <<endl;

for ( i = 0; i < sizeOfVector; i++)
{
   cin >> number;
   v1.insertAt(number);
}

cout <<"I'm now going to pop them off the stack" <<endl;

for (i = 0; i < sizeOfVector; i++)
   cout << v1.pop()<<endl;








   return 0;
}

heres what it does...say you entered the size 4: and enter 1,2,3,4. It prints out 3,2,1'0' ... but 0 wasn't entered....umm any ideas?

>if( first >=0)
This is never true since you initialize first to -1. Initialize it to 0 and the vector should work more like you expect.

Thanks for the reply, however changing -1 to 0 didnt make much difference, apart from now when i tell its a size for '3' and input for example ...'1 2 3 ' i get : '5 4 3 ' as output! When it should be 3 2 1

void vector::insertAt( int value)
{
   if( first >=0)
   {
   maxsize++;
   theArray[maxsize] = value;
   }
}

int vector::pop()
   {
   if(maxsize >=0)
      {
      int temp = theArray[maxsize];
      maxsize--;
      return maxsize;
      }
   else
      {
      cout << "Stack empty" << endl;
      return 0;
      }  
   }

In the InsertAt() function, you are incrementing maxsize and thus going further into the array, but not resizing it to fit these dimensions.

Then, the reason for the 5, 4, 3, is because the pop() function is returning maxsize, not the variable stored.

ok so the return statement is wrong... umm I'm totally lost as to what it should be... it should be the array location but the element stored there... how'd do I get about getting that?

just return the temp variable you assigned in the pop function, that should return the value in the array. But, like I said, you may want to initialize the values in the array before increasing the size of it.

okey dokey thankyou...it fixed it but theres another byg. If I specify that i want the array to be 3 ints in lenght and enter 3 ints and pop them its fine. Its only when I enter 5 for the size and in theory it should be able to hold 5 elements, it pops then (all 5 ) but then throws up a windows error

class_stack.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

(Followed by the debug button or a close button)

This is something I'd quickly made the other day (pursuing a question in another forum, which is why it may look entirely unrelated).

#include <stdio.h>

struct stack
{
   size_t size;
   int array[5];
};

struct stack mystack;

int push(int value)
{
   printf("push(%d)\n", value);
   if ( mystack.size < sizeof mystack.array / sizeof *mystack.array )
   {
      mystack.array[mystack.size++] = value;
      return 1;
   }
   return 0; /* fail */
}

int pop(void)
{
   puts("pop()");
   if ( mystack.size > 0 )
   {
      return mystack.array[--mystack.size];
   }
   puts("underflow");
   return -1;
}

size_t tos(void)
{
   return mystack.size;
}

int main(void)
{
   int i, j = 0, data[] = {10,20,30,40,50,60,70,80,90};
   for ( i = 0; i < 4; ++i )
   {
      if ( push(data[j]) )
      {
         ++j;
      }
      else
      {
         printf("push(%d) overflow\n", data[j]);
      }
      printf("tos() = %lu\n", (long unsigned)tos());
   }
   for ( i = 0; i < 2; ++i )
   {
      printf("pop() = %d\n", pop());
   }
   for ( i = 0; i < 5; ++i )
   {
      if ( push(data[j]) )
      {
         ++j;
      }
      else
      {
         printf("push(%d) overflow\n", data[j]);
      }
      printf("tos() = %lu\n", (long unsigned)tos());
   }
   for ( i = 0; i < 7; ++i )
   {
      printf("pop() = %d\n", pop());
   }
   return 0;
}

Note that I'm using a size_t, which is an unsigned type. This makes me take a different look at the indexing. One of the results is that I don't pop when there are zero elements.

That may not be what is going on in your code, but the crash certainly sounds like an out-of-bounds problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.