首页 > 解决方案 > 从 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();
        }
    }
}

如果你能帮助我,我会很感激,因为我被困在做什么。

标签: c++

解决方案


  1. bool found = true;在函数的开头是正确的Music::searchrecords,因此查看文件的第一次迭代立即返回。
  2. getline(cin, search).get();连续执行两次,而显然您想读取两个不同的string变量,一个用于艺术家,一个用于歌曲
  3. 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,因此查看文件的第一次迭代从函数返回(您没有完全读取文件)

  1. 你打电话
myfile.close();
displayfile();

在每次迭代中,所以即使你没有if上面的权利,那么你的程序无论如何都会崩溃,因为你关闭了循环中的文件它正在迭代它。

  1. 你不用比较line来检查你是否确实找到了你的歌

总的来说,您的程序根本无法运行,而且 StackOverflow 也不是一个可以代替您完全编写整个程序的论坛。所以你最好在你的 IDE 中使用调试器来一步步地查看你的程序做了什么,或者拿一张纸和一支笔来写下你将如何完成你的任务


推荐阅读