我的主要问题是我很难将包含数组的函数连接到主函数。
您学校的历史老师在对真/假测验进行评分时需要帮助。学生的ID和测试答案存储在文件中。文件中的第一项包含以下形式的测试答案:
TFFTFFTTTTFFTFTFTFTT
文件中的其他所有条目都是学生ID,后跟空白,然后是学生的回答。例如,条目:
ABC54301 TFTFTFTT TFTFTFFTTFT
指示学生ID为 ABC54301 ,问题1
的答案为是,问题2
的答案为否,依此类推。该学生未回答问题9
。考试有20
个问题,全班学生超过150
个。每个正确答案将得到2分,每个错误答案将得到1分,没有答案将得到0分。编写一个处理测试数据的程序。输出应该是学生的ID,然后是答案,然后是测试成绩,然后是测试成绩。假定以下等级量表:
90%–100%,A; 80%–89.99%,B; 70%–79.99%,C; 60%–69.99%,D;和0%–59.99%,F。
// Chap9BBProg.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int stux;
char stuGrade;
int correctAnswers(char[], char[]);
char studentGrade(int score);
char ansKey[10];
char stuA[10];
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("TFInput.txt");
outFile.open("TFOutput.txt");
double score;
char grade;
string key;
string studentID;
string stuAnswers;
getline(inFile, key);
outFile << "The correct answers are " << key << endl << endl;
while (getline(inFile, studentID, ' '))
{
outFile << studentID << " ";
getline(inFile, stuAnswers);
stux = studentGrade(stux);
outFile << " " << stuAnswers << endl;
}
return 0;
}
int correctAnswers(char answerKey[], char studentAnswers[])
{
int i;
int tempscore;
for (i = 0; i < 22; i++)
{
if (answerKey[i] == studentAnswers[i])
{
tempscore += 2;
}
else if (studentAnswers[i] == ' ')
{
tempscore += 0;
}
else
{
tempscore -= 1;
}
}
cout << tempscore << endl;
return tempscore;
}
char studentGrade(int x)
{
int i;
double score = 0;
char grade = ' ';
score = x / 40.0 * 100;
for (i = 0; i < 30; i++)
{
if (score >= 90)
grade = 'A';
else if (score < 90 && score > 79)
grade = 'B';
else if (score <= 79 && score > 69)
grade = 'C';
else if (score <= 69 && score > 60)
grade = 'D';
else if (score <= 59)
grade = 'F';
}
return grade;
}
答案 0 :(得分:2)
例如在函数correctAnswers()中发现一些小问题,未初始化变量tempscore,并且在char []和字符串之间发现函数参数冲突。
int stux;
char stuGrade;
int correctAnswers(string, string);
char studentGrade(int score);
char ansKey[10];
char stuA[10];
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("TFInput.txt");
outFile.open("TFOutput.txt");
double score;
string key;
string studentID;
string stuAnswers;
getline(inFile, key);
outFile << "The correct answers are " << key << endl << endl;
while (getline(inFile, studentID, ' '))
{
outFile << studentID << " ";
getline(inFile, stuAnswers);
score = correctAnswers(key, stuAnswers); //Changed here
stuGrade = studentGrade(score); //Changed here
outFile << " Score: " << score <<" Grade: " << stuGrade << endl; //Changed here
}
return 0;
}
int correctAnswers(string answerKey, string studentAnswers) //Changed here Array to string
{
int i;
int tempscore = 0; //Changed here Initialized to 0
for (i = 0; i < 21; i++) //Changed 22 to 21 here
{
if (answerKey[i] == studentAnswers[i])
{
tempscore += 2;
}
else if (studentAnswers[i] == ' ')
{
tempscore += 0;
}
else
{
tempscore -= 1;
}
}
cout << tempscore << endl;
return tempscore;
}
char studentGrade(int x)
{
int i;
double score = 0;
char grade = ' ';
score = x / 40.0 * 100;
if (score >= 90)
grade = 'A';
else if (score < 90 && score > 79)
grade = 'B';
else if (score <= 79 && score > 69)
grade = 'C';
else if (score <= 69 && score > 60)
grade = 'D';
else if (score <= 59)
grade = 'F';
return grade;
}