正确答案程序挑战的百分比

Percent of correct answers program challenge

我的程序输出有问题,txt 文件显示学生答错了 3 个答案,但它一直给我 0% 的正确答案百分比。

给我的挑战是:

"One of your professors has asked you to write a program to grade her final exams, which consist of only 20 multiple-choice questions. Each question has one of four possible answers: A, B, C, or D. The file CorrectAnswers.txt contains the correct answers for all of the questions, with each answer written on a separate line. The first line contains the answer to the first question, the second line contains the answer to the second question, and so forth. Write a program that reads the contents of the CorrectAnswers.txt file into a char array, and then reads the contents of another file, containing a student’s answers, into a second char array.

The program should determine the number of questions that the student missed, and then display the following:

• A list of the questions missed by the student, showing the correct answer and the incorrect answer provided by the student for each missed question

• The total number of questions missed

• The percentage of questions answered correctly. This can be calculated as Correctly Answered Questions ÷ Total Number of Questions

• If the percentage of correctly answered questions is 70% or greater, the program should indicate that the student passed the exam. Otherwise, it should indicate that the student failed the exam.

这是我目前的代码,在此先感谢您的任何建议!

#include <iostream> 
#include <fstream>
#include <string> 
using namespace std;

int main()
{
const int size=20;
static int count=0;
string correctAnswers[size];
string studentAnswers[size];
ifstream inFileC;

inFileC.open("c:/Users/levi and kristin/Desktop/CorrectAnswers.txt");

if (inFileC)
{
for (int i=0;i<20;i++)
{
    inFileC>>correctAnswers[i];
}
}
else
{
    cout<<"Unable to open \"CorrectAnswers.txt\""<<endl;
    cout<<"Please check file location and try again."<<endl<<endl;
}
inFileC.close();

ifstream inFileS;
inFileS.open("c:/Users/levi and kristin/Desktop/StudentAnswers.txt");

if (inFileS)
{
for (int t=0;t<20;t++)
{
    inFileS>>studentAnswers[t];
}
}
else
{
    cout<<"Unable to open \"StudentAnswers.txt\""<<endl;
    cout<<"Please check file location and try again."<<endl<<endl;
}
inFileS.close();

for (int k=0;k<20;k++)
{
    if (correctAnswers[k]!=studentAnswers[k])
    {
        cout<<endl<<"Correct Answer: "<<correctAnswers[k];
        cout<<endl<<"Student Answer: "<<studentAnswers[k]<<endl;
        count++;
    }
}
int percent=((20-count)/20)*100;

cout<<endl<<"Number of missed questions: "<<count;
cout<<endl<<"Percent of correctly answered questions: "<<percent<<"%";

if (percent>=70)
{
    cout<<endl<<endl<<"********"<<endl<<"**Pass**"<<endl<<"********"<<endl<<endl;
}
else
{
    cout<<endl<<endl<<"********"<<endl<<"**Fail**"<<endl<<"********"<<endl<<endl;
}
return 0;
}

除满分外,整数除法除以 0。改用浮点除法:

int percent = ((double)(20-count) / 20) * 100;

请注意,(double)(20-count) 将值 (20-count) 转换为双精度浮点数。对整个表达式求值后,它会被强制转换回整数,因为您将值分配给 int.

整数除法总是向零舍入,因此如果计数大于 0,(20 - count)/20 将为零。

不需要浮点数,这样就可以了:

int percent = 5 * ( 20 - count );