Home > OS >  how do I get this function to run
how do I get this function to run

Time:02-23

Im having trouble with calculating correct answers and determining weather a function returns true or false

char correctAnswers[] = {'B','D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};
char studentAnswers[10] = {}; 

I have my student answers and the correct answers stored like this ^^^

bool grade(char correctAnswers[], char studentAnswers[]){
    int j = 0;
    int b = 0;
    for(int x=0; x<10; x  ){
                if(correctAnswers[b]==studentAnswers[j]){
                    b  ;
                    j  ;
                      ansCorrect;
                    
                }
    }
                

    
    if(ansCorrect >= 8){//8 out of 10 correct
        return true;}
    else;
        return false;
}

then with this piece of code I am trying to check each answer the user inputs to see if it matches the given answer on the corresponding index (1,1)(2,2)(3,3) etc.

if (grade()==true){
    cout << "Congratulations!\nYou have passed exam.\n";
}
    if (grade()==false){
    cout << "Sorry, you have not passed the exam!\n";}

This piece of code is 100 % wrong, but I am trying to use it as a reference as to what I need, I need to take the grade function and check if it returned true or false

CodePudding user response:

Your loop logic is pretty wild. I remember seeing your question yesterday, and had pointed out many issues with it. Looks like you tried to take some of that on board and fix your loop but it's still really broken.

The issue is you only ever increment b and j if you get a correct answer. So, ask yourself: what if the values don't match? Well, you never increment your indices and so every subsequent iteration of the loop is testing the same values.

The way to test each element against the corresponding one is to use the loop variable x as an index.

for(int x = 0; x < 10; x  ) {
    if (correctAnswers[x] == studentAnswers[x]) {
          ansCorrect;
    }
}   

With a few extra tidy-ups, your function can become:

bool grade(char correctAnswers[], char studentAnswers[])
{
    int ansCorrect = 0;
    for(int x = 0; x < 10; x  ) {
        ansCorrect  = (correctAnswers[x] == studentAnswers[x]);
    }
    return ansCorrect >= 8;
}

And you should change how you call it. Right now, you're calling it twice. Just use else. And try to avoid comparing boolean values to true. Either omit the comparison completely, or test that it's not false. The reason is that not all "true-like" values are equal to true. In C , any non-zero value can be a logical truth, whereas zero is always false.

So, while this may seem to be splitting hairs for a type like bool which does have guarantees, getting in the habit of avoiding "equals true" tests will help you not create bugs in future when you are working on more complex programs.

if (grade()) {
    cout << "Congratulations!\nYou have passed exam.\n";
} else {
    cout << "Sorry, you have not passed the exam!\n";
}

CodePudding user response:

maybe the ";"after "else" is a bug. and you can try this:

#include <iostream>

using namespace std;

bool grade(char correctAnswers[], char studentAnswers[])
{
    int ansCorrect = 0;
    for (int i = 0; i < 10; i  )
    {
        if (correctAnswers[i] == studentAnswers[i])
        {
            ansCorrect  ;
        }
    }
    if (ansCorrect >= 8)
    { // 8 out of 10 correct
        return true;
    }
    else
        return false;
}

int main()
{
    char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};
    char studentAnswers[10] = {};
    for (int i = 0; i < 10; i  )
    {
        cin >> studentAnswers[i];
        cin.ignore();
    }
    if (grade(correctAnswers, studentAnswers) == true)
    {
        cout << "Congratulations!\nYou have passed exam.\n";
    }
    else if (grade(correctAnswers, studentAnswers) == false)
    {
        cout << "Sorry, you have not passed the exam!\n";
    }
}
  •  Tags:  
  • c
  • Related