Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 继续获取C2664错误-无法将参数从字符[10]转换为字符_C++_Arrays_Pass By Reference_C2664 - Fatal编程技术网

C++ 继续获取C2664错误-无法将参数从字符[10]转换为字符

C++ 继续获取C2664错误-无法将参数从字符[10]转换为字符,c++,arrays,pass-by-reference,c2664,C++,Arrays,Pass By Reference,C2664,当我试图编译和运行时,我一直收到一个C2664错误,“无法将参数1从char[10]转换为char”。我尝试用指针(char-answer[]到char*answers)替换数组。我可以在不将数组传递给函数的情况下实现这一点,但这正是我努力改进的地方 #include <iostream> #include <cctype> using namespace std; //Function prototypes bool grade(char, char, int, in

当我试图编译和运行时,我一直收到一个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;
}
#包括
#包括
使用名称空间std;
//功能原型
布尔等级(char,char,int,int&,int&);
int再见();
int main()
{
const int TEST_LENGTH=10;
char const correctAnswers[测试长度]=
{'B','D','A','A','C','A','B','A','C','D'};
char studentAnswers[测试长度]={};
int correct=0,missed=0;
传球;

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

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

更改原型以匹配您的声明,错误将消失。

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

//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)
bool grade(char, char, int, int&, int&);
更改原型以匹配您的声明,错误就会消失

bool grade(char, char, int, int&, int&);

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


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

更改您的函数原型:

//Function prototypes
bool grade(char*,const char*, const int, int, int);
而不是:

//Function prototypes
bool grade(char, char, int, int&, int&);
你应该做的技巧。简短提示: 检查编译器错误的输出也会让您找到答案:例如

error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]

更改您的功能原型:

//Function prototypes
bool grade(char*,const char*, const int, int, int);
而不是:

//Function prototypes
bool grade(char, char, int, int&, int&);
你应该做的技巧。简短提示: 检查编译器错误的输出也会让您找到答案:例如

error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]

等级
的定义和声明不同。
等级
的定义和声明不同。