我的代碼有點麻煩。它幾乎應該打開兩個文件,並將文件「StudentAnswers.txt」的第20行([作爲char輸入到char數組]中)與另一個文件的每行中的char值進行比較「CorrectAnswers.txt」在另一個陣列中的相同位置(索引)。這就像一個線性搜索,但在陣列中的位置相同。然後應該顯示一個報告,詳細說明學生錯過了哪個問題,給出的答案,正確答案,以及學生是否通過了(> 70%),如下所示:多次讀入數組
學生X報告: 2(A/D),3(C/D),5(D/A) 這位學生通過了考試!
然後它應該清除SAArray,並從StudentAnswers.txt中提供下20行,並重新開始整個過程。我想程序必須從('StudentAnswers.txt'文件/ 20行)確定學生人數。
我在顯示報告時遇到問題,並且在程序之後讓陣列自行清除。我猜測這可以通過一個while循環和一個累加器來完成(由上面的公式確定)。 此外,Visual Studio似乎去「錯過__問題總共___%」,然後保持循環-858993460。
任何幫助,將不勝感激。
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#include <algorithm>
using namespace std;
void GradeReturn(char[], char[], int, int, int);
string PassFail(float);
int main()
{
ifstream SA("StudentAnswers.txt");
ifstream CA("CorrectAnswers.txt");char CAArray[20];
char SAArray[20];
// char SA2Array[20];
bool isCorrect;
int correct;
int incorrect;
int counter;
correct = 0;incorrect = 0;
counter = 0;
cout << endl;
if (!SA.fail())
{
cout << "'StudentAnswers.txt' file opened successfully." << endl;
cout << "'CorrectAnswers.txt' file opened successfully." << endl << endl;
int a = 0;
int b = 0;
while (a < 20)
{
CA >> CAArray[a];
a++;
} // while loop to feed char into the array
while (b < 20)
{
SA >> SAArray[b];
b++;
}
} // while loop to feed char into array
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter);
return 0;
}
void GradeReturn(char CAArray[], char SAArray[], int correct, int incorrect, int counter)
{
float percent;
float hundred;
int student;
int catcher[20];
int writeCatcher; int starter;
int catcher_size;
student = 0;
writeCatcher = 0;
catcher_size = ((sizeof catcher)/4);
while (counter < 20)
{
if ((CAArray[counter]) == (SAArray[counter]))
{
correct++;
cout << "Good job!" << endl;
} // correct handling
else
{
incorrect++;
cout << "You got question " << counter << " wrong." << endl;
counter >> catcher[writeCatcher];
writeCatcher++;
} // incorrect handling
counter++;
} // while loop to determine if a student got a question right or wrong
static_cast <float> (incorrect); // float conversion
cout << endl; // for cleanliness
percent = ((static_cast <float> (correct))/20); // percentage
hundred = percent * 100;
PassFail(percent);
if (PassFail(percent) == "pass")
{
student++;
cout << "Report for Student " << student << ":" << endl;
cout << "-----------------------------" << endl;
cout << "Missed " << incorrect << " questions out of 20 for ";
cout << hundred << " % correct." << endl << endl;
starter = 0;
while (starter < (sizeof catcher)
{
if(1=1)
{
catcher_size
}
else
{
cout << "";
starter++;
}
}
}
else if (PassFail(percent) == "fail")
{
student++;
cout << "Missed " << incorrect << " questions out of 20 for ";
cout << hundred << " % correct." << endl << endl;
while (starter < catcher_size)
{
if ((catcher[starter]) == -858993460)
{
starter++;
}
else
{
cout << "";
starter++;
}
}
}
return;
}
string PassFail(float percent)
{
if (percent >= 0.70) // if <pass>
{
return "pass";
}
else // if <fail>
{
return "fail";
}
cout << endl;
}
顯示標籤C++沒有C. – fluter
你可以考慮直接返回True或False來通過或失敗並跳過字符串比較。如果您想「讓它看起來更好」,請考慮使用枚舉映射回來並通過失敗。 – ti7