为什么我的SetBookTitle没有存储字符串

时间:2015-01-21 20:34:49

标签: c++ arrays string class variables

奇怪的是,当我输入要存储的成员的标题时,它并不存储它的错误。我不知道问题是我尝试了很多东西。我知道中值变量工作正常,但实际的类成员函数存储不起作用请帮助。

 class BookType
{
private:
    string Bookinfo;
    string BookTitle;
    string publisher;
    double price;
    int ISBN;
    string authors[4];
    int tracka;
    int copy;
    int Booktracker;

public:
    BookType()
    {

    }
    void setpub(const std::string &pr)
    {
         publisher = pr;
    }
    string getpub()
    {
        return publisher;
    }
    void setcopy(int c)
    {
    int copy = c;
    }
    int getcopy()
    {
        return copy;
    }
    void setprice(double p)
    {
    double price = p;
    }
    double getprice()
    {
        return price;
    }
    void SetBookTitle(const std::string &BT)
    {
        BookTitle = BT;
    }
    string GetBookTitle()
    {
        return BookTitle;
    }
    void SetISBN(int I)
    {
         ISBN = I;
    }
    int GetISBN()
    {
        return ISBN;
    }
    void setnumAuthor(int a)
    {
        tracka = a;
    }
    void setauthor(const std::string &a, int tracka)
    {
        for (int tracka; tracka<4; tracka++)
        {
            authors[tracka] = a;
        }
    }
    void setTBook(int T)
    {
        Booktracker = T;
    }
    string getauthor()
    {
        for (int i = 0; i < 4; i++)
        {
            return authors[i];
        }
    }
};

int main()
{



    optionmenu();


    system("pause");
        return 0;
}

void optionmenu()
{
    BookType Book[10];
    int a = 0;
    string title;
    int isbn;
    int i = 0;
    string nu;
    string author;
    char choice;
    string pub;
    double price;
    int copy;
    int ch;
    string srch;
    bool found = false;

    cout << "Hello welcome to the Bookinfo site please select an option." << endl;
    cout << " do you wish to continue the book info program? 1 for yes any key for no!" ; cin >> ch;
    while (ch == 1)
    {

    MenuOptions();
    cin >> choice;

    switch (choice)
    {

    case '1': 

    while (i >= 10)
    {
        cout << "you have reached he book limit, choose another menu option!"; cin >> choice;
    }
    cout << i << endl;

    cout << i << endl;
        cout << "=====================" << endl;
        getline(cin, nu);
        cout << "Please enter book title" << endl;
        getline(cin,title);
    Book[i].SetBookTitle(title);
if (choice = '1')
    {
        i++;
    }

        cout << "please enter the author(s) name(s), if less then 4 authors leave (NA) in the leftover blanks." << endl;
        for (int ai = 0;  ai < 4; ai++)
        {
        getline(cin, author);   
        Book[i].setnumAuthor(ai);
        }
        cout << "=======================================================" << endl;
        cout << "please enter the books ISBN Number." << endl;
        cin >> isbn;
        Book[i].SetISBN(isbn);


        cout << "what is the publisher of the book?" << endl;
        getline(cin, nu);
        getline(cin, pub);

        Book[i].setpub(pub);

2 个答案:

答案 0 :(得分:4)

void SetBookTitle(string BT)
{
    string BookTitle = BT; //Does not assign to class member
}

这将创建一个新的函数局部变量BookTitle隐藏类成员。当函数超出范围时,不保留该值。

看起来应该更像这样:

void SetBookTitle(const std::string &BT)
{
    BookTitle = BT;
}

答案 1 :(得分:1)

您正在重新定义变量名称。

void SetBookTitle(string BT)
{
    string BookTitle = BT;
}

应该是:

void SetBookTitle(string BT)
{
    BookTitle = BT;
}

我倾向于使用this->来确保我使用该类成员。