'Output elements from a struct array
I'm tasked to ask the user for their data (name, last name, email & password), but I'm constrained to only using 25 lines per method which means that I have to use MANY of them. I'm using a struct for the data, and vectors for each user. However, when I want to display the users information it shows completely blank or just the direction of the array. I've tried many things with little to no success.
Here's the code in question.
using System;
public struct Usuario
{
public String name;
public String lastname;
public String email;
public String phone;
public String password;
}
static void Main()
{
Console.Clear();
Usuario[] user = new Usuario[5];
Array.Copy(user, UserNew(), 5);
UserView(user);
}
static Usuario[] UserNew()
{
int control = 1, i = 0;
Console.Clear();
Usuario[] register = new Usuario[5]; //It's supposed to have 5 users
Console.Write("\t\t¡Bienvenido al registro de usuario!\n\n");
while(control == 1)
{
register[i].name = Name();
register[i].lastname = LastName();
register[i].email = Email();
register[i].phone = Phone();
register[i].password = Psswrd();
Console.Write("\n\n\t¿Desea registrar otro usario?\n\tDigite 1 para registrar otro: ");
control = int.Parse(Console.ReadLine());
i++;
}
Menu();
return register;
}
static void UserView(Usuario[] UserReg)
{
UserReg = new Usuario();
Console.Write("\n\tIntroduzca el código de acceso: ");
String access = Asterisk('*');
if (access == "123456")
{
for (int i = 0; i <= 2; i++)
{
Console.Write("\n" + UserReg[i]);
}
}
else
{
Console.Write("\n\n\tEl código de acceso es incorrecto!");
Thread.Sleep(2000);
Menu();
}
}
Solution 1:[1]
This is a perfect example of why you should ALWAYS read the relevant documentation, especially when something doesn't work as you expect. If you had debugged your code then you would have seen that UserNew returns an array as expected but, after calling Array.Copy, it's contents is not copied to the user array. If you had then consulted the documentation for the Array.Copy method then you would have seen that the first parameter is the source and the second is the destination. You're copying the empty array over the full one, instead of the other way around. If you pass the source and destination arrays in the correct order, things will work as expected:
Array.Copy(UserNew(), user, 5);
That said, the way you're doing things doesn't really make much sense. You start by creating an array with a number of empty items, then you create another array with populated items, then you copy the data from the populated array to the empty one. Why not just use the populated array? This:
Usuario[] user = new Usuario[5];
Array.Copy(user, UserNew(), 5);
would more sensibly be written like this:
Usuario[] user = UserNew();
Now you can't mess up the copy because you're not doing it. You also don't use double the data for no reason.
EDIT:
The code seems worse than I first thought, so I'm going to provide a basic outline of how it ought to work and you can fill in the blanks. You should have method to get the data, e.g.
static User[] GetUsers()
{
var users = new User[5];
for (var i = 0; i < users.Length; i++)
{
// Gather input and set users[i]
}
return users;
}
and a method to display data, e.g.
static void DisplayUsers(User[] users)
{
foreach (var user in users)
{
// Display user
}
}
and then you should call one to get the data and then pass that data into the other, e.g.
var users = GetUsers();
DisplayUsers(users);
You almost certainly wouldn't use an array like that in a real app, because you generally wouldn't know how many users you were going to have. If your assignment requires you to use arrays though, this is pretty much how you should do it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
