0
 // The addQuestion function allows the exam administrator to add a question to the exam
    function addQuestion(uint _questionNumber, string memory _question, string memory _answer) public {
        require(msg.sender == administrator, "Only the administrator can add questions to the exam.");
        require(_questionNumber > 0 && _questionNumber <= numQuestions, "Invalid question number.");
        require(questions[_questionNumber] == bytes32(0), "Question has already been added.");

        questions[_questionNumber] = _question;
        answers[_questionNumber] = _answer;
    }

    // The submitAnswers function allows a user to submit their answers to the exam
    function submitAnswers(uint[] memory _answers) public {
        require(isOpen, "Exam is closed.");
        require(_answers.length == numQuestions, "Incorrect number of answers.");
        address user = msg.sender;
        Submission storage submission = submissions[user];
        submission.numAttempts++;

        require(submission.numAttempts <= maxAttempts, "Exceeded maximum number of attempts.");

        // Grade the submission
        submission.score = 0;
        for (uint i = 0; i < numQuestions; i++) {
            if (_answers[i] == uint(answers[i])) {
 {submission.score++;
            }
        }
// Save the submission
        submission.answers = _answers;
    }}
1
  • Can you be a bit more clear with your question, and write your code examples in code blocks appropriately?
    – immaxkent
    Commented Dec 27, 2022 at 11:10

1 Answer 1

0

I am guessing that you have a mapping like mapping(uint => string) public questions;

So instead of comparing it with a bytes32 object for the size, you can just do

require(bytes(questions[_questionNumber]).length == 0,"Already Exists!")

2
  • thanks alot this was super helpful...i got another TypeError: Explicit type conversion not allowed from "string storage ref" to "uint256".at the if statement below as well if (_answers[i] == uint(answers[i])) { {submission.score++; } } Commented Dec 27, 2022 at 15:33
  • Still need help or did you manage to solve it? Commented Dec 29, 2022 at 20:47

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.