gpt4 book ai didi

c++ - 从文件中读取对象并打印

转载 作者:行者123 更新时间:2023-11-28 02:14:18 25 4
gpt4 key购买 nike

我正在写一个关于从文件读取/写入的项目,当我尝试从文件读取对象时遇到同样的问题。我必须打印所有成绩 > 3.00 的学生,但它不打印任何东西!有什么建议吗?附:对不起,我的英语不好 ;)这是我的代码:

#include<iostream>
#include<fstream>
#include<cstring>
#include<algorithm>
#include<queue>
const int MAX_SIZE = 35;

using namespace std;

class Student {

char name[MAX_SIZE];
double mark;
int phone_number;
public:

Student(char const* _name = "anonimous", double = 2, int = 0);
char const* getName() const { return name; }
int getMark() const { return mark; }
int getPhone() const { return phone_number;}
void setMark(double newmark) { mark = newmark; }
void setPhone(int newphone) { phone_number = newphone; }


friend istream& operator>>(istream& is, Student& s);
friend ostream& operator<<(ostream& os, Student const& s);
bool operator>(Student const& s) const;
bool operator<(Student const& s) const;

void mycopy(Student const& s){
strcpy(name,s.getName());
mark = s.getMark();
phone_number = s.getPhone();
}
};


Student::Student(char const* _name, double _mark, int _phone_number)
:mark(_mark), phone_number(_phone_number){
strncpy(name, _name, MAX_SIZE);
name[MAX_SIZE] = '\0';
}

istream& operator>>(istream& is, Student& s){
return (is >> s.phone_number >> s.mark).getline(s.name,MAX_SIZE);
}

ostream& operator<<(ostream& os, Student const& s){
return os << "Name: "<<s.name<<" Grade: "<<s.mark<<" Phone: "<<s.phone_number<<endl;
}

bool Student::operator>(Student const& s) const{
return name > s.getName();
}
bool Student::operator<(Student const& s) const{
return name < s.getName();
}

Student* readStudent(int n){
Student* s = new Student[n];
for(int i=0;i<n;i++){
cin >> s[i];
}
return s;
}

void writeStudent(Student* s, int n){
ofstream fo("my_database.txt");
for(int i=0;i<n;i++){
fo << s[i];
}
}

int main(){
Student* students = new Student[10];
students[0] = Student("Ivan Petrov",4.25,359887954521);
students[1] = Student("Marina Popopva",5.75,359897254521);
students[2] = Student("Petar Ivanov",3.15,359888845723);
students[3] = Student("Stilqn Petrov",2.65,359895745812);
students[4] = Student("Ivelina Veselinova",3.20,359878745861);
students[5] = Student("Margarita Ivanova",4.50,359885421457);
students[6] = Student("Boqn Pavlov",6.00,359898632541);
students[7] = Student("Iliqn Karov",3.00,359878389699);
students[8] = Student("Ivan Dobromirov",4.18,359886574287);
students[9] = Student("Georgi Lubenov",5.61,359885749354);

writeStudent(students,10);

ifstream sf("my_database.txt");
Student s;
while( sf >> s){
if (s.getMark() > 3.00){
cout << s << endl;
}
}

return 0;}

最佳答案

有几个问题:

  • int getMark() const { return mark; }应该返回 double , 不是 int

  • students[0] = Student("Ivan Petrov",4.25,359887954521);您的电话号码大于 int ,如果他们需要以 0 开头怎么办? ?应该是类型 std::string相反

  • 检查您的 ::operator>><< .它们不一致:

你的 operator<< :

ostream& operator<<(ostream& os, Student const& s){
return os << "Name: "<<s.name<<" Grade: "<<s.mark<<" Phone: " <<s.phone_number<<endl;
}

将产生如下所示的数据行:

Name: Ivan Petrov Grade: 4.25 Phone: 359887954521

然后是你的 operator>> :

return (is >> s.phone_number >> s.mark).getline(s.name,MAX_SIZE);

错了。您需要阅读并忽略一些字符串。我会建议你使用 std::string存储名称。这是一个假设这样使用的实现:

istream& operator>>(istream& is, Student& s){
std::string firstName;
std::string lastName;
std::string tmpStr;
is >> tmpStr; // "Name: "
is >> firstName;
is >> lastName;
s.name = firstName + " " + last_name; // consider even storing these in separate fields
is >> tmpStr; // "Grade: "
is >> s.mark;
is >> tmpStr; // "Phone: "
is >> s.phone_number;
return is;
}

其他:

  • Student* students = new Student[10];无需使用新的:Student students[10];

  • 尽量避免将“database.txt”硬编码为输入/输出文件。将文件名作为函数的参数。

关于c++ - 从文件中读取对象并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34501115/

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