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
Program crashes cause you attempt to access "classified" "intel" in line 20 of your show function.
constunsigned 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 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...
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.
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.
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.