1

Hey guys I need to use an Int called points from method ValidateAns in a Method Main. I searched around the net and people said I should do ValidateAns(points), however it does not work for me, Im not sure if I'm doing it wrong or I should use some other way to do it

static void Main(string[] args)
{
    const int QuestionNumbers = 10;
    char[] Answear = new char[QuestionNumbers];

    Question[] MCQ = new Question[QuestionNumbers];

    MCQ[0] = new Question(Slaughterhouse);
    MCQ[1] = new Question(Frankenstein);
    MCQ[2] = new Question(Things);
    MCQ[3] = new Question(Jane);
    MCQ[4] = new Question(Kill);
    MCQ[5] = new Question(Beloved);
    MCQ[6] = new Question(Gatsby);
    MCQ[7] = new Question(Catcher);
    MCQ[8] = new Question(Pride);
    MCQ[9] = new Question(Nineteen);

    for (int i = 0; i < QuestionNumbers; i++)
    {
        Console.WriteLine("Question {0}", i + 1);
        Answear[i] = MCQ[i]();
        ValidateAns(i + 1, Answear[i]);
        Console.WriteLine();
        Console.ReadKey();
    }

}

static void ValidateAns(int Nbr, char Ans)
{
    int points= 0;

    switch (Nbr)
    {
        case 1:

            if (Ans == 'b' || Ans == 'B')
            {
                Console.WriteLine("Good Answear");
                points++;
                break;
            }
            else
            {
                Console.WriteLine("Wrong Answer - The right answer was (B)");
                break;
            }
    }
}
2
  • You can just return bool from ValidateAns to indicate if the answer was correct. In the loop logic, you will add 1 to a counter of points if the bool was true. Commented Aug 30, 2014 at 14:30
  • I think this question should not have the tag [visual-studio-2012]! Commented Aug 30, 2014 at 16:15

2 Answers 2

1

I restructured the members you gave us.

Think about naming convention, if you're going to validate something you would need to return a Boolean or bool to state whether the answer was give is valid or not. see IsValidAnswer(). When writing members that return a bool think of using a linking verb. Is, Has, Will.

when you compare the type char you can use char.ToUpperInvariant(char val), so you dont have to compare your answer to too different cases of the same character.

i hope this helps, check out the comments in the code. There is a nugget in there that you will love as a developer. :) have a good day

private static void Main(string[] args)
{
    const int QuestionNumbers = 10;
    var Answer = new char[QuestionNumbers];

    Question[] MCQ = new Question[QuestionNumbers];


    MCQ[0] = new Question(Slaughterhouse);
    MCQ[1] = new Question(Frankenstein);
    MCQ[2] = new Question(Things);
    MCQ[3] = new Question(Jane);
    MCQ[4] = new Question(Kill);
    MCQ[5] = new Question(Beloved);
    MCQ[6] = new Question(Gatsby);
    MCQ[7] = new Question(Catcher);
    MCQ[8] = new Question(Pride);
    MCQ[9] = new Question(Nineteen);

    for (int i = 0; i < QuestionNumbers; i++)
    {
        Console.WriteLine("Question {0}", i + 1);
        Answer[i] = MCQ[i]();

        // return bool since you want to validate an answer.
        var result =  IsValidAnswer(i + 1, Answer[i]);

                            // this is an if/else conditional statment, its called a ternary expression
        Console.WriteLine(result ? "Answer is valid" : "Answer is not valid");
        Console.WriteLine();
        Console.ReadKey();
    }
}


private static bool IsValidAnswer(int Nbr, char Ans)
{
    // if you really wanted to use a method. 
    var correctAnswer = default(char);
    switch (Nbr)
    {
        case 1:
            correctAnswer = 'b';
            break;
        case 2:
            //.. 
            break;
    }
    return char.ToUpperInvariant(Ans) == char.ToUpperInvariant(correctAnswer);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The answer helped a lot, Thanks.
0

define your function as

static int ValidateAns(int Nbr, char Ans)

and return the points value with return points;

then call it with something like

int p=ValidateAns(i + 1, Answear[i]);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.