【问题标题】:List in class with pointers [closed]带指针的类中的列表[关闭]
【发布时间】:2012-03-21 12:45:39
【问题描述】:

“玩家必须有能力在游戏过程中随时知道包里有什么。该方法不接受任何参数,一旦调用,将列出包中的所有内容并显示包中的物品总数当前持有。它还会在当前显示的物品名称下列出每个魔法物品的特性。"

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

class MysticalBag
{
private:
    int useContent,rangeContent;
    string word;
public:
    void inventory()
    {
        useContent;
        rangeContent;
        word;
    }
    void addItems()
    {
        cout << "Enter use count (1-3)" << endl;
        cin >> useContent;
        if(useContent > 1 && useContent < 4)
        {
            cout << "Enter content (1.0  - 100.0)" << endl;
            cin >> rangeContent;
            if(rangeContent > 1.0 && rangeContent < 101)
            {
                cout << "Enter items name as text" << endl;
                cin >> word;
                this->inventory();
            }
            else
            {
                cout << "Invalid" << endl;
            }
        }
        else
        {
            cout << "Invalid!" << endl;
            exit(1);
        }
    }
    void showItems()
    {
        cout << "Showing Item content" << endl;
        cout << word << endl;
        cout << "U - " << useContent << ", C - " << rangeContent;
    }
};

int main()
{
    char choice;
    MysticalBag start;
    MysticalBag *Head = new MysticalBag();
    MysticalBag *Body = new MysticalBag();
    MysticalBag *Tail = new MysticalBag();


    cout << "What do you want to do with the bag?" << endl << endl;
    cout << "(a)dd item" << endl;
    cout << "(r)emove item" << endl;
    cout << "(s)how items" << endl;
    cout << "e(x)it" << endl;
    cin >> choice;

    if(choice == 'a')
    {
        start.addItems();
        main();
    }
    else if(choice == 'r')
    {

    }
    else if(choice == 's')
    {
        start.showItems();
        main();
    }
    else if(choice == 'x')
    {
        cout << "Thanks for playing!" << endl;
        getch();
        exit(1);
    }

    getch();
}

问题:

1.) 我如何在清单中输入任何值,但它不允许我接受任何参数。

2.) 如果用户想要放入两个项目。我该怎么做

3.) 在showItems()中显示多个项目

【问题讨论】:

  • 4.) 为你的“问题”付出一些努力。

标签: c++ list class pointers


【解决方案1】:
  1. “方法”显然是指显示库存,所以这不是问题。
  2. 创建一类可放入袋子的魔法物品。制作一类可以包含多个物品的袋子。将物品添加到包中。
  3. 从 2 开始为包添加显示方法。
  4. (奖励项目)不要打电话给main。写一个循环。
  5. (奖励项目)useContent;rangeContent;word; 行没有完成任何事情。
    目前还不清楚你期望在那里发生什么。确保您可以访问一本好书。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多