cout doesn't print string in C++ (include<iostream>, inlcude<string>, and flush all present) -
hi thank , let me try make simple :)
my code:
there array in class "table" hold newly added book (struct);
add put in 1 next 1 in array;
search can use isbn, title or author, variables in book (struct);
print supposed cout info of book
problem: print cant print string (variable in book string)
may not problem: insert,add...this kind of function should work because when search book, shows "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 publication data:"<<endl; cin>>entry.date; cout<<endl; if(t.search_isbn(entry.isbn)) cout<<"==== book exists !!! ==="<<endl;///////////////////////输入重复时,此处并未执行 else t.insert(entry); } void search(table t) { string option; cout<<"seach isbn (i), book title (t), or author (a). choice: "; cin>>option; cout<<endl; while((option != "i") && (option != "t") && (option != "a")) { cout<<"not accessible option, try again:"<<endl <<"seach 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<<"==== book not exist !!! ==="<<endl; } int main() { table hash_table; string action; bool done = false; while(!done) { cout<<"add new book (a), search (s), or end program (e)? "; cin>>action; cout<<endl; while((action != "a") && (action != "s") && (action != "e")) { cout<<"not accessible option, try again:"<<endl <<"add 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); // code try test problem in simple way system("pause"); return 0; }
the problem not print
function or related it. in function add
(and search
too) pass table
object value. pass reference.
void add(table& t) // ^
Comments
Post a Comment