I am trying to write a simple election contract, currently trying to add candidates to an array of Candidates in my Election struct.
Can this be done and if so how can it be implemented? The code works fine when adding a single candidate without making it an array.
struct Candidate {
uint256 candidateID;
uint256 voteCount;
string name;
}
struct Elections {
uint256 elecID;
Candidate[] candidates;
}
mapping(uint256 => Candidate) public candidates;
mapping(uint256 => Elections) public elections;
function createElection(string[] memory _names) public {
electionCount++;
elections[electionCount].candidates[0] = Candidate(0, 0, _names[0]);
elections[electionCount].candidates[1] = Candidate(1, 0, _names[1]);
//elections[electionCount] = Election(electionCount, _candidates);
}