Trying to add template class to maintain list

Sorry for the lengthy post, I just figured it would be best if you see all the work I have done so far.
I wrote this menu-driven program that maintains a list of restaurants.

The program runs fine as it is right now, but my problem is I need to create a new array template class to maintain the list of restaurants, and when a new restaurant is added it must be created dynamically.

I'm having a hard time figuring out what exactly I need to do for this. Templates confuse me allot and I've read all the sections on templates here and in my book, but i'm still lost. The dynamic memory part is throwing me off as well. Any help would really be appreciated.


main.pp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>
#include <iomanip>
#include "Restaurant.h"
using namespace std;

const int MAX = 100;
enum choice{ADD = 1, PRINT, EXIT};

int ReadMenuChoice();
void ProcessChoice(int choice, Restaurant R[], int&count);
void ReadName(Restaurant R[]);
void ReadRating(Restaurant R[], int count);
void ReadHour(Restaurant R[], int count, string label1, string label2);
void displayMenu();
void displayInputMenu(Restaurant R[], int& count);
void displayOutputMenu( Restaurant R[], int count);
int crashPrevention();
void writeLine(char line, int size);

//function name: main()
//purpose:sets name, rating, opening and closing time for a restaurant
//input: none
//output:none
//in param:none
//out param:none
//return: 0
//function called: ReadMenuChoice();, ProcessChoice()
int main()
{
	Restaurant R[MAX];
	int choice;
	int count = 0;

	choice = ReadMenuChoice();

	while(EXIT != (choice) )
	{
		ProcessChoice(choice, R, count);
		choice = ReadMenuChoice();
	}
	return 0;
}

int ReadMenuChoice()
{
	int choice;
	bool valid;

	do
	{
		displayMenu();
		cin >> choice;
		if(choice < ADD || choice > EXIT)
		{
			cout << "Invalid input" << endl;
			valid = false;
		}
		else
			valid = true;
	}while(!valid);

	cin.ignore(1000, '\n');
	return choice;
}

void ProcessChoice(int choice, Restaurant R[], int&count)
{
	switch(choice)
	{
	case ADD:
		displayInputMenu(R, count);
		break;
	case PRINT:
		displayOutputMenu(R, count);
		break;
	}
}

void ReadName(Restaurant R[], int count, string label)
{
	string title;
	bool valid;

	do
	{
		cout << label;
		getline(cin, title);

	if(title == "")
	{
		cout << "Invaliid input" << endl;
		valid = false;
	}
	else
	{
		valid = true;
	}
	}while(!valid);

	R[count].setTitle(title);
}

void ReadRating(Restaurant R[], int count, string label)
{
	bool valid;
	int rating;
	do
	{
	cout << label;
	cin>>rating;
	if(rating < 1 || rating > 5)
	{
		cout << "invalid" << endl;
		valid = false;
	}
	else 
		valid = true;
	}while(!valid);
	R[count].setRating(rating);
}

void ReadHour(Restaurant R[], int count, string label1, string label2)
{
	cout << label1<<endl;
	R[count].setOpeningHour();
	cout << label2<<endl;
	R[count].setClosingHour();
}

void displayMenu()
{
	cout << "MENU" << endl;
	cout << "1. Add Movie" << endl;
	cout << "2. Print All " << endl;
	cout << "3. Exit" << endl;
}

void displayInputMenu(Restaurant R[], int& count)
{
	ReadName( R, count,  "Name: ");
	ReadRating(R, count, "Rating: ");
	ReadHour(R, count, "Opening Hour:", "Closing Hour:");
	count++;
}

void displayOutputMenu(Restaurant R[], int count)
{
	cout << "RESTAURANTS: " << endl;

	for(int n = 0; n<count; n++)
	{
		writeLine('=', 50);
		cout << "Name" << setw(20) << "Rating" << setw(10) 
			 << "Open" << setw(10) << "Close" << endl;
		writeLine('-', 50);
		cout << R[n].getTitle();
		cout << setw(20);
		cout << R[n].getRating();
		cout << setw(10);
		cout <<  R[n].getOpeningHour() << setw(10) << R[n].getClosingHour() << endl;
	}
	writeLine('-', 50);
}

int crashPrevention()
{
	bool valid;
	if(cin.fail())
	{
		cout << "Invaliid input" << endl;
		valid = false;
		cin.clear();
	}
	else
	{
		valid = true;
	}
	cin.ignore(1000, '\n');
	return valid;
}

void writeLine(char line, int size)
{
	cout << setfill(line) << setw(size) << " " << endl;
	cout << setfill(' ') << endl;
}


Restaurant.h
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
#pragma once
#include <iostream>
#include <string>
#include "FTime.h"

using namespace std;

class Restaurant
{
private:
	string title;
	int rating;
	FTime openingHour;
	FTime closingHour;

public:
	int getRating() const;
	void setRating(int r);
	string getTitle() const;
	void setTitle(string t);
	FTime getOpeningHour() const;
	void setOpeningHour();
	FTime getClosingHour() const;
	void setClosingHour();
	void Restaurant::GetHours(FTime& open, FTime& close)const;

public:
	Restaurant();
	~Restaurant();
};


