2013-05-08 19 views
-3

你好謝謝你,讓我儘量讓這個簡單的:)COUT不打印在C字符串++(包括<iostream>,inlcude <string>,並刷新所有存在)

我的代碼:
有一個數組類「表」來保存新添加的書(結構);
只加一個放在數組中的一個旁邊;
搜索可以使用ISBN,標題或作者,它們都是book(struct)中的所有變量;
打印應該清點書

的信息問題:打印着打印字符串(在本書變量都是字符串)

可能不會有問題: 插入,加...這種功能應該工作得很好,因爲當我搜索了一些書,顯示「找到書」

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

爲什麼你發佈代碼牆,當你真的應該只發布'print'函數,也許是類聲明? – 2013-05-08 14:01:29

+0

*「打印不能打印字符串」*請給出具體的細節。哪個打印?它是否編譯?你在期待什麼?它做什麼呢? – 2013-05-08 14:06:02

回答

2

的問題不在於print功能或某事與它有關。在功能add(和search太)你通過價值table對象。只需通過參考。

void add(table& t) 
//   ^
+0

非常感謝! – 2013-05-08 14:33:51

相關問題