gpt4 book ai didi

C++ - 文件 I/O 和字符数组

转载 作者:行者123 更新时间:2023-11-28 03:34:42 28 4
gpt4 key购买 nike

以下是问题描述:

The history teacher at your school needs help in grading a True/False test. The students’ IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTTFFTFTFTFTT

Every other entry in the file is the student ID, followed by a blank, followed by the student’s responses. For example, the entry:

ABC54301 TFTFTFTT TFTFTFFTTFT

indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted, and no answer gets zero points. Write a program that processes the test data. The output should be the student’s ID, followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scale: 90%–100%, A; 80%–89.99%, B; 70%–79.99%, C; 60%–69.99%, D; and 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 (无法在此处正确编辑错误,抱歉)

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

最佳答案

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

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

关于C++ - 文件 I/O 和字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336880/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com