How to store class info into an Array

So I have an array of my own class, "Restaurant," but I'm having a hard time figuring out how to store the info entered by the user into the array.

In total, I have two classes. I will show you all I have, it's a lot but I know it will be easier to understand.

My problem is in the AddRestaurant(), since that is where I should ask the user for all the information and store it in the array.

Also consider that I have to use the FTime class as well to handle the time.

Any help is greatly appreciated.

Thank you.

Basically I want to do something like this:

info[count].SetRating();

which will put the rating part of the class Restaurant into the array[count]

Also, my program doesn't compile because of the AddRestaurant and PrintAll functions since what I am showing is what I want to do, not what works.

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//main.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "Restaurant.h"
#include "FTime.h"

int ReadMenuChoice();
void DisplayMenu();
void ProcessChoice(int choice, Restaurant info[], int& count);
void AddRestaurant(Restaurant info[], int& count);
void WriteLine(char ch, int size);
void PrintAll(const Restaurant info[], int count);

void ReadRating(Restaurant& r);
void ReadName(istream& in, Restaurant& n);
void ReadTime(FTime& t, string prompt);

using namespace std;

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

template <class T>
bool Read(T& val);

int main()
{
	Restaurant info[MAX];
	int count = 0, choice;
    
	while( EXIT != (choice = ReadMenuChoice() ) )
	{
		ProcessChoice(choice, info, count);
	}
    
	return 0;
}

int ReadMenuChoice()
{
	int choice;
    bool valid;
    
	DisplayMenu();
    
    do
    {
        cout << "Enter 1-3 : ";
        valid = Read(choice);
        if(!valid)
		{
			cout << "Invalid Format" << endl;
		}
		else if(valid)
        {
            if (choice < ADD || choice > EXIT)
            {
                cout << "Invalid Entry! Try again." << endl;
                valid = false;
            }
            else
            {
                valid = true;
            }
        }
    }while(!valid);
    
	return choice;
}

void DisplayMenu()
{
	WriteLine('=', 60);
	cout << "MENU" << endl;
	WriteLine('-', 60);
	cout << "1. Add Restaurant" << endl;
	cout << "2. Print All " << endl;
	cout << "3. Exit" << endl;
	WriteLine('-', 60);
}

void ProcessChoice(int choice, Restaurant info[], int& count)
{
    FTime openHour, closeHour;
	switch(choice)
	{
        case ADD:
			AddRestaurant(info, count);
			break;
		case PRINT:
			PrintAll(info, count);
			break;
	}
}

void PrintAll(const Restaurant info[], int count)
{
	cout << endl;
	cout << "RESTAURANTS:" << endl;
	WriteLine('=', 60);
	cout <<  left << setw(20) << "Name:" << setw(15) << "Rating" << setw(15) << "Open" << setw(15)
    << "Close" << endl;

	WriteLine('-', 60);
    
    for(int i = 0; i < count; i++)
        cout <<  left << setw(20) << info[i].GetName() << setw(15) << info[i].GetRating() << setw(15)
             << info[i].GetTime() << setw(15)
             << info[i].GetTime() << endl;
    
	WriteLine('-', 60);
}

void AddRestaurant(Restaurant info[], int& count)
{
    FTime open, close;
    Restaurant n , r;
	int rating;
	if(count < MAX)
	{
		ReadName(cin, n);
		ReadRating(r);
                ReadTime(open, "Opening Hour:");
		ReadTime(close, "Closing Hour:");
		count++;
	}
	else
		cout << "FULL" << endl;
}

void ReadName(istream& in, Restaurant& n)
{
    bool valid;
    
    do
    {
    try
    {
        cout << "Name: ";
        getline(cin, n);
        valid = true;
    }
        catch(runtime_error e)
        {
            cout << e.what() << endl;
            valid = false;
            cin.clear();
            cin.ignore(1000, '\n');
        }
	}while(!valid);
}

void ReadRating(Restaurant& r)
{
    bool valid;
    
    do
    {
        try
        {
            cout << "Rating: ";
            cin >> r;
            valid = true;
        }
        catch(runtime_error e)
        {
            cout << e.what() << endl;
            valid = false;
            cin.clear();
            cin.ignore(1000, '\n');
        }

	}while(!valid);
}

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

void ReadTime(FTime& t, string prompt)
{
	bool valid;
    
	do
	{
        try
        {
            cout << prompt << " (HH:MM): ";
            cin >> t;
            valid = true;
        }
        
        catch(runtime_error e)
        {
            cout << e.what() << endl;
            valid = false;
            cin.clear();
            cin.ignore(1000, '\n');
        }
	}while(!valid);
    
}

