在我的char数组函数中获取错误?

时间:2015-03-06 14:11:47

标签: c++ arrays function

#include <iostream>
using namespace std;

const int SIZE = 20;
char correctAnswers(char [], int);
char userAnswers(char [], int);
void compareArray(char[] , char [], int [], int, int &);

int main(){
    char correct[SIZE];
    char userArray[SIZE];
    int result[SIZE];
    int element;

    correctAnswers(correct, SIZE);
    cout << "Please enter the student's answers for each of the questions."  << endl;
    cout << "Press Enter after typing each answer." << endl;
    cout << "Please enter only an A, B, C, or D for each question." << endl;
    userAnswers(userArray, SIZE);
    compareArray(correct, userArray, result, element);

    cout << "Array 1 and Array 2 are different at" << element<<" positions: "   << " ";

    for(int i = 0; i < element; i++)
        cout << result[i] << " ";

    return 0;
}

char correctAnswers(char correct[], int SIZE){
    correct[SIZE] = {'B','D','A','A','A','B','B','A','C','D','B','B','D','A','D','D','A','B','D','A'};
}

char userAnswers(char userArray[], int SIZE){
    userArray[SIZE];

    for(int i = 0; i < 20; i++){
        cout << "Question " << (i+1) << ":";
        cin >> userArray[i];
        while(userArray[i] != 'A' || userArray[i] != 'B' || userArray[i] != 'C' || userArray[i] != 'D'){
            cout << "Invalid input. Choose A,B,C or D." << endl;
            cin >> userArray[i];
        }
    }
}

void compareArray(char correct[], char userArray[], int result[], int &element){
    int right = 0, wrong = 0;

    element = 0;

    for(int i = 0; i < SIZE; i++){
        if(userArray[i] == correct[i]){
            right = right + 1;
        }
        else
            wrong = wrong + 1;
    }

    if(right >= 15){
        cout << "You Passed!";
    }
    else
        cout << "You Fail.";

    cout << "Correct answers = " << right << endl;
    cout << "Incorrect answers = " << wrong << endl;

    for(int i = 0; i < SIZE; i++){
        if(userArray[i] != correct[i]){
            result[element] = i+1;
            element++;
        }
    }
}

我有3个错误,无法找出原因。在我的char correctAnswers函数中,我收到错误说&#34;无法转换为&#39; braced-init-list&#39;到#char;&#34;并且初始化程序包含太多元素。另外,在我的上一个函数中它告诉我函数不带四个元素?

1 个答案:

答案 0 :(得分:0)

correct中的{p> correctAnswerchar的数组,因此correct[SIZE]是该数组的特定元素(尽管位于末尾)你正试图将一个字符数组分配给。(/ p>)

即使您删除了[SIZE],您仍然会遇到问题,因为您正在尝试使用初始化程序(正如同样的建议,初始化 )进行作业。

根本不明白你对最后一项功能的意思。