程序挑战的正确答案百分比
Percent of correct answers program challenge
我的程序输出有问题,txt文件显示学生答错了3个答案,但它一直给我0%的正确答案。
我面临的挑战是:
"你的一位教授让你写一个程序来给她的期末考试打分,只有20道选择题。每个问题都有四个可能的答案之一:A、B、C或D。文件CorrectAnswers.txt包含所有的问题,每个答案都写在一行。第一行包含第一个问题的答案,第二行包含第二个问题的回答,依此类推。编写一个程序,将CorrectAnswers.txt文件的内容读取为字符数组,然后将包含学生答案的另一个文件的内容读入第二个字符数组。
该程序应确定学生错过,然后显示以下内容:
•学生遗漏的问题列表,显示正确答案和学生为每个遗漏的问题提供的错误答案
•遗漏的问题总数
•正确回答问题的百分比。这可以计算为正确回答的问题÷问题总数
•如果正确回答问题的百分比为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;
}
整数除法除完美分数外,所有结果都为0。改用浮点除法:
int percent = ((double)(20-count) / 20) * 100;
请注意,(double)(20-count)
将值(20-count)
强制转换为双精度浮点数。一旦对整个表达式求值,它就会被强制返回为一个整数,因为您要将值赋给int
。
整数除法总是向零取整,因此如果count大于0,则(20 - count)/20
将为零。
不需要浮点,这就可以了:
int percent = 5 * ( 20 - count );
相关文章:
- 欧拉项目#8答案是大以获得有效答案
- 首要问题的答案让值班员搞错了
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 编写一个函数以使用 n 百分比的 CPU 使用率
- 使用 bfs 解决连接组件问题时得到错误的答案
- 同一对象的"sizeof"的不同答案
- 递归求和任务的错误答案
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 为什么C++程序在太大时返回代码而不是答案?
- 不理解为什么代码没有产生所需的答案
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 获取 2 个数字之间的差异百分比
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- C++ 中 pow() 函数的输出没有给出准确的答案
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- 给出不精确答案的递归Karatsuba算法
- 程序挑战的正确答案百分比