Array printing garbage!and I have no idea why.

HeY,I am fairly new to programming so i am in need of help. I need help figuring out why the elements from my dynamic array are not printing out correctly. So far when I run my program, the output is garbage and I don't know how to fix this(I've been trying to fix this problem since 11:00 pm yesterday) Can some one please let me know what i am doing wrong and assist me in fixing this. I have been staring at my code for hours and have tried many things to fix this but no success.

Here is my code:

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
#include "stack.h"
#include "Student.h"


void stack::add(Student st )  // function to add student record
{
	if(size==0)              // check if size is zero
	{
		list=new Student[size+1];      // create space in memory add one to size
		list[0]=st;   //  place student record into list
	}
	else
	{
		Student* temp=new Student[size];   // create a temp array
		cout<< size;
		for(int i=0; i<size; i++)
		{
			temp[i]=list[i];         // copy list into array
		}
		delete [] list;     // delete list
		

		list=new Student[size+1];     // create array  list

		
		for(int i=0; i<size; i++)      // for loop to  copy temp in to list
		{
			list[i]=temp[i]; // copy temp in to list
			
		 
		}
		
		
		list[size]= st;              
		
		delete [] temp; // delete temp array
		/*temp=NULL; */    // to Be sure the deallocated memory isn't used.
        
	}
	
	size++;  // increment size
}



void stack::Delete()       // function to delete
{
	size--; // decrement size

	if(size==0)
	{
		delete [] list;      // delete array.
	}

}


void stack::show()      // function to show
{
	
	for(int i=0; i<size; i++)  // for loop to print
	{
		
		 list[i].print();
	}
		
}





Here is the header file (1) for the code above:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include "Student.h"

class stack // declared class Stack
{
private:           // create private member variables
	int size;      // intilized size
	Student* list;  // create poiner list of type student
public:
	stack()       // constructor stack
	{ 
	    size=0;  // set size to zero
	    list=NULL;    // set list equal to null
	}
	void add(Student st);   //  function add
	void Delete();         // function delete
	void show();          // function to show
}; 





the other header file (2):


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Student   // create class student
{
public:        
	string name;       // declared public member variables
	int id;           // variable to hold id
	string dob;       // variable to hole date of birth
	Student(){}      // default constructor
	Student(string name, int id, string dob); // constructor  with 3 arguments
	void print();       // function to print
};





My Driver file(if its needed):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include"Student.h"
#include"stack.h"

int main()
{
	
	Student st1("Bobby", 110, "11/12/1990"); // i am trying to print these 3
	Student st2("Justin", 100, "12/10/1989");  
	Student st3("andi", 113, "09/07/1994");
	stack stack1;
	stack1.add(st1);
	stack1.add(st2);
	stack1.add(st3);
	stack1.show();
	cout << "----------\n";
	stack1.Delete();
	stack1.add(st1);
	stack1.show();
	system("pause");
	return 0;
}




Lastly the file that has the implementation of the print function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Student.h"


Student::Student(string name, int id, string dob)
	{
		name="";
		id=0;
		dob="";
	}
void Student::print()  // function to print out name and dob, and id
{
	
	cout << name <<id  <<  dob << endl;
	
	 

}





It would mean a lot if someone can help me correct my problem
Please help me and Thank you in Advance.

Last edited on
Your student constructor isn't actually initializing the student members. It should be:
1
2
3
4
5
6
Student::Student(string _name, int _id, string _dob)
	{
		name = _name
		id=_id;
		dob= _dob;
	}

Better still, you can cause the members to be constructed directly with the proper values like this:
1
2
3
4
5
Student::Student(string _name, int _id, string _dob) :
    name(_name),
    id(_id),
    dob(_dob)
{;}

See if that fixes it. Then if you want, you could make your stack::add() more efficient by making just one new array:
- create array with size+1 elements.
- copy size elements from list to new array
- copy the element that you're adding to the end of the new array.
- delete old list array
- set list to the new array.

Wow!! that fixed it. Thank you, Thank you, Thank you very much!!!!
Topic archived. No new replies allowed.