why am I getting garbage data?

I am getting good data up until my readout towards the end and I can't figure out why.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Hotel_Room
{
private:
	char room_number[4];
	char* guest;
	int rm_capacity,rm_occupant;
	double rm_rate;
public:
	Hotel_Room(char room[], int capacity, double rate,char* gues, int occu = 0);
	~Hotel_Room();
	const char* Get_Number()const;
	char* Get_Guest();
	int Get_Capacity();
	int Get_Status();
	double Get_Rate();
	int Change_Status(int);
	void Change_Rate(double);	
};

Hotel_Room::Hotel_Room (char room[],int capacity, double rate, char* gues,int occu)
{
	strcpy_s(room_number,room);	
	guest = new char[strlen(gues) +1];
	strcpy(guest,gues);
	rm_capacity = capacity;	
	rm_rate = rate;
	rm_occupant = occu;
}
Hotel_Room::~Hotel_Room()
{
	cout << endl << endl <<"Room " <<room_number<<" checked out\n."<<endl;
	delete [] guest;
	system("pause");
}

const char* Hotel_Room::Get_Number()const
{
	return &room_number[4];
}

int Hotel_Room::Get_Capacity()
{
	return rm_capacity;
}

int Hotel_Room::Get_Status()
{
	return rm_occupant;
}

double Hotel_Room::Get_Rate()
{
	return rm_rate;
}

int Hotel_Room::Change_Status(int stat)
{
	
	
	if(stat <= rm_capacity)
	{		
		rm_occupant = stat;
		return rm_occupant;
	}
	else
		return 0;
}

void Hotel_Room::Change_Rate(double rate)
{
	rm_rate = rate;
}

char* Hotel_Room::Get_Guest()
{
	return &*guest;
}
Hotel_Room* Make_Room();
void Display_Room(Hotel_Room&);

int main(int argc, char* argv[])
{
	cout << setprecision(2)
		<< setiosflags(ios::fixed)
		<< setiosflags(ios::showpoint);

	Hotel_Room* room_table[200];

	
	int	count = 0;
	int x;
	char response;

	cout << endl << "Do you want to set up a room? (Y or N) ";
	response = cin.get();
	cin.get();
	while (tolower(response) == 'y' && count < 200)
	{
		room_table[count] = Make_Room();
		++count;
		cout << endl << "Do you want to set up a room? (Y or N) ";
		response = cin.get();
		cin.get();
	}
	cout << endl << endl;
	for ( x = 0; x < count; ++x)
		Display_Room(*room_table[x]);
	system("pause");
	for ( x = 0; x < count; ++x)
		delete room_table[x];
	system("pause");
	return 0;
}

Hotel_Room* Make_Room()
{
	char room[4];
	char gues = '\0';
	int capacity;	
	double rate;

	Hotel_Room* rm_ptr;

	cout << endl << "Enter three digit room number: ";
	cin.getline(room, 4);
	cout << endl <<"Enter room max capacity: ";
	cin >> capacity;
	cout << endl << "Enter room rate: ";
	cin >> rate;
	cin.get();;
	rm_ptr = new Hotel_Room(room, capacity, rate, &gues);
	return rm_ptr;
}
void Display_Room(Hotel_Room& rm)
{
	cout << endl << endl;
	cout << "Room number: \t" << rm.Get_Number() << endl;
	cout << "Rented to: \t" << rm.Get_Guest() << endl;
	cout << "Occupants: \t" << rm.Get_Status() << endl;
	cout << "Rate: \t\t" << rm.Get_Rate() << endl;
	cout << "Max occupancy:\t" << rm.Get_Status() << endl;
}


Thank you for any help in advance.
1. strcpy is in <cstrings>. I included that, and used strcpy instead of strcpy_s

2. In Display_Room, the Max occupancy should be rm.Get_Capacity()

3. Get_number should be
1
2
3
4
const char* Hotel_Room::Get_Number()const
{
	return room_number;
}

For an array room_number (char[4] in your case), room_number is a pointer to room_number[0]

I assume that GetGuest should be the same return guest;

It would be easier for you to include strings instead of char* or char[ ]
Last edited on
One problem is with gues which you are setting by taking a reference to a local which immediately goes out of scope.
Thank you very much.
Topic archived. No new replies allowed.