cout不会在C ++中打印字符串(包括<iostream>,include <string>,并刷新所有存在的字符串)</string> </iostream>

时间:2013-05-08 13:59:01

标签: c++ string cout

我的代码:
类“table”中有一个数组来保存新添加的书(struct);
在数组中添加一个旁边的一个;
搜索可以使用ISBN,标题或作者,它们都是book(struct)中的变量;
打印应该cout书的信息

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

可能不是问题: 插入,添加...这种功能应该很好用,因为当我搜索一些书时,它会显示“找到书”

#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;
}

1 个答案:

答案 0 :(得分:2)

问题不在于print功能或与之相关的问题。在函数add(以及search)中,您可以按值传递table个对象。只是通过引用传递。

void add(table& t)
//            ^