Need Help getting Output to Display

I can answer the questions and get their output but I can't get beyond "Tracking Number" in the voidprintList(NoahsArk* Head) to display. I'm trying to get the Tracking Number, Animal and Boarding Charge to be displayed in column form.

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 <iomanip>
#include <iostream>
#include <string>
using namespace std;


struct NoahsArk
{
	int TrackingNumber;
	string Animal;
	double Charge;
	NoahsArk *link;
};

NoahsArk *HeadPointer = NULL;
void InsertItem(int, string, double,NoahsArk*);
void printList(NoahsArk*);

int main()
{
	int InitTrackingNumber = 0;
	string InitAnimal = "\0";
	double InitCharge = 0;
	NoahsArk *Pointer = NULL;
	char ContinuationFlag = 'Y';
	while (toupper(ContinuationFlag) == 'Y')
	{
		cout << "Please Enter the Tracking Number: " << endl;
		cin >> InitTrackingNumber;
		cout << "Please Enter the Animal: " << endl;
		cin >> InitAnimal;
		cout << "Please enter the Boarding Charge: " << endl;
		cin >> InitCharge;
		Pointer = new NoahsArk;
		
		InsertItem(InitTrackingNumber, InitAnimal, InitCharge, Pointer);
		OriginalPointer = Pointer;
		cout << "Would you like to enter more information?" << endl;
		cout << "Enter 'Y' or 'N'" << endl;
		cin >> ContinuationFlag;
	}
	printList(OriginalPointer);
	return 0;
}
void InsertItem(int InitTrackingNumber, string InitAnimal, double InitCharge,NoahsArk* Pointer)
{
	Pointer->TrackingNumber = InitTrackingNumber;
	Pointer->Animal = InitAnimal;
	Pointer->Charge = InitCharge;
	Pointer->link = OriginalPointer;
}
void printList(NoahsArk* Head)
//{
	while (Head != NULL)
	{
		char Separator[50]"_________________________________________________";
			cout << endl << Separator << endl << endl;
			cout << setw(36) << setfill(' ') << "Noah's Ark" << endl;

		cout << setw(15) << setfill(' ') << "Tracking Number" << endl << endl << endl;
		cin >> TrackingNumber >> endl;
			Head->TrackingNumber << " "<<endl;
		cout << setw(15) << setfill(' ') << "Type of Animal" << endl
			Head->Animal << " " << endl;
			cout<<setw(15) << setfill(' ') << "Daily Boarding Charge"
			Head->Charge << " "<<
		Head = Head->link;
	}
}
Last edited on
1
2
3
4
5
6
7
void InsertItem(int InitTrackingNumber, string InitAnimal, double InitCharge,NoahsArk* CurrentRecordPointer)
{
	Pointer->TrackingNumber = InitTrackingNumber;
	Pointer->Animal = InitAnimal;
	Pointer->Charge = InitCharge;
	Pointer->link = OriginalPointer;
}


I think you need to use CurrentRecordPointer instead of Pointer.

BTW Is it necessary to use a pointer ? If yes please make sure to delete it at th end, otherwise you will get a memory leak.
Yes, we have to use pointers. What do you mean delete it at the end?
When you allocate memory with new you have to use delete to delete(release) the memory.

At the end of main call delete Pointer;

http://www.cplusplus.com/reference/new/operator%20delete/
Thomas1965 wrote:
At the end of main call delete Pointer;

It's a little more complicated than that. The OP is building a linked list. The OP needs to walk the list starting with HeadPointer and delete each item in the list.

@OP - I get compile errors in your program.
Line 37,42,60: OriginalPointer is undefined.

