如何在 中输入记录的一个字段时搜索记录
How to search for a record when one field of the record in entered in c++
我试图在循环文件时将字符串输入与函数返回的字符串进行比较,如果输入等于文件中返回的字符串,它应该打印与输入字符串相关的详细信息。但是它一直给我错误消息invalid use of void expression
...
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
struct student { /* ... */ };
void search_course(string course)
{
int check = 0;
student st;
ifstream file;
file.open("student.dat", ios::binary | ios::out);
if (!file) {
cout << "No Such file in Database...";
return;
}
while (!file.eof() && check == 0) {
file.read((char *) &st, sizeof(student));
if (strcmp(st.return_prog(), course) == 0) {
st.showid_detail();
}
}
file.close();
}
int main()
{
string course = "physics";
search_course(course);
}
showid_detail()
是类中的一个函数,它显示有关记录的信息,如下所示。 id, index, name, course, year, Hall
return_prog()
是一个void
函数,用于输出学生的课程我期望代码显示所有那些做物理的学生,但它一直告诉我无效地使用空表达式......
return_prog(( 是一个 void 函数,用于输出学生的课程
所以有你的问题。
它不会返回学生的课程,而是输出它。
您尝试在表达式中使用它strcmp(st.return_prog(), course)
就好像它具有返回类型和值一样,但它没有。 strcmp
不会神奇地检查操作数生成的控制台输出;它观察操作数的值。
我建议这样做。然后,如果您想要输出,您仍然可以在某个地方执行std::cout << st.return_prog()
。
相关文章:
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 如何在 中输入记录的一个字段时搜索记录
- 在添加新记录和访问记录时有一个问题
- 在其他班级成员的向量中找到一个分类记录
- 我如何用doxygen记录一个富特枚举
- sqlite3带有参数的多个插入物;仅插入一个记录
- 是否有一个工具来记录代码执行
- 一个简单的PC应用程序制造商的数据记录应用程序
- 我只需要从文件中提取每条记录的一个字段,但它不起作用
- 一个方便的C++日志记录语句,使用boost::格式
- 制作一个简单的c++键盘记录程序
- 在Qsort、c或c++中使用一个记录数组
- 创建一个二进制文件,向其中添加信息,并从中读取所有记录(电话簿)
- 通过c++将记录从一个数据库的表复制到另一个数据库的同名表
- Sphinx和记录python从一个swigge c++ api
- Python的氧:一个段落记录文件和包
- 在一个向量中搜索一系列记录
- 如果我有一个用户记录在一行内如何找到登录的数据
- 如何获得具有相同姓氏的下一个记录
- 实现一个登录函数或宏,可以记录日期和函数名