Char array and double variables

I'm trying to make the type of animal a char array and make the charge a double, how do I about this? Everytime I run my code, it always loops, continuously once I answer "Enter type of animal" and I have to exit out of program in order for it to stop. I just need the output to be in three columns: Tracking Number, Type of Animal and Boarding Charge.

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


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

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


int main()
{
	int InitTrackingNumber = 0;
	char 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;

		cout << fixed << showpoint << setprecision(2);

		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 HeadPointer;


}


void Info(int InitTrackingNumber, char InitAnimal, double 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;



	}
}
This is what I get:

Please Enter the Tracking Number:
1
Please Enter the Animal:
a
Please enter the Boarding Charge:
11
Would you like to enter more information?
Enter 'Y' or 'N'
N
____________________________________________________________________________

Noaks Ark

____________________________________________________________________________

Tracking Number Type of Animal Boarding Charge

1 a 11.00

Please Enter the Tracking Number:
1
Please Enter the Animal:
a
Please enter the Boarding Charge:
11
Would you like to enter more information?
Enter 'Y' or 'N'
Y
Please Enter the Tracking Number:
2
Please Enter the Animal:
b
Please enter the Boarding Charge:
22
Would you like to enter more information?
Enter 'Y' or 'N'
N
____________________________________________________________________________

Noaks Ark

____________________________________________________________________________

Tracking Number Type of Animal Boarding Charge

2 b 22.00
1 a 11.00




The formatting also looks right on the screen
Line 10,23,56: Animal and InitAnimal allow only a single character. However your formatting at line 78 appears to expect animal to be up to 25 characters wide. If you want more than one character, then you need to declare a character array:
 
  char Animal[26];  //  Allow space for trailing null 


As your code is, if you enter more than a single character at line 32, the extra characters will remain in the input buffer and cause the cin at line 34 to fail.

Line 59: You can't assign character arrays that way. I asked in your other thread why you can't use std::string. std::string is easier and safer to use than character arrays. You still have an include for <string> at line 3.


Last edited on
Topic archived. No new replies allowed.