C
C#13mo ago
Mikaeldef

Can I add elements to an array while running the code?

Hi there! Bashing my head in with my beginner C# assignment. From the instructions they want us to Create a program that allows the user to select these options via a menu.
- Enter the name and age of a user-defined number of family members. - Print the stored family members in the console - Print the sum of the ages of the added family members - Print the average age of the added family members in the console o The average age should be printed to two decimal places, for example, 37.29. - Be able to choose to exit the program The data entered by the user should be stored using appropriate data types and data structures. In this assignment, you should also use an array to store the names of the family members and an array to store the ages.
The problem I'm having is with the array part. From what I've seen on the internet a List would be better for this. Is there something I'm missing?
8 Replies
Jimmacle
Jimmacle13mo ago
a list would be better in that it can dynamically expand to hold more items, but since your requirements specify a user defined size an array will work fine don't expect school to teach you the best ways to do things, because in my experience they don't
Mikaeldef
MikaeldefOP13mo ago
How would you do this? Just have the user manually put the data in arrays or can I somehow create empty array slots and edit those?
Jimmacle
Jimmacle13mo ago
your program should create an array large enough to hold the number of items the user says they're going to put in so the second one, though that's not exactly how i'd describe it
Mikaeldef
MikaeldefOP13mo ago
Took a small break, trying to make it so I could store the user input in the array. But I'm having trouble. I'm trying to get a for loop to put all the stuff I need into the array but I can't figure out how to do it... This is what I have at the moment but the user input does not seem to not transfer to "familyName".. is there something I'm missing or am I thinking totally wrong?
No description
SinFluxx
SinFluxx13mo ago
You're not too far off, familyName though is the array as a whole, whereas you want to assign a name to a single entry in the array Also enteredName is already a string, so you don't need to do anything to it when assigning it to one of the entries in familyName
Mikaeldef
MikaeldefOP13mo ago
I tried this, but that didn't work either. I still just get "System.String[]" as a response. Is this more right?
No description
SinFluxx
SinFluxx13mo ago
Well again, when you're using Console.WriteLine() you're passing in familyName, which is the whole array, you'd need to pass in a specific index of it (like you've now done in your for loop) to print out that individual value - or loop through the array to get all the values and turn them into one string you can then print out
Mikaeldef
MikaeldefOP13mo ago
Aaaa I understand Now it works very cool!
Want results from more Discord servers?
Add your server