如何删除已经输入的书,如何让书名、语言、书名加空格不出错? [英] How to delete the book that have been input and how to make the title, language, and name doesn't error if we put space on it?

查看:67
本文介绍了如何删除已经输入的书,如何让书名、语言、书名加空格不出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我制作一个新菜单来删除所有已输入的书籍?以及如何让标题、姓名、语言可以输入空格?我已经搜索了关于它的其他问题,其中许多使用 getline.但我不明白如何在这样的课堂上使用它.

Can anyone help me to make a new menu to delete all the books that have been entered? And how to make the title, name, and language can be entered with space? I've searched other questions about it, many of them using getline. But i don't understand how to use it on class like this.

(对不起,我的语法不好,我英语不是很好)

(Sorry my grammar is bad, i'm not very good in English)

这是我制作的源代码.

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

class Book {
    int number, year;
    string language, name, title;
    Book * head, * next;
public:
Book (string & name, string & title, int number, string & language, int year) {
head = NULL;
this -> name = name;
this -> title = title;
this -> language = language;
this -> number = number;
this -> year = year;
};
~ Book (void) {
    delete head;
};
void display (void);
void add (void);
void dellete (string&);
};

void Book :: add (void) {
string name, title, language;
int year, number;

cout << endl << "Author:", cin >> name;
cout << "Title:", cin >> title;
cout << "Number of books:", cin >> number;
cout << "Language:", cin >> language;
cout << "Year of publication:", cin >> year;

Book * p = new Book (name, title, number, language, year);
p -> next = head;
head = p;
}

void Book :: display (void) {
Book * p = head;
while (p) {
    cout << "----------------------------- \n";
    cout << "Author:" << p -> name << endl;
    cout << "Title:" << p -> title << endl;
    cout << "Number of books:" << p -> number << endl;
    cout << "Language:" << p -> language << endl;
    cout << "Year of publication:" << p -> year << endl;
    cout << endl;
    p = p -> next;
}
}

int main (int argc, char const ** argv) {

string blank = "";
Book * B = new Book (blank, blank, false, blank, 0);
int opt;
cout << "\nBOOK STACKS \n";
for (;;) {
    cout << "1) Add a book.\n";
    cout << "2) Show all books.\n";
    cout << "3) Exit. \n\n";

    cout << "Options:", cin >> opt;

    switch (opt) {
            case 1:
                B -> add ();
            break;
            case 2:
                B -> display ();
            break;
            case 3:
                exit (0);
            default:
            continue;
    }
}

return 0;
}

请帮我获取代码,因为这是我的中期测试任务,而且我仍然是编程的初学者.谢谢.

Please help me to get the code because it's my mid-test task and i'm still a beginner at programming. Thanks.

推荐答案

如果你想每行输入数据,这里是一个例子:

If you want to enter data per line, here is an example:

class Book
{
    int number;
    int year;
    std::string language
    std::string name;
    std::string title;
  public:
    friend std::istream& operator>>(std::istream& input, Book& b);
//...
};

std::istream& operator>>(std::istream& input, Book& b)
{
  std::string text_line;
  std::getline(input, text_line);
  std::istringstream  text_stream(text_line);
  text_stream >> b.number >> b.year >> b.language >> b.name >> b.title;
  return input;
}

如果每个数据项都在单独的行上,您可以将 operator>> 更改为:

If each data item is on a separate line, you could change operator>> as:

std::istream& operator>>(std::istream& input, Book& b)
{
    input >> b.number;
    input >> b.year;
    std::getline(input, b.language);
    std::getline(input, b.name);
    std::getline(input, b.title);
    return input;
}

来自文件的示例输入:

std::vector<Book> library;
Book b;
while (data_file >> b)
{
    library.push_back(b);
}

这篇关于如何删除已经输入的书,如何让书名、语言、书名加空格不出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