2010-08-08 4 views
0

약간의 도움이 필요한 숙제 프로그램이 있습니다. 나는 학생의 대답을 포함하는 배열에 대한 테스트의 대답 키를 포함하는 배열을 비교할 필요가있다. 내가 겪고있는 문제는 공백 응답을 고려해야한다는 것입니다. 나는 배열을 비교하고 점수를 표시하는 코드를 생각해 낼 수 없다. 이 시험은 정답의 경우 2 점, 오답의 경우 1 점, 빈 대답의 경우 0 점으로 점수가 매겨집니다.
ABC54102 T FTFTFTTTFTTFTTF TFC++의 배열 요소 비교

첫 줄 열쇠가 두 번째 행은 학생 데이터의 첫 번째 라인

TTFTFTTTFTFTFFTTFTTF :

입력의 예이다. 설명에 대한

#include <cmath> 
#include <fstream> 
#include <cstring> 
#include <string> 
#include <iostream> 

using namespace std; 

int checkAnswers(char key[], char answers[]); 
void displayGrade(int score); 

int main() 
{ 
    ifstream inFile; 

    int score = 0; 
    char key[21]; 
    string studentID; 
    char answers[21]; 
    int studentCount; 

    inFile.open("Ch9_Ex6Data.txt"); //opens input file 
    if (!inFile) //sets condition for if input file does not exist 
    { 
     cout << "Unable to locate file." << endl; //informs user that input file is missing 
     cout << "Program terminating." << endl; //informs user that program is terminating 
     return 1; //terminates program with error 
    } 

    inFile.getline(key, 21); 

    cout << "Processing Data..." << endl << endl; 
    cout << "Key: " << key << endl << endl; 

    while (inFile >> studentID) 
    { 
     cout << studentID; 
     inFile.getline(answers, 22); 
     cout << answers << " "; 
     score = checkAnswers(key, answers); //calls checkAnswer function and sets result equal to score 
     displayGrade(score); 

    } 

    return 0; 
} 

//User-defined Function 1 
int checkAnswers(char key[], char answers[]) 
{ 
     //Function Variables 
    int i, length; //declares i variable 
    int correct = 0, incorrect = 0, blank = 0, score = 0; //declares and initializes correct, incorrect, blank, and score variables 

    answers >> length; 
    for (i = 0; i < 22; i++) //initiates conditions for for loop 
    { 
     if (answers[i] == ' ') //initiates if condition 
     { 
      i++; 
     } 
     else if (key[i] == answers[i]) //initiates if condition 
     { 
      correct++; //sets condition for correct answers 
     } 

     else if (key[i] != answers[i]) //initiates if condition 
     { 
      incorrect++; //sets condition for incorrect answers 
     } 

     score = 40 - incorrect; //calculates score 
    } 

    cout << score << " "; //output student score 
    return score; //pass score 
} 

편집 :

는 내가 가지고있는 코드 나는과 같이 표시 할 코드가 필요합니다 :

키 : TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF 27 D
ADE62366 TTFTFTTTFTFTFFTTF__ 34 B (_가 공백 인 경우)

이 표시됩니다 (210)

방법은 다음과 같이이다 :

키 : TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF 27 D
ADE62366 TTFTFTTTFTFTFFTTF (34)의 B, 내 생각, 정렬 문제입니다

, 내가 지금 코드를 트윗 했으니 까.

+1

질문이 있으면 무엇이 있습니까? 작동하는, 작동하지 않는 등? – deinst

+1

실제로 문제 또는 질문을 말하십시오. 코드가 컴파일되지 않으면 오류 메시지가 나타납니다. 컴파일하지만 올바르게 실행되지 않으면 예상되는 동작과 실제 동작을 제공하십시오. –

+1

또한 귀하의 의견은 왜 어떤 일이 일어나고 있는지 말해야합니다. 독자는 int i;와 같은 문장은'i'를 선언하고,'i'는 무엇을 의미 하는지를 알지 못한다. if 문과 마찬가지로 비교를하는 이유는 무엇입니까? – deinst

답변

2

일부 발언 :

char answers[21]; 
    inFile.getline(answers, 22); 

당신은 21 크기의 배열에 22 개 문자를 읽을 수 없습니다.

answers >> length; 

이것은 의미가 없습니다.

for (i = 0; i < 22; i++) //initiates conditions for for loop 

답변이 20 개 밖에없는 경우 왜 색인을 생성하기 위해 루프를 돌릴까요?

score = 40 - incorrect; //calculates score 

이것은 루프 이후에 배치 할 수 있지만 규칙에 따라 점수를 계산하지 않는 이유는 무엇입니까 (2 * correct-incorrect)?