1

I keep getting an C2664 error, "cannot convert argument 1 from char[10] to char" when I attempt to compile and run. I've tried replacing arrays with pointers (char answer[] to char * answers). I can do this without passing arrays to a function, but that's what I'm trying to work on getting better at.

#include <iostream>
#include <cctype>
using namespace std;

//Function prototypes
bool grade(char, char, int, int&, int&);
int goodbye();

int main()
{
    const int TEST_LENGTH = 10;
    char const correctAnswers[TEST_LENGTH] =
    { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
    char studentAnswers[TEST_LENGTH] = {};
    int correct = 0, missed = 0;
    bool passing;

    cout << "Welcome to your Driver's License Exam." << endl;
    cout << "This test is ten multiple choice questions." << endl;
    cout << "Only A, B, C, and D are valid answers." << endl;
    cout << "Please begin.";

    for (int i=0; i < TEST_LENGTH; i++)
    {
        int errorCount = 0;
        cout << "Question " << i + 1 << ": ";
        cin >> studentAnswers[i];
        studentAnswers[i] = toupper(studentAnswers[i]);
        while (studentAnswers[i] < 'A' || studentAnswers[i] > 'D')
        {
            if (errorCount++ > 3)
                goodbye();
            cout << "Only A, B, C, and D are valid input. Reenter. " << endl;
            cout << "Question " << i + 1 << ": ";
            cin >> studentAnswers[i];
        }
    }

    passing = grade(studentAnswers, correctAnswers, TEST_LENGTH, correct, missed);

    if (passing) 
    {
        cout << "Congratulations!" << endl;
        cout << "You have passed the exam." << endl;
        cout << "Total number of correct answers: " << correct << endl;
        cout << "Total number of incorrect answer: " << missed << endl;
    }
    else 
    {
        cout << "You have failed the exam" << endl;
        cout << "Sorry, you have not passed the exam." << correct << endl;
        cout << "Total number of incorrect answer: " << missed << endl;

    }
    return 0;
}

bool grade(char answers[], const char key[], const int size, int& hits, int & misses)
{
    for (int i = 0; i < size ; i++ )
    {
        if (answers[i] == key[i])
            hits++;
        else
            misses++;
    }

    if (hits >= 8)
        return true;
    else
        return false;
}

int goodbye() 
{
    cout << "GOOD BYE" << endl;
    return 1;
}

3 Answers3

0

Your function prototype doesn't match your declaration:

//Prototype, takes two chars as first params
bool grade(char, char, int, int&, int&); 

//Declaration, takes two char-arrays (pointers to char) as first params
bool grade(char answers[], const char key[], const int size, int& hits, int & misses)

Change your prototype to match your declaration and the error should go away.

melk
  • 520
  • 3
  • 9
0
bool grade(char, char, int, int&, int&);

and

bool grade(char answers[], const char key[], const int size, int& hits, int & misses)

See the difference? This is a case where the compiler error message is telling you exactly what the problem is. You just need to learn to see clearly the code that you have written.

john
  • 71,156
  • 4
  • 49
  • 68
0

Change your function prototype:

//Function prototypes
bool grade(char*,const char*, const int, int, int);

instead of:

//Function prototypes
bool grade(char, char, int, int&, int&);

should do the tricks. Short TIP: checking the output of the compiler error would lead you also to the answer: e.g

error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
dboy
  • 867
  • 2
  • 11
  • 21