Thread: C project Halp!

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    C project Halp!

    Hello everyone, I am writing a program that shows the user a list of choices. 1. Addition 2. Subtraction, 3. Multiplication 4. Division. The program runs through 5 problems (randomly generated numbers) I'd like to calculate the final grade after the 5 problems have been answered. I am using a function to show if the answer is correct or wrong. This same function is calling another funtion to calculate the grade (correctAnswers/numproblems * 100) however the way I have it now this does not work.
    Code:
        while(choice != 5)
        {
            switch (choice)
            {
                case 1: // Addition
                    {
                        for(i = 1; i <= PROBLIM; i++)
                        {
                        opSymbol = '+';
                        num1 = twoDigitNumber();
                        num2 = twoDigitNumber();
                        correctAnswer = addition(num1, num2);
                        showOperation(num1, num2, opSymbol);
                        scanf("%d", &studentAnswer);
                        checkAnswer(studentAnswer, correctAnswer, i); //here is where I try to calculate the grade
                        }
                        break;
    // this is the funtion that checks to see if the answer is right or wrong
    void checkAnswer(int studentAnswer, int correctAnswer, int a)
    {
        double correctCounter = 0.0, problemCounter = 0.0;
        
        if (studentAnswer == correctAnswer)
        {
            printf("Correct!\n\n");
            correctCounter++;
        }
        else
        {
            printf("Incorrect, Answer is: %d\n\n", correctAnswer);
            problemCounter++;
            correctCounter++;
        }
        if (a ==5)
        {
            printf("%.2f\n\n", grade(ptr_correctCounter, ptr_problemCounter)); // here i call the grade funtion
        }
        return;
    }
    
    double grade(double a, double b)
    {
    
        return ((a/b) * 100); //should return a percentage
    }
    I know the reason it doen't work is bc each time i call the checkAnswer funtion both counters get reset back to 0.0 anyone know of a better perhaps simpler way of doing this?
    Thank you for any help in advance and let me know if you need any clarification

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your "checkAnswer()" function is trying to do too much. It should only check a single answer, and return a result indicating whether the answer was correct or not.

    Keeping track of the number of correct/incorrect answers, and the total number of problems, should be done in the function with the "while()" loop.

    That same function should, after all problems have been answered, then call your "grade()" function.

    Also note counters should be of type int, not double.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-02-2013, 06:45 PM
  2. Struct halp.. :p
    By sourpatchkid in forum C Programming
    Replies: 5
    Last Post: 05-27-2013, 01:18 AM
  3. Music database using Linked Data Structures. halp!
    By Chucker in forum C Programming
    Replies: 1
    Last Post: 03-28-2012, 04:34 PM
  4. Replies: 7
    Last Post: 09-14-2010, 04:24 PM
  5. Ned Halp Wit Getng Input
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 03-18-2005, 03:50 PM

Tags for this Thread