继续收到C2664错误-无法将参数从char [10]转换为char

问题描述 投票:0回答:2

当尝试编译和运行时,我不断收到C2664错误,“无法将参数1从char [10]转换为char”。我试过用指针(char answer []到char * Answers)替换数组。我可以在不将数组传递给函数的情况下做到这一点,但这就是我要努力提高的水平。

#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;
}
c++ arrays pass-by-reference c2664
2个回答
0
投票

您的函数原型与您的声明不匹配:

//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)

更改原型以符合您的声明,错误应该消失。


0
投票
bool grade(char, char, int, int&, int&);

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

看到区别了吗?在这种情况下,编译器错误消息会告诉您确切的问题是什么。您只需要学习清楚地看到已编写的代码即可。

© www.soinside.com 2019 - 2024. All rights reserved.