Hi, all i have big assignment and don't have any compiler problem just have runtime bug--this make more headache.

Here is the driver;

int main{
          int * values2 = new int [ARRAY_SIZE * 2];
	  values2[ARRAY_SIZE * 2 - 2] = 8;
	  values2[ARRAY_SIZE * 2 - 1] = 5;
         stack.pop (values2, 0);
/*stack belong to Intstack class and the pop function is to pop everything inside the stack into the values2;that means pop(values2,0) didn't do anything, but pop(values2,1) means pop out the top int element inside stack to the values2[0]. There already have 200 int element inside the intstack*/
	  stack.pop (values2, 1);
       	   stack.pop (&(values2[1]),ARRAY_SIZE - 1);
     //print out the array  
     std::cout << "About to output  values2 arrays" << std::endl;

		for (i = 0; i < COUNTOF (values); ++i)
		{
			std::cout << << values2 [i] << std::endl;
		}

		std::cout << "Finished output of values2 arrays" << std::endl;
}

Here is my code

class Intstack 
{
//private element top, p and size;
//public functions and constructor including pop and overload pop function;
};

void IntStack::pop(int a[],size_t n)//this is my pop function to implement the main
{
 for(int i=0;i<n;i++)
 {
 
  if(top==-1)
  {cout<<"There is empty\n";}
  else
  {
   int v;
   v=p[top];
   top--;
   a[i]=v;
  }
 }
}

So basically, the results should like:

About to output values2 arrays
element from 1-200
Finished output of values2 arrays

Recommended Answers

All 8 Replies

Eewww, Slove your own bug.

Sorry about the format.
Post it again:
Hi, all i have big assignment and don't have any compiler problem just have runtime bug--this make more headache.

Here is the driver;

/*stack belong to Intstack class and the pop function is to pop everything inside the stack into the values2;that means pop(values2,0) didn't do anything, but pop(values2,1) means pop out the top int element inside stack to the values2[0]. There already have 200 int element inside the intstack*/

int main{
          int * values2 = new int [ARRAY_SIZE * 2];
	  values2[ARRAY_SIZE * 2 - 2] = 8;
	  values2[ARRAY_SIZE * 2 - 1] = 5;
         stack.pop (values2, 0);

	  stack.pop (values2, 1);
       	   stack.pop (&(values2[1]),ARRAY_SIZE - 1);

     //now print out the array  
     std::cout << "About to output  values2 arrays" << std::endl;

		for (i = 0; i < COUNTOF (values); ++i)
		{
			std::cout << << values2 [i] << std::endl;
		}

		std::cout << "Finished output of values2 arrays" << std::endl;
}

Here is my code

class Intstack 
{
//private element top, p and size;
//public functions and constructor including pop and overload pop function;
};

void IntStack::pop(int a[],size_t n)//this is my pop function to implement the main
{
 for(int i=0;i<n;i++)
 {
 
  if(top==-1)
  {cout<<"There is empty\n";}
  else
  {
   int v;
   v=p[top];
   top--;
   a[i]=v;
  }
 }
}

So basically, the results should like:

About to output values2 arrays
element from 1-200
Finished output of values2 arrays

My problem is on the runtime the array pointer give me the address instead of the real int element. I don't know where is the problem.
Thanks la.

Ok, this is some bugs when i post it here.
Sorry about that.
I check my code again in the windows XP platform its working fine now. Don't know why??
Cause i run it at home which the OS is vista, it have some bugs.

How is anyone suppost to GUESS what is wrong with that ??

There is no definition for Intstack but a cryptic comment about what is private/public.

Beginners using #define macros/parameters are heading for a (100-epsilon)% certainty of having incomprehensible bugs. Use const int or a function.

There are no declarations. What is ARRAY_SIZE, did you leave it undefined so you have [-1] on line 6?

You allocate memory (values2) and it gets dropped on program exit only. Really not good practice.

If you use macros like COUNTOF(values) [by the way what is values??? did you forget the 2) , especially as I can guess that
COUNTOF is something like

// DONT EVER WRITE THIS:
#define COUNTOF(v) sizeof(v)/sizeof(int)

I am beginnng to think that we need a stick post that says :
(a) Use code tags
(b) format your code so it look nice
(c) if you want people to find bugs post a complete example
(d) check that your example REALLY compiles if you are looking for a runtime bug.
(e) Spend some time simplifing your example and adding appropiate comments.
(f) Copy and paste DIRECTLY.
(g) Check you post.

You are asking people to FREELY give up there time and help, show them some respect.

To all sorry for the rant, I have read a large number of posts recently that I think fail to fill that brief.

well, if you don't want to read my post, you don't have to.

Apparently, this is just part of my code and which you point out are all incorrect. Except that guess.

About the format post, i will pay more attention next time and show my respects.

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.