0

Very new to Solidity/Coding in general. Trying to make a Kahoot-like game where there's a Teacher (owner) that can upload questions with an ID to each question, 4 optional answers & 1 correct answers.

Created a nested mapping with a Struct in it, and the Struct has an array in it.

How do i append this array inside the struct inside the nested mapping? i tried to do this function but i am getting an error - Undeclared identifier.

Would really appreciate some help!

function addQuestion(
uint _qid, 
string memory _question, 
string memory _answerOne, 
string memory _answerTwo, 
string memory _answerThree, 
string memory _answerFour, 
string memory _correctAnswer
) public isOwner {
    questions[_qid][_question] = Questions(Answers.push(_answerOne, _answerTwo, _answerThree, _answerFour), _correctAnswer);

}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract MyContract {
    
    address payable public owner;
    constructor() {
        owner = payable(msg.sender);
    }

    modifier isOwner {
        require(msg.sender == owner);
        _;
    }

    function deposit() public payable {
        owner.transfer(msg.value);
    }

    struct Questions {
        string[] Answers;
        string correctAnswer;
    }

    struct Students {
        string Name;
        address StudentAddress;
    }
    


    mapping(uint => Questions) public students;
    mapping(uint => mapping(string => Questions)) public questions;

    function addStudent(uint _id, address _studentAddress, string memory _studentName) public isOwner {
        students[_id] = Students(_studentName, _studentAddress);
    }


    function addQuestion(uint _qid, string memory _question, string memory _answerOne, string memory _answerTwo, string memory _answerThree, string memory _answerFour, string memory _correctAnswer) public isOwner {
        questions[_qid][_question] = Questions(Answers.push(_answerOne, _answerTwo, _answerThree, _answerFour), _correctAnswer);

    }

}

1 Answer 1

0

Working with strings and especially string arrays in solidity might be a little bit tricky. Because in solidity strings are basically byte arrays. If you make a string array, you are making a two-dimensional byte array. Two-dimensional arrays also have some limitations.

Maybe this isn't the ideal solution but this should work:

function addQuestion(uint _qid, string memory _question, string memory 
_answerOne, string memory _answerTwo, string memory _answerThree, string memory 
_answerFour, string memory _correctAnswer) public isOwner {
        string[] storage _answers;
        _answers.push(_answerOne);
        _answers.push(_answerTwo);
        _answers.push(_answerThree);
        _answers.push(_answerFour);
        questions[_qid][_question] = Questions(_answers, _correctAnswer);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.