Line 53: Opening { is commented out.

Line 56: = missing.

Line 61: Head-> missing. You can't use endl on cin. Why are you doing a cin in a print function?

Line 62: Missing cout <<

Line 64, 66: Missing <<

Line 66: Extraneous <<. Missing ;


Last edited on
Should I get rid of the "Head" inside of the void PrintList? And just set up my columns?
Should I get rid of the "Head" inside of the void PrintList?


Not sure if you're referring to the variable "Head" or the "print heading".

If you mean the variable "Head", then no. Passing in Head is perfectly reasonable. It removes the dependency on a global by moving the dependency to the caller.

If you mean the print heading, again, I would say no. PrintList is intended to print the entire list. I would however be inclined to move lines 56-58 to in front of the while loop.
I've edited those lines and I believe this may be the right coding now. I tested it with and without the delete pointer; and it didn't seem to make a difference, so should I still leave it?


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
70
71
72
73
74
75
76
77
78
79
80
81
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;


struct NoahsArk
{
	int TrackingNumber;
	string Animal;
	int Charge;
	NoahsArk *link;
};

NoahsArk *HeadPointer = NULL;
void Info(int, string, int, NoahsArk*);
void printList(NoahsArk*);


int main()
{
	int InitTrackingNumber = 0;
	string InitAnimal = "\0";
	int InitCharge = 0;
	NoahsArk *Pointer = NULL;
	char ContinuationFlag = 'Y';
	while (toupper(ContinuationFlag) == 'Y')
	{
		cout << "Please Enter the Tracking Number: " << endl;
		cin >> InitTrackingNumber;
		cout << "Please Enter the Animal: " << endl;
		cin >> InitAnimal;
		cout << "Please enter the Boarding Charge: " << endl;
		cin >> InitCharge;
		Pointer = new NoahsArk;
	


		Info(InitTrackingNumber, InitAnimal, InitCharge, Pointer);
		HeadPointer = Pointer;
		cout << "Would you like to enter more information?" << endl;
		cout << "Enter 'Y' or 'N'" << endl;
		cin >> ContinuationFlag;
	}
	printList(HeadPointer);
	return 0;
delete pointer;

}


void Info(int InitTrackingNumber, string InitAnimal, int InitCharge, NoahsArk* Pointer)
{
	Pointer->TrackingNumber = InitTrackingNumber;
	Pointer->Animal = InitAnimal;
	Pointer->Charge = InitCharge;
	Pointer->link = HeadPointer;
}
void printList(NoahsArk* Head)
{
	cout << left << setw(50) << "____________________________________________________________________________" << endl << endl;
	cout << right << setw(40) << setfill(' ') << "Noaks Ark" << endl << endl;
	cout << left << setw(50) << "____________________________________________________________________________" << endl << endl;
	cout << left << setw(25) << setfill(' ') << "Tracking Number" <<
		left << setw(25) << setfill(' ') << "Type of Animal" <<
		left << setw(25) << setfill(' ') << "Boarding Charge" << endl << endl;
	while (Head != NULL)
	{

		

		
		cout<<left<<setw(25)<<setfill(' ')<<Head->TrackingNumber<<
			left<<setw(25)<<setfill(' ')<<Head->Animal << 
			left<<setw(25)<<setfill(' ')<<Head->Charge<< endl;
		Head = Head->link;



	}
}
Last edited on
Looks reasonable except for not walking the list and deleting the dynamic memory allocated at the end of the program as mentioned above.

Freeing dynamic memory is a good habit to get into. Memory leaks should be avoided.
Last edited on
What do you mean by walking the list?And I thought the delete pointer; in main at the end would delete the dynamic memory?
 
  delete [] HeadPointer;

Deletes only the first entry in the list.

To delete all the entries in the list, you have to "walk" the list (as you do in printList). e.g.
1
2
3
4
5
6
7
8
 
  NoahsArk * curr = HeadPointer;  // pointer to current node
  NoahsArk * next;  // We need to save the next pointer before we delete a node
  while (curr) 
  {  next = HeadPointer->link;
      delete [] curr;
      curr = next;
  }
Last edited on
I was never taught about curr so I'm not sure if we're allowed to use that in our code. Also, for the output, I couldn't my charging board output to show 2 decimal points. I typed in 30.50 and the output showed just 30
Also what changes would need to be made to the code if I wanted the type of animal to be a character array and the boarding charge to be a double?
I was never taught about curr

curr is just a pointer. No different than Pointer at line 25.

I couldn't my charging board output to show 2 decimal points.

Line 76: Use setprecision(2).
http://www.cplusplus.com/reference/iomanip/setprecision/

what changes would need to be made to the code if I wanted the type of animal to be a character array

Why? std::strings are much safer to work with.

the boarding charge to be a double?

Change line 11.





The animal type has to be a character array basically. Also I put the fixed setprecision line in but it still isn't showing up, I put it in main on line 33
The animal type has to be a character array

What does that mean? Does it mean you can not use std::string?

I put the fixed setprecision line in but it still isn't showing up, I put it in main on line 33

That's not where you displaying the boarding charge. The setprecision() call needs to be on line 75.
Topic archived. No new replies allowed.