Error in negative numbers(Help plez)

Hi
I have this problem when i enter a negative numbers
and when i enter a positive values its okay ?

can anyone help.

This is the problem : http://postimage.org/image/s6g8f6jyj/


And this is the code(In three files)
---------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
using namespace std;

class ArrayList
{
 private:
	 int size;					    //private Identifier

 public:
	//Functions prototype
	ArrayList ();					//Default Constructor
	ArrayList (int n);				//Constructor with Parameters
	~ArrayList ();					//Destructor
	
	int *list;
	int GetSize();
	int PositiveCount();

	void SetSize (int);
	void ReadArray();
	void PrintArray();
	void CopyPositiveValues(ArrayList & obj1);
};


------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include"ClassF.h"


//Functions Implementation
ArrayList::ArrayList ()
{
	size=5;
	int *list=new int[5];
}

ArrayList::ArrayList (int n)
{
	size=n;
	int *list=new int[n];
}

ArrayList::~ArrayList()
{
	delete list;
}

void ArrayList::SetSize(int nu)
{
	size=nu;
}

int ArrayList::GetSize()
{
	return size;
}

void ArrayList::ReadArray()
{
	cout<<"Please Enter 6 values to fill the list: ";

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

void ArrayList::PrintArray()
{
	cout<<"The contents of the first list are:	";

	for(int i=0;i<=5;i++)
		cout<<list[i]<<" ";
	cout<<endl;
}

int ArrayList::PositiveCount()
{
	int count=0;

	for(int i=0;i<=5;i++)
	{
		if(list[i]>=0)
		count++;
	}
	return count;
}

void ArrayList::CopyPositiveValues(ArrayList & obj1)
{
	for(int i=0;i<=size;i++)
	{
		if(list[i]>=0)
			obj1.list[i]=list[i];
	}
	
}

---------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include"ClassF.h"


void main()
{
	ArrayList list1;
	int PC;
	
	list1.list=new int [6];
	list1.ReadArray();
	list1.PrintArray();
	
	PC=list1.PositiveCount();

	cout<<"The number of positive values inside the first list= "<<PC<<"\n"<<endl;


	ArrayList list2;
	list2.list=new int [PC];

	list1.CopyPositiveValues(list2);

	cout<<"The size of the second list is: "<<PC<<"\nAnd the contents of the second list are: ";

	for(int i=0; i<PC; i++)
	{
		cout<<list2.list[i]<<" ";
	}

	cout<<"\n"<<endl;
}

------------------------------------------

Looking Forward
Last edited on
Well, first of all you're creating a memory leak on line 9 of your main file. You are allocating memory with list1.list, but list1.list already points to memory you allocated in the ArrayList constructor, so you're losing the pointer. It also appears that your size member is going to be incorrect because when you reallocate memory with list1.list, you're setting the size to 6, but the class expects it to be 5. This is an excellent example of why encapsulation is important, and should be used. Problems like this can be avoided by making data members like list, private, which I recommend you do.

As for your problem involving the strange output, it's because whenever an negative number in the list of whatever object is calling CopyPositiveValues is found, it skips it and increments i. This means that obj1.list[i] is going to be left uninitialized, hence the junk value that you end up with. To fix this, just have a separate variable for whatever the next uninitialized element in obj1.list is, and only increment it when you add a value to the list, like this:
1
2
3
4
5
6
7
        int obj1_index = 0;
	for(int i = 0; i <= size; ++i) {
		if(list[i]>=0) {
			obj1.list[obj1_index] = list[i];
                        ++obj1_index;
                }
	}

Last edited on
ascii pointed it out correctly. Also, please use delete[] list instead of delete list in your destructor.
Thank you very much ascii & incognito for help
It's work now ^_^
and thanx again for answering in short time .

I love this website.
Topic archived. No new replies allowed.