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? [closed]

左心房为你撑大大i 提交于 2019-12-13 09:53:27

问题


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)

This is the source code that i've been made.

#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.


回答1:


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

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

An example input from a file:

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


来源:https://stackoverflow.com/questions/52594929/how-to-delete-the-book-that-have-been-input-and-how-to-make-the-title-language

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!