C++ game store question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void store ()
{
    string answer2;
    cout << endl;
    cout << location <<endl;
    cout << "Now that you have acquired your party. You should bring supplies with you.\n"
    << "You only have $1000 to spend. So choose wisely." << endl << endl;
    cout << "Choose which supply you would like to purchase" << endl;
    cout << "1) Medical kit" << endl;
    cout << "2) lighter" << endl;
    cout << "3) knife" << endl;
    cout << "4) food" <<endl;
    cout << "5) weapons" << endl;
    cout << "6) clothing" << endl;
    cout << "7) rope" << endl;
    cout << "8) tent" <<endl;
    cout << "9) compass" << endl;
    cout << "10) unknown" <<endl <<endl;

    while (true)
    {
        if (cin >> answer2)
        {
            if (answer2 == "1")
            {
                cout << "A medical kit will heal your party's wounds. " <<endl <<endl;
                break;
            }

            else if (answer2 == "2")
            {
                cout << "A lighter will provide you with fire. "<<endl <<endl;
                break;
            }

            else if (answer2 == "3")
            {
                cout << "A knife will provide you will a valuable tool as well as a weapon. "<<endl <<endl;
                break;
            }

            else if (answer2 == "4")
            {
                cout << "Food is essential to surviving. "<<endl <<endl;
                break;
            }

             else if (answer2 == "5")
            {
                cout << "A weapon will provide you with strong protection. "<<endl <<endl;
                break;
            }

            else if (answer2 == "6")
            {
                cout << "Clothing will provide you protection from the weather. "<<endl <<endl;
                break;
            }

            else if (answer2 == "7")
            {
                cout << "Rope will provide you the ability to craft in the wilderness. "<<endl <<endl;
                break;
            }

             else if (answer2 == "8")
            {
                cout << "A tent will provide your party with shelter. "<<endl <<endl;
                break;
            }

            else if (answer2 == "9")
            {
                cout << "A compass will provide you with navigation. "<<endl <<endl;
                break;
            }

            else if (answer2 == "10")
            {
                cout << "undetermined."<<endl <<endl;
                break;
            }
            else
            {
                cout << "That is not a correct answer, please try again."<<endl <<endl;
            }
        }
    }

    cout << "\t\t\t\t\t\tPress y and Enter to continue." <<endl;

    while(true)
    {
        if(cin >> answer2)
        {
            if(answer2 == "y")
                break;
        }
    }
}


This is a function inside my program that is pretty much a store. The user starts off with 1000 dollars and purchases reduce the money amount depending on what they buy,

im wondering if i can condense this whole function...its seems repeatative and i think there would be a way to use arrays to do that, but i cant figure out how to have different items at a store, and use arrays.

But then again i dont know if a store would just be that long either.

i only started working on the first option (medkit) so the rest of them are not done.

i also am having trouble with the process of using loops to return the user back to the store menu after choosing an option(whether they buy or not), and then breaking the loop after they are done
You should create two functions. One Menu, other choices. Then put choices at Menu's end & Menu at Choices end.
In code form:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void menu();
void choice();

void Menu()
{
      //The Text of the menu
      choice();
}

void choice ()
{
      if (/*condition*/)
      {
            //Something.
            Menu();
      }
}
wouldn't that cause more confusion with more functions?
what is the purpose of having them split into two functions?
closed account (3hM2Nwbp)
Ever work with classes before?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <list>
#include <string>

class Product
{
  public:
    Product(const std::string& name, const std::string& description, int price)
      : name_(name), description_(description), price_(price) { }
    const std::string& getName(void) const { return name_; }
    const std::string& getDescription(void) const { return description_; }
    int getPrice(void) const { return price_; }
  private:
    std::string name_, description_;
    // Sprite sprite_; if you decide to go graphical.
    int price_;
};

class Store
{
  public:

    Store(const std::string& name) : name_(name)
    {

    }

    void registerProduct(const Product& product)
    {
      products_.push_back(product);
    }

    const std::string& getName(void) const
    {
      return name_;
    }

    const std::list<Product>& getProducts(void) const
    {
      return products_;
    }
  private:
    std::string name_;
    std::list<Product> products_;
};

#include <iostream>

int main()
{
  Store store("Amazing Things");
  Product thingy("Thingy", "It's...well...an odd thing.", 12345);
  store.registerProduct(thingy);
  const std::list<Product>& products = store.getProducts();
  std::cout << "Welcome to " << store.getName() << '\n';
  for(std::list<Product>::const_iterator iter = products.begin(); iter != products.end(); ++iter)
  {
    std::cout << iter->getName() << " : " << iter->getDescription() << " : " << iter->getPrice() << '\n';
  }
  return 0;
}
Last edited on
i have read through classes, but it just confused me. So i stuck with the basics till i learn them, before i decided to move to classes.
plus i am totally lost when i look at the code you posted lol
Last edited on
@metalburr:
By having two functions you can create a mutual recursion between them. One function calls the other which in turn calls the first.
Though you will need some way to get out of the shop!

This will work like this:

The main calls menu();
In Menu, you make a choice.
It calls the choice();
choice(); calls menu() again.
This pattern continues, until you put a way to leave the shop.
Last edited on
ahh ok thanks Nisheeth
Topic archived. No new replies allowed.