S
sherifffruitfly
Hi,
I'm just learning cpp, and the exercise I'm working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of the struct, and populate them
with data.
3) cin 1, 2, 3, or 4 from the user
4) If the user selected, say, 2, display the contents of the 2nd data
member from each of the structs in the array. Similarly for 1,3, and 4.
I'm trying to find an elegant way to achieve 4).
It's easy to achieve the functionality with a set of ifs/for loops (if
input=1, then for loop thru the struct array, cout <<
stuctInstance.FirstMember ....)
But this seems grossly inelegant, as I'm essentially writing the same
for-loop code 4 times - once for each input possibility. I would like
NO ifs, and just ONE for loop, whose key statement intuitively looks
like:
cout << structInstance.whicheverMemberTheUserSelected
Does it make sense what I'm looking to do? Is there a standard/elegant
way to achieve this? If I'm not making sense, I'll just post the
"inelegant" solution I have...
tia!
cdj
I'm just learning cpp, and the exercise I'm working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of the struct, and populate them
with data.
3) cin 1, 2, 3, or 4 from the user
4) If the user selected, say, 2, display the contents of the 2nd data
member from each of the structs in the array. Similarly for 1,3, and 4.
I'm trying to find an elegant way to achieve 4).
It's easy to achieve the functionality with a set of ifs/for loops (if
input=1, then for loop thru the struct array, cout <<
stuctInstance.FirstMember ....)
But this seems grossly inelegant, as I'm essentially writing the same
for-loop code 4 times - once for each input possibility. I would like
NO ifs, and just ONE for loop, whose key statement intuitively looks
like:
cout << structInstance.whicheverMemberTheUserSelected
Does it make sense what I'm looking to do? Is there a standard/elegant
way to achieve this? If I'm not making sense, I'll just post the
"inelegant" solution I have...
tia!
cdj