c++ - 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char
问题描述
当我尝试编译和运行时,我不断收到 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, 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)
更改您的原型以匹配您的声明,错误应该会消失。
推荐阅读
- git - 将代码推送到过时的 git 分叉分支中
- java - 将哈希表保存到文件的最佳方法是什么?
- netty - 如何从 Netty 客户端检索响应?
- java - 如何解决从休眠中定位未映射的类错误?
- java - 如何在 Apache Web 服务器上运行 Grails 应用程序 WAR 文件?
- spring - x509 验证在捕获之前失败
- c# - 无法将自定义方法作为参数传递给 ElapsedEventHandler()
- excel - 为用户选择的文件夹调用函数的问题
- python - 为什么 Scipy 的 fit() 函数用于 gamma 分布会产生完全不同的分布?
- c# - 如何在 C# 中解析具有多个 xmlns 属性的 xml?