我有一個家庭作業程序,我需要一點幫助。我需要將包含測試答案關鍵字的數組與包含學生答案的數組進行比較。我遇到的問題是我需要考慮空白答案。我似乎無法拿出比較數組的代碼,然後顯示分數。測試得分爲正確答案爲2分,不正確答案爲1分,空白答案爲零分。比較C++中的數組元素
這是輸入的一個示例:
TTFTFTTTFTFTFFTTFTTF
ABC54102ŤFTFTFTTTFTTFTTF TF
第一行是關鍵,第二行是學生數據的第一行。
這是我的代碼:
#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牛逼FTFTFTTTFTTFTTF TF 27 d
ADE62366 TTFTFTTTFTFTFFTTF__ 34 B(帶_空格)
它顯示的方式是這樣的:
重點:TTFTFTTTFTFTFFTTFTTF
ABC54102牛逼FTFTFTTTFTTFTTF TF 27 d
ADE62366 TTFTFTTTFTFTFFTTF 34b的
其中,我認爲,這是一個定位的問題,因爲我現在已經推送了一些代碼。
什麼,如果有什麼是你的問題?什麼工作,不工作等? – deinst 2010-08-08 21:31:20
請確實說明問題或問題。如果你的代碼不能編譯 - 給出你得到的錯誤信息。如果它編譯但是沒有正確運行,則給出您的預期行爲和實際行爲。 – 2010-08-08 21:31:53
另外,你的意見應該說明爲什麼會發生。讀者可以看到像'int i'這樣的陳述聲明'i',他/他不知道'i'代表什麼。與if語句類似,爲什麼要進行比較? – deinst 2010-08-08 21:37:01