sách gpt4 ai đã đi

c++ - 将 istream 运算符 >> 转换为 istream getline

In lại 作者:行者123 更新时间:2023-11-28 02:29:02 hai mươi bốn 4
mua khóa gpt4 Nike

Tôi đang cố gắng sử dụng istream& getline 而不是使用 istream& operator 从文件中读取行。

结果cpp

#include "Result.h"

Result::Result()
{
Coursename[0] = '\0';
}

Result::Result( const char * nam, unsigned scor )
{
strncpy( Coursename, nam, CourseNameSiz );

score = scor;
}
//istream& getline( istream & input, Result & Re, ',' )
istream & operator >>( istream & input, Result & Re )
{
//getline(Re.Coursename, input, ',');
input >> Re.Coursename >> Re.score;
trả về đầu vào;
}

ostream & operator <<( ostream & os, const Result & Re )
{
os << " Unit Name: " << Re.Coursename << '\n'
<< " Result: " << Re.score << '\n';
return os;
}

int Result::GetScore() const
{
return score;
}

inline void Result::SetScore( unsigned scor )
{
score = scor;
}

结果标题

#ifndef RESULT_H
#define RESULT_H

#include
#include
#include

sử dụng không gian tên std;

const unsigned CourseNameSiz = 10;

class Result
{
công cộng:

Result();

Result( const char * nam, unsigned scor );

int GetScore() const;
// Get the number of s.

void SetScore( unsigned scor );
// Set the number of score.
//string Result:GetCourseName() const;

friend ostream & operator <<( ostream & os, const Result & Re );
//friend istream & getline( istream & input, Result & Re, ',' );
friend istream & operator >>( istream & input, Result & Re );

riêng tư:
char Coursename[CourseNameSiz];
//string Coursename[CourseNameSiz]; // Unit name, C style string
int score; // number of scores
string str;

};
#endif // RESULT_H

这一切都按照我想要的方式工作,但我需要更改它,而不是一次读一个词,我想使用带有分隔符的 istream& getline。代码中的大部分注释是我尝试更改为 getline 方法。我制作了一个字符串数组,作为测试,我试图将读取文件中的第一项放入其中,但显然没有用。

我只是想在这里寻找一点指导。提前致谢

编辑:输入文件:rinput.txt

31525 1 4
JPN_101 3 Japanese, 90,
ICT 4 example1, 91,
TIC 4 example2, 92,
CIT 4 example3, 93

所以在上面写着 example1 和 91 的地方,这是我尝试使用带有定界符的 getline 的两个项目,因为我想在我的读取文件中使用空格

其他信息在不同的类中读取,当我让这个类工作时,我将更改为 getline 方法。

输出文件:routput.txt

Student ID: 31525
Semester: 1
Unit: JPN_101
Credits: 3
Unit Name: Japanese
Result: 90

Unit: ICT
Credits: 4
Unit Name: example1
Result: 91

Unit: TIC
Credits: 4
Unit Name: example2
Result: 92

Unit: CIT
Credits: 4
Unit Name: example3
Result: 93

Number of courses = 4
Total credits = 15

这是我对数组字符使用运算符方法时的输出。

include\Result.h|28|error: expected identifier before ','|

include\Result.h|28|error: expected ',' or '...' before ','|

||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

它指向头文件第 28 行,它是 friend istream & getline( istream & input, Result & Re, ',' );

我相信我只是不明白为什么只是将其更改为字符串数组 (string Coursename[CourseNameSiz];) 然后将 operator 方法更改为 getline 方法不像操作符方法那样只是让它返回输入。

我希望我的解释是正确的,我请人稍微翻译一下以帮助我更好地表述。

Cảm ơn

1 Câu trả lời

这一行

friend istream & getline( istream & input, Result & Re, ',' );

不会编译,因为你有一个常量作为第三个参数而不是参数声明。

在您的情况下,因为您在方法内对 getline 的调用中对 ',' 进行了硬编码,所以根本不需要传入逗号。

friend istream & getline( istream & input, Result & Re);

同样,当你定义函数时,你不需要函数定义中的',':

istream& getline( istream & input, Result & Re)
{
getline(Re.Coursename, input, ',');
trả về đầu vào;
}

请注意,您仍然需要在 getline 的内部调用中使用逗号。

这应该可以修复您列出的编译器错误,如果它不起作用,您仍然需要调试代码。

关于c++ - 将 istream 运算符 >> 转换为 istream getline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508337/

hai mươi bốn 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com