驾驶执照考试计划.无法将 std:: 字符串转换为 std :: 字符串和其他错误

Driver License exam program. cannot convert std:: string to std :: string and other errors

本文关键字:std 字符串 错误 其他 转换 驾驶执照 考试计划      更新时间:2023-10-16

我正在做一个驾驶执照程序,其中用户必须输入一系列答案,这些答案被放入一个字符串中,然后与实际答案进行比较,然后确定用户是通过还是失败。下面是我的程序,我收到许多错误。我在Visual Studios中使用C++

Error   C2440   'initializing': cannot convert from 'initializer list' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' line 69
Error   C2664   'void TestGrader::setKey(std::string [])': cannot convert argument 1 from 'std::string' to 'std::string []' line 73
Error (active)  E0289   no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list       line 69
Error (active)  E0413   no suitable conversion function from "std::string" to "std::string *" exists   line 73

以下是我当前的代码。如果有人可以向我解释为什么我会收到这些错误以及如何修复它们,那将不胜感激。

#include <iostream> 
#include <iomanip>
#include <string>
#include <cctype> 
using namespace std;
const int STRING_COUNTER = 20;
class TestGrader 
{
  private:
    string answers[20];
public:
    void setKey(string right[])
    {
        for (int i = 0; i < STRING_COUNTER; i++)
        {
            answers[i] = right[i];
        }
    };
    void grade(string exam[])
    {
        int correct = 0;
        int wrong = 0;
        for (int i = 0; i < STRING_COUNTER; i++)
        {
            if (exam[i] == answers[i])
            {
                correct += 1;
            }
            else
            {
                wrong += 1;
            }
        }
        if (correct >= 15)
        {
            cout << "You passed the test!" << endl;
        }
        else
        {
            cout << "You failed the test." << endl;
        }
        cout << endl;
        cout << "You got " << correct << " questions right and got " << wrong << " questions wrong." << endl << endl;
        for (int x = 0; x < STRING_COUNTER; x++)
        {
            if (exam[x] != answers[x])
            {
                cout << "You got question number " << x << " wrong.";
                cout << endl;
            }
        }
    };
};

int main()
{
    string answer = {     "B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A" };
TestGrader test;
test.setKey(answer);
string stuTest[STRING_COUNTER];
int choice = 1;
do
{
    for (int i = 0; i < STRING_COUNTER; i++)
    {
        cout << "Please enter your answer for question number " << i + 1 << ": ";
        cin >> stuTest[i];
        cout << endl;
        while (stuTest[i] > "D" || stuTest[i] < "A")
        {
            cout << "Error, only letters A,B,C and D are accepted: ";
            cin >> stuTest[i];
            cout << endl;
        }
    }
    test.grade(stuTest);
    cout << "Would you like to Retake the test? 1. Yes 2. No";
    cin >> choice;
} while (choice == 1);

return 0;
}

Error C2440 'initializing': cannot convert from 'initializer list' to 'std::basic_string,std::allocator>' line 69 -表示您有一个列表(或数组(,您正在尝试使用它初始化std::string,这是不允许的。字符串是字符串,而不是字符串数组。 如果变量是一个数组,那就没问题了:

string answer[] = {"B","D","A","A","C","A","B","A","C","D","B","C","D","A","D","C","C","B","D","A" };

Error C2664 'void TestGrader::setKey(std::string [])': cannot convert argument 1 from 'std::string' to 'std::string []' line 73 - set 键需要一个字符串数组 - 你给它一个字符串 - 上面的修复程序可以解决这个问题。