gpt4 book ai didi

c++ - 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char

转载 作者:行者123 更新时间:2023-12-02 10:33:07 25 4
gpt4 key购买 nike

当我尝试编译和运行时,我不断收到 C2664 错误,“无法将参数 1 从 char[10] 转换为 char”。我试过用指针替换数组(char answer[] to 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;
}

最佳答案

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

//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)以匹配您的声明,错误应该会消失。

关于c++ - 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61495155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com