【问题标题】:how to have conditions in displaying a text from a text file using c++如何在使用 C++ 显示文本文件中的文本时有条件
【发布时间】:2021-04-04 06:12:19
【问题描述】:

我有这个学校项目,我有这个文本文件:

VIDEO ID        : 1
MOVIE TITLE     : TITANIC
GENRE           : ROMANCE
PRODUCTION      : PARAMOUNT PICTURES
NUMBER OF COPIES: 3
VIDEO ID        : 2
MOVIE TITLE     : IT
GENRE           : HORROR
PRODUCTION      : NEW LINE CINEMA
NUMBER OF COPIES: 0
VIDEO ID        : 3
MOVIE TITLE     : HARRY POTTER
GENRE           : FANTASY
PRODUCTION      : WARNER BROS. PICTURES
NUMBER OF COPIES: 10

当这样搜索时程序应该显示电影:

Enter movie ID: 1 //user input
VIDEO ID        : 1
MOVIE TITLE     : TITANIC
GENRE           : ROMANCE
PRODUCTION      : PARAMOUNT PICTURES
NUMBER OF COPIES: 3
AVAILABILITY    : AVAILABLE

或者这个:

Enter movie ID: 2 //user input
VIDEO ID        : 2
MOVIE TITLE     : IT
GENRE           : HORROR
PRODUCTION      : NEW LINE CINEMA
NUMBER OF COPIES: 0
AVAILABILITY    : NOT AVAILABLE

我的问题是,如果副本数等于或大于 1,我不知道如何在需要说“可用”的地方显示可用性部分,否则如果为 0,它将显示“不可用” .

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream file("test.txt");
    string line;
    int find = false;
    string id;

    cout << "Enter movie ID: ";
    cin >> id;
    string idnum = "VIDEO ID    : " + id;
    cout << idnum << endl;

    while (file.good())
    {
        getline(file,line);
        if(line==idnum){
            find=true;
            for(int i=0;i<=3;i++){
                    getline(file,line);
                    cout << line << endl;
                 }
            break;
        }
    }
    file.close();

    if(find!=true){
        cout << "Movie Not Found" << endl;
    }
    return 0;}

非常感谢任何愿意提供帮助的人:)

【问题讨论】:

  • 文件是否包含字段名称,例如VIDEO ID :MOVIE TITLE : ...甚至AVAILABILITY :?这似乎没有必要。
  • @TedLyngmo 是的。它在文本文件中
  • AVAILABILITY : AVAILABLE 是否已经存在于文本文件中,或者您是否必须将其单独打印到控制台?如果是前者,你可以直接打印出来,根本不用检查份数。
  • @Ruks 不,它不存在于文本文件中。可用性部分应仅在读取文本文件后显示在程序中
  • @Noran 那么请展示文本文件中的实际内容。添加两个电影条目,以便我们了解它们是如何存储的。

标签: c++ file text


【解决方案1】:

我会创建一个class 来存储有关一部电影的信息,并添加一个成员函数以便能够从istream 中读取一部电影(如ifstream)。由于该文件也包含字段名称,因此我还将添加一个函数来删除该部分。示例:

#include <sstream> // std::istringstream

struct Movie {
    int id;
    std::string title;
    std::string genre;
    std::string production;
    int number_of_copies;

    // Read a line and strip away the field name and put the result in an istringstream
    // for further processing.
    std::istringstream strip(std::istream& is) {
        std::istringstream iss;
        std::string line;
        if(std::getline(is, line)) {
            if(line.size() >= 18) iss.str(line.substr(18));
        }
        return iss;
    }

    // A function to read the fields from an istream using the above strip function.
    std::istream& read(std::istream& is) {
        strip(is) >> id;
        std::getline(strip(is), title);
        std::getline(strip(is), genre);
        std::getline(strip(is), production);
        strip(is) >> number_of_copies;
        return is;
    }

    // A function to print the information about one movie
    std::ostream& print(std::ostream& os) {
        os << "VIDEO ID        : " << id << '\n'
           << "MOVIE TITLE     : " << title << '\n'
           << "GENRE           : " << genre << '\n'
           << "PRODUCTION      : " << production << '\n'
           << "NUMBER OF COPIES: " << number_of_copies << '\n'
           << "AVAILABILITY    : "
           // here availability depends on if there are more than zero copies in store
           << (number_of_copies > 0 ? "AVAILABLE" : "NOT AVAILABLE") << '\n';
        return os;
    }
};

有了class,您可以一次阅读一部电影,检查它是否有正确的id,然后打印您找到的电影。

int main() {
    std::ifstream file("test.txt");

    std::cout << "Enter movie ID: ";
    int id;
    std::cin >> id;

    bool found = false; // bool, not int
    Movie mov;
    while(mov.read(file)) { // loop for as long as a movie can be successfully read
        if(mov.id == id) {
            found = true;
            mov.print(std::cout);
            break;
        }
    }
    if(not found) {
        std::cout << "Movie Not Found\n";
    }
}

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2012-07-02
    相关资源
    最近更新 更多