cout 不会打印C++字符串(包括<iostream>、包含<string>和刷新所有存在的内容)
cout doesn't print string in C++ (include<iostream>, include<string>, and flush all present)
我的代码:
在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)
// ^
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中