cout 不会打印C++字符串(包括<iostream>、包含<string>和刷新所有存在的内容)

cout doesn't print string in C++ (include<iostream>, include<string>, and flush all present)

本文关键字:lt gt 刷新 存在 string 包含 打印 包括 字符串 cout iostream      更新时间:2023-10-16

我的代码:
在table类中有一个数组用来存放新添加的book (struct);
在数组中一个接一个地放一个;
搜索可以使用ISBN、标题或作者,它们都是book (struct);
中的变量。Print应该计算书的信息

问题:print不能打印字符串(书中的变量都是字符串)

可能不是问题:插入、添加…这种函数应该工作得很好,因为当我搜索一些书时,它会显示"book found"

#include<iostream>
#include<string>
using namespace std;
struct book
{
    string isbn;
    string title;
    string author;
    string date;
};
class table
{
public:
    //member constant
    static const size_t CAPACITY = 30;
    //constructor
    table() {used = 0;}
    //modification
    bool insert(book entry);
    //constant
    size_t hash_isbn(string target_isbn);
    size_t hash_title(string target_title);
    size_t hash_author(string target_author);
    size_t search_isbn(string target_isbn);
    size_t search_title(string target_title);
    size_t search_author(string target_author);
    void print(size_t index);
private:
    //member variables
    book data[CAPACITY];
    size_t used;
};
//modification member functions
bool table::insert(book entry)
{
    if(search_isbn(entry.isbn))
        return false;
    data[used] = entry;
    used++;
    return true;
}
//constant member functions
size_t table::hash_isbn(string target_isbn)
{
    size_t index = 0;
    bool found = false;
    while((index < used) && (!found))
    {
        if(data[index].isbn == target_isbn)
        {
            found = true;
            continue;
        }
        index ++;
    }
    if(!found)
        index = -1;
    return index;
}
size_t table::hash_title(string target_title)
{
    size_t index = 0;
    bool found = false;
    while((index < used) && !found)
    {
        if(data[index].title == target_title)
        {
            found = true;
            continue;
        }
        index ++;
    }
    if(index == used)
        index = -1;
    return index;
}
size_t table::hash_author(string target_author)
{
    size_t index = 0;
    bool found = false;
    while((index < used) && !found)
    {
        if(data[index].author == target_author)
        {
            found = true;
            continue;
        }
        index ++;
    }
    if(index == used)
        index = -1;
    return index;
}
size_t table::search_isbn(string target_isbn)
{
    return hash_isbn(target_isbn)+1;
}
size_t table::search_title(string target_title)
{
    return hash_isbn(target_title)+1;
}
size_t table::search_author(string target_author)
{
    return hash_isbn(target_author)+1;
}
void table::print(size_t index)
{
    cout.flush();
    cout<<data[index].title<<endl;
    cout<<"Title: "<<data[index].title<<endl;
    cout<<"ISBN: "<<data[index].isbn<<endl;
    cout<<"Author: "<<data[index].author<<endl;
    cout<<"Publication data: "<<data[index].date<<endl;
    cout<<endl;
}
//nonmember functions
void add(table t)
{
    book entry;
    cout<<"Enter author name:"<<endl;
    cin>>entry.author;
    cout<<endl;
    cout<<"Enter book name:"<<endl;
    cin>>entry.title;
    cout<<endl;
    cout<<"Enter ISBN:"<<endl;
    cin>>entry.isbn;
    cout<<endl;
    cout<<"Enter the publication data:"<<endl;
    cin>>entry.date;
    cout<<endl;
    if(t.search_isbn(entry.isbn))
        cout<<"==== The book already exists !!! ==="<<endl;///////////////////////输入重复时,此处并未执行
    else
        t.insert(entry);
}
void search(table t)
{
    string option;
    cout<<"Seach by ISBN (I), book title (T), or author (A). Choice: ";
    cin>>option;
    cout<<endl;
    while((option != "I") && (option != "T") && (option != "A"))
    {
        cout<<"Not an accessible option, try again:"<<endl
            <<"Seach by ISBN (I), book title (T), or author (A). Choice: ";
        cin>>option;
        cout<<endl;
    }
    size_t index;
    if(option == "I")
    {
        string target_isbn;
        cout<<"Enter ISBN: ";
        cin>>target_isbn;
        cout<<endl;
        index = t.search_isbn(target_isbn);
    }
    if(option == "T")
    {
        string target_title;
        cout<<"Enter Title: ";
        cin>>target_title;
        cout<<endl;
        index = t.search_isbn(target_title);
    }
    if(option == "A")
    {
        string target_author;
        cout<<"Enter Author: ";
        cin>>target_author;
        cout<<endl;
        index = t.search_isbn(target_author);
    }

    if(index+1)
    {
        cout<<"Book found"<<endl;
        t.print(index);
    }
    else
        cout<<"==== The book does not exist !!! ==="<<endl;
}
int main()
{
    table hash_table;
    string action;
    bool done = false;
    while(!done)
    {
        cout<<"Add a new book (A), search (S), or end program (E)? ";
        cin>>action;
        cout<<endl;
        while((action != "A") && (action != "S") && (action != "E"))
        {
            cout<<"Not an accessible option, try again:"<<endl
                <<"Add a new book (A), search (S), or end program (E)? ";
            cin>>action;
            cout<<endl;
        }
        if(action == "A")
            add(hash_table);
        if(action == "S")
            search(hash_table);
        if(action == "E")
        {
            done = true;
            continue;
        }
    }
    hash_table.print(0); // this code just try to test my problem in a simple way

    system("pause");
    return 0;
}

问题不在于print功能或与之相关的东西。在函数add(和search也是)你传递table对象的值。参考传递。

void add(table& t)
//            ^