如何显示具有相同信息的多个项目
How to show multiple items with the same information?
我正在编写一个程序,它可以显示来自学生列表的信息。我想知道我怎样才能使程序打印出所有共享相同信息的学生。例如:有两个学生都住在伦敦,当我输入"伦敦"时,程序只打印出它在列表中找到的第一个学生。下面是我当前的代码:
void Person_list::findAddr()
{
Person *s;
string addr;
string temp_addr;
s = head;
cout << "Please enter student's address: ";
getline(cin, addr);
while ((s!=NULL) && (s-> Get_addr() != addr))
{
s = s -> next;
}
if (s != NULL)
{
s -> Show();
}
if (s == NULL)
{
cout << "Cant find. n" << endl;
}
}
你的问题是while循环的条件:
while ((s!=NULL) && (s-> Get_addr() != addr))
它说:只要s不为空,地址不是你要找的那个,看看下一个学生。
在第一次查找时,循环终止,因此后续可能的查找永远不会到达。
你可以这样做:
while (s != nullptr) {
if (s-> Get_addr() == addr) {
s -> Show();
}
s = s -> next;
}
遍历所有学生,只在地址匹配的情况下打印。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 如果我尝试将对话框从一个项目移动到另一个项目,我是否需要从 rc 文件中复制 DESIGNINFO、对话框信息和AFX_
- 资源管理器在 TILE 视图模式下不显示 shell 命名空间扩展的项目详细信息
- Qt-检测QListView中的项目信息更改
- 发布没有调试信息警告的项目
- 其中是调试信息(断点等)存储在本机C++dll项目VS2013中的位置
- 如何创建一个具有全局变量的 lib 文件,这些全局变量应该将其信息共享给另一个项目
- 如何显示具有相同信息的多个项目
- 从混合模式c++项目中的CLR-to-SEH异常中获取明智的信息
- 如何将Visual Studio . net 2003项目构建信息转换为VS命令提示符
- 添加资源.Rc文件信息项目
- Visual Studio 2012的智能感知和浏览信息将无法用于c++项目