1
//SPDX-License-Identifier: Unlicense

// contracts Exam
pragma solidity ^0.8.0;


contract Exam {
    // The address of the exam administrator
    address public administrator;

    // The exam questions and answers
    mapping(uint => string) public questions;
    mapping(uint => string) public answers;

    // The exam submissions
    mapping(address => Submission) public submissions;

    // The number of questions in the exam
    uint public numQuestions;

    // The minimum score required to pass the exam
    uint public passingScore;

    // The maximum number of attempts allowed per user
    uint public maxAttempts;

    // The state of the exam (open or closed)
    bool public isOpen;

    // The Submission struct represents a user's exam submission
    struct Submission {
        uint[] answers;
        uint score;
        uint numAttempts;
    }

    constructor(uint _numQuestions, uint _passingScore, uint _maxAttempts) public {
        administrator = msg.sender;
        numQuestions = _numQuestions;
        passingScore = _passingScore;
        maxAttempts = _maxAttempts;
        isOpen = true;
    }

    // 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;
    }}

    // The getScore function allows a user to retrieve their current score for the exam
   function getScore() public view returns (uint) {
        address user = msg.sender;
        return submissions[user].score;
    }

    // The closeExam function allows the exam administrator to close the exam
    function closeExam() public {
        require(msg.sender == administrator, "Only the administrator can close the exam.");
        isOpen = false;
    }
}

1 Answer 1

1

to check if string value has been changed from default you need to check length of this string. To do this change in your code from:

require(questions[_questionNumber] == bytes32(0), "Question has already been added.");

To:

require(bytes(questions[_questionNumber]).length == 0, "Question has already been added.");

You need to convert string value to bytes, cuz string doesn't have .length functionality.

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.