我无法按照规范完成作业。这是分配方案:
大学迫切需要一个自动测试评分系统。使用C ++,为大学写一个评分系统,并对至少五名学生的测试进行评分。
要创建评分系统,请按照以下步骤操作:
首先询问测试中的问题数量
然后要求每个问题的正确答案。请注意,多项选择测试和问题将有从A到D的答案。
询问学生人数并通过询问他们的姓名来处理每个学生,然后循环询问学生答案的问题。
为每个问题打分。
在最后一个问题计算出学生得分并显示“学生'插入学生姓名'得分为20分中的10分或50%。”
重复直到所有学生都得分。
在对所有学生进行评分后,以与以前相同的方式插入打印所有学生成绩的班级列表。
这是我到目前为止所做的:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//declare variables
char choice;
string studentName;
vector<char> answers;
vector<string> names;
int getStudents();
int getQuestions();
//calls function to get number of questions
float questions = getQuestions();
//Get answers
for (int i = 0; i < questions; ++i) {
cout << "What is the answer for question " << i + 1 << endl;
cin >> choice;
answers.push_back(choice);
}
//Get number of students
int students = getStudents();
//Get student names
for (int i = 0; i < students; i++) {
cout << "Student " << i + 1 << ", what is your name?" << endl;
cin >> studentName;
names.push_back(studentName);
}
float score = 0;
char studentAnswer;
vector<char> userAnswer;
vector<float> finalScore;
//gets student answers
for (int i = 0; i < students; i++) {
for (int j = 0; j < questions; j++) {
cout << names[i] << ", what is your answer for question " << j + 1 << "?" << endl;
cin >> studentAnswer;
userAnswer.push_back(studentAnswer);
}
}
//calculates student scores
for (int i = 0; i < students; i++) {
for (int j = 0; j < questions; j++) {
if (userAnswer[j] == answers[j])
score = score + 1;
}
finalScore.push_back(score);
}
//outputs scores
for (int i = 0; i < students; i++) {
cout << names[i] << " scored " << finalScore[i] << " out of " << questions <<
" or " << (finalScore[i] / questions) * 100 << "%" << endl;
}
system("pause");
return 0;
}
//function to get number of questions
int getQuestions()
{
int questions;
cout << "How many questions are there?" << endl;
cin >> questions;
return questions;
}
//function to get number of students
int getStudents()
{
int students;
cout << "How many students are there?" << endl;
cin >> students;
return students;
}
最终得分返回的值不准确,我无法找到错误发生的位置。
同样,对于最后一步的排序,我被要求按升序或字母顺序按降序和名称排序。我可以相互独立地对它们进行排序,但不知道如何将它们组合起来并按照这种方式对它们进行排序。
答案 0 :(得分:3)
您将score
初始化为0,但之后您不会为每位学生重置它。你应该这样做:
//calculates student scores
for (int i = 0; i < students; i++) {
score = 0; // here, so that it is reset for each student
顺便提一下,您的questions
变量是float
,我想您想要int
。
答案 1 :(得分:3)
//calculates student scores
for (int i = 0; i < students; i++) {
score = 0; //Fabio was right you need to reset score to 0 for each student
for (int j = 0; j < questions; j++) {
if (userAnswer[j] == answers[j])
score = score + 1;
}
finalScore.push_back(score);
}
当你检查userAnswer
是否等于answers
时,你总是从0开始。所以你每次都要检查学生1的答案。你可以尝试:
if (userAnswer[i*questions+j] == answers[j])