template <class T>
bool Read(T& val)
{
    bool valid;
    
    cin >> val;
    if( cin.fail() )
    {
        cin.clear();
        valid = false;
    }
    else
    {
        valid = true;
    }
    cin.ignore(1000, '\n');
    
    return valid;
}


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
//Restaurant.h

#pragma once

#include <string>
#include "FTime.h"
using namespace std;

class Restaurant
{
    friend istream& operator>>(istream& in, Restaurant& obj);
    friend istream& getline(istream& in, Restaurant& obj);
	friend ostream& operator<<(ostream& out, const Restaurant& obj);
    
private:
	string name;
	int rating;
    FTime open, close;
    
public:
	string GetName() const;
	int GetRating() const;
	void SetName(string val);
	void SetRating(int val);
    void SetInfo(string n, int r);
    
public:
	Restaurant();
	Restaurant(string n, int r);
	~Restaurant();
};


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
//Restaurant.cpp

#include "Restaurant.h"
#include "FTime.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdexcept>
using namespace std;

Restaurant::Restaurant()
{
	SetInfo("", 0);
}

Restaurant::Restaurant(string n, int r)
{
	SetInfo(n, r);
}

Restaurant::~Restaurant()
{
    
}

string Restaurant::GetName() const
{
	return name;
}

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

void Restaurant::SetName(string val)
{
    name = (val == "") ? "" : val;
}

void Restaurant::SetRating(int val)
{
	if(val < 0)
	{
		rating = 0;
	}
	else if (val > 5)
	{
		rating = 5;
	}
	else
	{
		rating = val;
	}
}

void Restaurant::SetInfo(string n, int r)
{
	SetName(n);
	SetRating(r);
}

istream& getline(istream& in, Restaurant& obj)
{
    in >> obj.name;
    if(obj.name == "")
        throw runtime_error("Invalid Name");
	cin.ignore(1000, '\n');
    return in;
}

istream& operator>>(istream& in, Restaurant& obj)
{
	in >> obj.rating;

	if(obj.rating < 0 || obj.rating > 5)
		throw runtime_error("Invalid Rating");

	if(cin.fail())
		throw runtime_error("Invalid Format");

	return in;

}
Last edited on
This is the second class:

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
//FTime.h
#pragma once

#include <string>
using namespace std;

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

private:
	int hour;
	int min;
	int sec;

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

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


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
//FTime.cpp

#include "FTime.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdexcept>
using namespace std;

FTime::FTime()
{
	SetTime(0,0);
}

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

FTime::~FTime()
{

}

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

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

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

	ss << setfill('0')
	   << setw(2) << hour << ":"
	   << setw(2) << min  << ":"
	   << setw(2) << sec;

	return ss.str();
}

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

	int h = hour % 12;

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

	return ss.str();
}

void FTime::SetHour(int val)
{
	hour = (val < 0 || val > 23) ? 0 : val; // only with if-else
}

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

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

istream& operator>>(istream& in, FTime& obj)
{
	in >> obj.hour;
	if(obj.hour < 0 || obj.hour > 23)
		throw runtime_error("Invalid Hour");

	in.ignore();
	in >> obj.min;

	if(obj.min < 0 || obj.min > 59)
		throw runtime_error("Invalid Min");

	if(cin.fail())
		throw runtime_error("Invalid Format");
	return in;
}

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

bool FTime::operator==(const FTime& obj) const
{
	if((hour == obj.hour) && (min == obj.min) && (sec == obj.sec))
		return true;
	else
		return false;
}

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

	return *this;
}

bool FTime::operator>(const FTime obj) const
{
	if(hour > obj.hour)
	{
		return true;
	}
	else if( hour < obj.hour)
	{
		return false;
	}
	else 
	{
		if(min > obj.min)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
Basically I want to do something like this:

info[count].SetRating();

Looks like you've already figured it out! That's exactly the correct syntax to call a method on an object in an array.

However, your big output statement at line 109 of your first code block doesn't make a lot of sense. I suspect what you really meant to do was call GetRating() etc, not Set* methods.
@MikeyBoy

Yes, I realized that after and corrected it.

Now, if I do info[count].SetRating(), it takes in an int, so what I can't figure out is how to read it, and then SetRating(int r) to put it in the array?

Also for the PrintAll function, line 110, GetTime() is in the FTime class, so its complaining that there is no member GetTime in restaurant. How do I go about that?
Now, if I do info[count].SetRating(), it takes in an int, so what I can't figure out is how to read it, and then SetRating(int r) to put it in the array?

Well, I assume that's what you intended ReadRating() to do. Currently, it takes in a Restaurant object and attempts to assign to it whatever the user typed in. Clearly, that makes no sense, because a Restaurant is an object, not a variable that can be set to a value.

You should probably be storing, in a local variable, the value that the user typed in, and then setting the rating of the restaurant to that value .
Last edited on
Topic archived. No new replies allowed.