// 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;
}}
-
Can you be a bit more clear with your question, and write your code examples in code blocks appropriately?– immaxkentCommented Dec 27, 2022 at 11:10
Add a comment
|
1 Answer
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!")
-
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
-