help with some functions

Hello,

I am writing some function that involve using structs and arrays

the first question is

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
  void show(const Address address[], unsigned elements, unsigned desiredZip)
{
	for (unsigned i = 0; i < elements; i++)
	{
		if (address[i].zip == desiredZip)
		{
			show(address[i]);
			cout << "-----------------" << endl;
		}
	}
}

void show(const Address address[], unsigned addressElements, const unsigned
 desiredZip[], unsigned desiredZipElements)
{
	for (unsigned x = 0; x < addressElements; x++)
	{
		for (unsigned y = 0; y < desiredZipElements; y++)
		{
			if (address[x].zip == desiredZip[y])
			{
				show(address[x]);
				cout << "-----------------" << endl;
			}
		}
	}


and what is in the main function

1
2
3
const unsigned desiredZip[] = { 91371, 91375 };
show(loc,10,91371);
show(loc, 10, desiredZip, 10);


I have problem when I increase the elements to more than 10 the program crashes

The second thing,

void show( const Time & time );

I wrote this function like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
	switch (time.day)
	{
	case 0: cout << "Sunday" << " "; break;
	case 1: cout << "Monday" << " "; break;
	case 2: cout << "Tuesday" << " "; break;
	case 3: cout << "Wednesday" << " "; break;
	case 4: cout << "Thursday" << " "; break;
	case 5: cout << "Friday" << " "; break;
	case 6: cout << "Saturday" << " "; break;
	};

	cout << time.hour << ":";
	if (time.minute < 10) cout << "0" << time.minute;
		else cout << time.minute;
		
	if (time.pm == true) cout << " " << "PM" << endl;
		else if (time.pm == false) cout << " " << "AM" << endl;


there's a recommended way which is globally declare a constant array of strings with the names of the days. I don’t want you to
//const string days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; days;
I tried this but didn't work
Last edited on
Program crashes cause you attempt to access "classified" "intel" in line 20 of your show function.

const unsigned desiredZip[] = { 91371, 91375 } contains only 2 elements.
In fuction show(loc,10,desiredZip,10) , when y is 2, program tries to access desiredZip[2] which is inaccessible. Hence program crashes.

Just change show(loc, 10, desiredZip, 10); to show(loc, 10, desiredZip, 2); .

I dont know what your second question is.
I assume for your second question, you mean to print from an array? Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
const std::string days[7] = { "Sunday", "Monday", /*...*/, "Saturday" };

// ...

// make sure time.day is in a valid range. You can do it any way you like,
// but this is probably the simplest (but most overkill).
assert(time.day < 7);

// print the corresponding day from the array we made earlier.
std::cout << days[time.day] << " ";

// etc... 
Last edited on
I would advise against using assert() for anything but debug purposes. It should not be used to check input for validity or be part of the program logic.
There is several reasons:
1) You cannot alter its behavior. It terminates program, no questions asked. Also it does not give useful information (although there are trick for that)
2) In is not included in release version of program. All assert() entries are removed. So you can have different behavior for program with assert in debug/release mode leading to bugs you cannot debug because they do not exis in debugging mode.

Just throw an exception if you do not want to do any error recovery.
Hello,

my second question was something like
if you were asked to write

void show( const Time & time );
Display the time, so it looks like
Wednesday 7:05 PM

and the struct is
1
2
3
4
5
6
struct Time{
	unsigned day;
	bool pm;
	unsigned hour;
	unsigned minute;
};


Note that this show function and the input function below both need to know the spellings of the days of the week. A good way to handle this is to globally declare a constant array of strings with the names of the days. I don’t want you to ever declare a global variable in this class, but global constants are fine. Just don’t forget the “const”.

I solved it with the codes above how can I do it differently without using NT3 way.

Hope this make some sense
up up up
how can I do it differently
For example C++ has locales. It is class containing all kinds of locale settings and strings.

There is global locale which can be reached by calling specific function and used by locale-depended functions by default. Also all such functions accept locale as one of the parameters.
so we can't do something like declaring array of global constants includes the
days as strings and I am not sure cause I new for structs
so we can't do something like declaring array of global constants includes the
days as strings
Why? We totally can. In fact ths is what NT3 probably did (He did not specify where his array is placed).
Actually it worked but I think the assert part is optional (checking purposes )

I declared the array inside the function definition
Last edited on
Topic archived. No new replies allowed.