Restaurant.cpp
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
#include "Restaurant.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

Restaurant::Restaurant()
{
	rating = 0;
}

Restaurant::~Restaurant()
{
}

int Restaurant::getRating() const
{
	return rating;
}

void Restaurant::setRating(int r) 
{
	rating = r;
}

string Restaurant::getTitle() const
{
	return title;
}

void Restaurant::setTitle(string t)
{
	title = t;
}

FTime Restaurant::getOpeningHour() const
{
	return openingHour;
}

void Restaurant::setOpeningHour()
{
	int hour, min;
	bool valid;

	do
	{
		cout << "(HH:MM) : ";
		cin >> hour;
		cin.ignore();
		cin >> min;
		cin.ignore();

		if(cin.fail())
		{
			valid = false;
			cout << "HH:MM only" << endl;
			cin.clear();
		}
		else if(hour < 0 || hour > 23 ||
				min  < 0 || min  > 59 )
		{
			valid = false;
			cout << "Invalid time" << endl;
		}
		else
		{
			valid = true;
		}

	}while(!valid);
}

FTime Restaurant::getClosingHour() const
{
	return closingHour;
}

void Restaurant::setClosingHour()
{
	int hour, min;
	bool valid;

	do
	{
		cout <<  "(HH:MM) : ";
		cin >> hour;
		cin.ignore();
		cin >> min;
		cin.ignore();

		if(cin.fail())
		{
			valid = false;
			cout << "HH:MM only" << endl;
			cin.clear();
		}
		else if(hour < 0 || hour > 23 ||
				min  < 0 || min  > 59 )
		{
			valid = false;
			cout << "Invalid time" << endl;
		}
		else
		{
			valid = true;
		}
	}while(!valid);
}

void Restaurant::GetHours(FTime& open, FTime& close)const
{
	open = openingHour;
	close = closingHour;
}


FTime.h
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
#pragma once
#include <string>
using namespace std;

//class FTime
class FTime
{
	friend ostream& operator<<(ostream& out, const FTime& obj);
	friend istream& operator>>(istream& in, FTime& obj);

public:
	const FTime& operator=(const FTime& obj);

public:
	bool operator==(const FTime& obj) const;

private:
	int hour;
	int min;	

public:
	int GetHour() const;
	int GetMin() const;
	void GetTime(int& h, int& m) const;
	string GetStandard() const;
	string GetMilitary() const;
	void SetHour(int val);
	void SetMin(int val);
	void SetTime(int h, int m);

public:
	FTime(int h = 0, int m = 0);
	~FTime();
};


FTime.cpp
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
#include "FTime.h"
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

FTime::FTime(int h, int m)
{
	SetHour(h);
	SetMin(m);
}

FTime::~FTime()
{
	
}

int FTime::GetHour() const
{
	return hour;
}

int FTime::GetMin() const
{
	return min;
}

string FTime::GetStandard() const
{
	stringstream ss;

	int h = hour % 12;

	ss << setfill('0') 
	   << setw(2) << (h == 0 ? 12 : h) << ":"
	   << setw(2) << min 
	   <<  ((hour >= 12) ? " PM" : " AM");

	return ss.str();
}

string FTime::GetMilitary() const
{
	stringstream ss;
	ss << setfill('0') 
	   << setw(2) << hour << ":"
	   << setw(2) << min;

	return ss.str();
}

void FTime::GetTime(int& h, int& m) const
{
	h = hour;
	m = min;
}

void FTime::SetHour(int val)
{
	hour = ((val > 23 || val < 0) ? 0 : val);
}

void FTime::SetMin(int val)
{
	min = ((val > 59 || val < 0) ? 0 : val);
}

void FTime::SetTime(int h, int m)
{
	SetHour(h);
	SetMin(m);
}

ostream& operator<<(ostream& out, const FTime& obj)
{
	out << obj.GetStandard();
	return out;
}

istream& operator>>(istream& in, FTime& obj)
{
	int hours;
	in >> hours;
	obj.SetHour(hours);
	int min;
	cin >> min;
	obj.SetMin(min);

	return in;
}

const FTime& FTime::operator=(const FTime& obj)
{
	hour = obj.hour;
	min = obj.min;

	return *this;
}

bool FTime::operator==(const FTime& obj) const
{
	if(hour == obj.hour && min == obj.min)
		return true;
	else 
		return false;
}
The dynamic memory is what he was going over in class. Its supposed to be in your .h file.
p.s. your crash prevention is wrong.
Last edited on
Which .h file? He was going so fast I couldn't really grasp all of it.
Also, what part of my crash prevention is wrong?
Last edited on
i think restaurant.


make into template
change int to bool

put in
 
cin >> val;

before if statements

and in your parameters put in T& val
Topic archived. No new replies allowed.