c++ - 从 C++ 文件中搜索艺术家姓名和歌曲
问题描述
我已经为C++中的搜索功能编写了一些代码来查找艺术家姓名和歌曲。我的代码似乎不起作用。一条消息显示找到了艺术家和歌曲,然后菜单无限次重复。
我的完整程序代码如下。
#include "Music.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void Music::menu()
{
int input;
do {
cout << "Select an option from the menu" << endl;
cout << "1. Display records" << endl;
cout << "2. Search for records" << endl;
cout << "3. Writing records to a file" << endl;
cin >> input;
switch (input) {
case 1:
displayfile();
break;
case 2:
searchrecords();
break;
case 3:
writefile();
break;
default:
cout << "Invalid option entered. Try again" << endl;
menu();
}
} while (input != 4);
}
void Music::displayfile()
{
string line;
ifstream myfile;
myfile.open("Music.txt");
if (myfile.is_open()) {
while (getline(myfile, line)) {
cout << line << endl;
}
myfile.close();
}
else {
cout << "Unable to open file" << endl;
}
}
void Music::writefile()
{
ofstream write;
write.open("Music.txt", ofstream::app);
if (write.is_open()) {
cin.ignore();
cout << "Enter a title of song to the playlist" << endl;
cin.getline(title, 255).get();
write << " " << title;
cout << "Enter the artist of the song to playlist" << endl;
cin.getline(artist, 255).get();
write << " " << artist;
cout << "Enter the year of the song" << endl;
cin >> year;
write << "" << year;
cout << "Enter the duration of the song" << endl;
cin >> duration;
write << " " << duration;
cout << "Writing to a file is successful" << endl;
}
else {
cout << "File couldn't be open" << endl;
}
write.close();
displayfile();
}
bool Music::searchrecords()
{
bool found = true;
string search;
string line;
ifstream myfile;
myfile.open("Music.txt");
cout << "Enter the artist you want to search for: " << endl;
getline(cin, search).get();
cout << "Enter for the song you want to search for: " << endl;
getline(cin, search).get();
if (myfile.is_open()) {
while (!myfile.eof()) {
if (found) {
cout << "The artist is found" << search << endl;
cout << "The song is found" << search << endl;
return found;
}
else {
cout << "The artist is not found" << endl;
return false;
}
getline(myfile, line);
myfile.close();
displayfile();
}
}
}
如果你能帮助我,我会很感激,因为我被困在做什么。
解决方案
- 您
bool found = true;
在函数的开头是正确的Music::searchrecords
,因此查看文件的第一次迭代立即返回。 - 您
getline(cin, search).get();
连续执行两次,而显然您想读取两个不同的string
变量,一个用于艺术家,一个用于歌曲 if
你在每次迭代时都用这个循环
if (found) {
cout << "The artist is found" << search << endl;
cout << "The song is found" << search << endl;
return found;
}
else {
cout << "The artist is not found" << endl;
return false;
}
在这两种情况下,您都通过调用来终止函数return
,因此查看文件的第一次迭代从函数返回(您没有完全读取文件)
- 你打电话
myfile.close();
displayfile();
在每次迭代中,所以即使你没有if
上面的权利,那么你的程序无论如何都会崩溃,因为你关闭了循环中的文件,它正在迭代它。
- 你不用比较
line
来检查你是否确实找到了你的歌
总的来说,您的程序根本无法运行,而且 StackOverflow 也不是一个可以代替您完全编写整个程序的论坛。所以你最好在你的 IDE 中使用调试器来一步步地查看你的程序做了什么,或者拿一张纸和一支笔来写下你将如何完成你的任务
推荐阅读
- ios - SWIFT 4 中 UITableViewCell 上的内存管理问题谷歌地图
- python - 在哪里可以找到 docplex 自动调优工具的文档?
- c++ - 重载一个接受右值引用的类构造函数
- tensorflow - 如何在 Tensorflow 中投影我的 word2vec 模型
- python - 如何在 python 中使用 pyshark 通过 IP 读取数据?
- javascript - 如何从模态中获取数据?
- angular - 在 docker 多阶段构建期间选择 angular.json 配置
- python - Python Socket 编程:从客户端上传文件到服务器
- php - 无法在 Laravel 5.1.46 中启用中间件 CORS
- php - 分页 - 如何不允许用户进一步浏览现有页面?