首页 文章

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

提问于
浏览
0

我的程序输出有问题,txt文件显示学生错误地得到了3个答案,但它仍然给我0%的正确答案百分比 .

我给出的挑战是:

“你的一位教授要求你写一个程序来评估她的最终考试,这些考试只包含20个多项选择题 . 每个问题都有四个可能的答案之一:A,B,C或D.文件正确答案.txt包含所有问题的正确答案,每个答案都写在一个单独的行上 . 第一行包含第一个问题的答案,第二行包含第二个问题的答案,依此类推 . 编写程序将CorrectAnswers.txt文件的内容读入char数组,然后将包含学生答案的另一个文件的内容读入第二个char数组 . 程序应确定学生错过的问题数,然后显示以下内容:•学生错过的问题列表,显示正确的答案以及学生针对每个错过的问题提供的错误答案•错过的问题总数•问题答案的百分比正确答案 . 这可以计算为正确答案问题÷问题总数•如果正确答案问题的百分比是70%或更高,程序应指示学生通过了考试 . 否则,它应表明学生未通过考试 .

这是我到目前为止的代码,提前感谢任何建议!

#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;
}

3 回答

  • 0

    除了满分之外,整数除法将为所有内容产生0 . 改为使用浮点除法:

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

    请注意, (double)(20-count) 将值 (20-count) 转换为双精度浮点数 . 一旦评估了整个表达式,它就会被强制回一个整数,因为你要将值赋给 int .

  • 2

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

  • 0

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

    int percent = 5 * ( 20 - count );
    

相关问题