I'm not quite sure I get the question. If you meant the switch in the previous post, well, that's already just about a function in it's own right. You could do something like this I suppose, and have switch/case inside the new function outputAddress():
1 2 3 4
|
while(!fin.eof())
{
outputAddress(Printchoice, str1, str2, str3);
}
|
If you're concerned about passing so many parameters around, you could group some of them together, for example the address lines str1, str2, str3 are related, so could be put into a structure or class, and then passed around as a single parameter.
1 2 3 4 5
|
struct Address {
std::string line1;
std::string line2;
std::string line3;
} address;
|
In this case, you'd need to pass address instead of str1, str2 and str3.
So why even use enum? Does it really improve readabilty and what exactly do you mean by readability? |
It's sometimes hard to explain ideas because necessarily the programs done as exercises are not hugely complicated, but real-world programs can be very long, and if you are working on some part in the middle, it's a great help to be clear about what it is you are dealing with.
Let's say for example, you have a variable like this:
int dayNumber
;
Now if you just glance at that, you don't know whether it's the day of the week, or of the month, quarter, or year. Let's say it has a more meaningful name,
int dayOfWeek = 4;
. Can you tell whether dayOfWeek here represents "Tuesday" or "Wednesday" or "Thursday"? You'd have to read the documentation, (which may be missing or out of date), or the program comments (ditto), or then start to study the program code itself.
But if an enum was used, all becomes clear:
1 2
|
enum weekday { sun, mon, tue, wed, thu, fri, sat };
weekday dayOfWeek = thu;
|
Now this is a trivial example, in the real world the types of data, and the permitted values may be much less familiar, and the usefulness of the idea becomes more valuable.
Bear in mind it will often be necessary to work on code which was written by someone else, and even if you write it all yourself, six months down the line even one's own code can appear baffling if it's not clearly written. Comments in the code can help, but these can and do get out of step with what the program really does. So good clear, self-documenting code does help. By self-documenting, I mean the enum, together with sensible names for functions and variables etc. should make it clear what the program is doing.