0

以下是问题描述:

您学校的历史老师在对真/假测试评分时需要帮助。学生的 ID 和测试答案存储在一个文件中。文件中的第一个条目包含以下形式的测试答案:

TFFTFFTTTTFFTFTFTFTT

文件中的每个其他条目都是学生 ID,后跟一个空白,然后是学生的回答。例如,条目:

ABC54301 TFTTFTTFTTFTTFTTFTTFT

表示学号为 ABC54301,第 1 题的答案为真,第 2 题的答案为假,以此类推。这位学生没有回答第 9 题。考试有 20 道题,班级有 150 多个学生。每答对一题得两分,答错一题扣一分,不答题得零分。编写一个处理测试数据的程序。输出应该是学生的 ID,然后是答案,然后是考试成绩,然后是考试成绩。假设以下等级等级:90%–100%,A;80%–89.99%,乙;70%–79.99%,C;60%–69.99%,D;和 0%–59.99%,F。

这是我制作的程序:

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

int main()
{
//declaring variables
ifstream file("grading.txt");
char key[21];
char id[9];
char student[21];
int score = 0, i;

//initializing arrays
for(i=0; i<21; i++)
{
    key[i]=0;
    student[i]=0;
}
for(i=0; i<9; i++)
    id[i]=0;

//processing the key
file >> key;
file.ignore(100, "\n");

//processing student grades
while(file.good())
{

    file >> id;
    file.ignore();
    getline(file, student);
    file.ignore(100, "\n");

    //comparing key and student answer
    for(i=0; i<21; i++)
    {
        if(strcmp(student[i], key[i])
            score += 2;
        else
            score -= 1;
    }

    //outputing student id, score and grade
    cout << "Student ID: " << id;
    cout << "Score: " << score;
    score = (score/(40))*100;
    if(score >= 90 && score <= 100)
        cout << "Grade: A" << endl << endl;
    else if(score >= 80 && score <= 89.99)
        cout << "Grade: B" << endl << endl;
    else if(score >= 70 && score <= 79.99)
        cout << "Grade: C" << endl << endl;
    else if(score >= 60 && score <= 69.99)
        cout << "Grade: D" << endl << endl;
    else if(score >= 0 && score <= 59.99)
        cout << "Grade: F" << endl << endl;
    else
        cout << "Invalid percentage" << endl;
}

//closing file
file.close();

return 0;
}

我似乎收到以下编译错误: http: //pastebin.com/r0Y1xX8M(无法在此处正确编辑错误,抱歉)

编译错误以及有关如何解决此问题的任何其他建议将不胜感激。

4

3 回答 3

2

您应该将其'\n'用作分隔符 - 字符常量,而不是字符串文字"\n"

的第二个参数的分隔符ignore是 类型int的,可以隐式转换成字符常量;相反,字符串文字不能转换为int,所以这就是编译器告诉你的。

于 2012-07-05T01:30:07.267 回答
0

始终查找具有源文件和行号的位置。在这种情况下,您可以看到其中几个:

c:\users\haxtor\desktop\projects\lab 8 part 2 prob 6\lab 8 part 2 prob 6\problem 6.cpp(34)

重要的部分是第(34)34 行有问题的说法。在这种情况下,第 34 行的问题是 student 是一个char数组而不是一个字符串,而第 35 行的问题是"\n"应该是'\n'. 可能还有更多问题。尝试找到其他提到行号的地方并自己修复它们。

于 2012-07-05T01:31:07.643 回答
0

你的第一个问题是

file.ignore(100, "\n");

我相信您想使用'\n'单引号而不是双引号。单引号代表一个字符。双引号代表一个数组或字符(一个字符串)

其次,我认为iostream包含一个string类,它封装了字符数组。我相信getline使用那个字符串类。对于使用字符数组,我个人的偏好是fscanf,但您的大部分代码似乎都基于 iostream,因此将字符数组更改为字符串可能是最简单的更改。

此外,您的if(strcmp(student[i], key[i])行正在传递字符而不是字符数组。我相信这if(student[i] == key[i])就是你想要做的。

您的编译错误会告诉您错误在哪一行代码,因此对于其余的代码,您可以查看您的代码并自己考虑问题。

于 2012-07-05T16:59:08.657 回答