how can i shorten up the options?

currently there is only the first option and the exit option, but after having all 10 options, there will be a lot of code for this function. Is there a way to shorten the options up with loops and arrays.

I have already learned about arrays and loops, but i can't think of how to implement them here when i am using different sentences for each option?

also is it better to use switch or if else if in menus?

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
void store ()
{

    cout << endl;

    int money =1000, choiceAmount;
    int medkit = 0;//items
    char choice;//needs to be char for choices cuz of '1'

    cout << "Now that you have acquired your party. You should bring supplies with you.\n" << endl << endl;

    startmenu://goto

    //display money amount
    cout << "**************" <<endl;
    cout << "  Money $" << money <<endl;
    cout << "**************" <<endl <<endl;


    cout << "Choose which supply you would like to purchase" << endl;
    cout << "E) ***Exit Menu***"  <<endl<<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<<endl;

    cin >> choice;


        if(choice == '1')//choice for medkits
        {
            cout << endl <<endl;
            cout << "A medical kit will heal your party's wounds. " <<endl;
            cout << "medkits: $100/each"<<endl;
            cout << "How many would you like to buy?"<<endl;

            cin >> choiceAmount;

            if(money < (100 * choiceAmount))//not enough money, will * choiceamount and value before comparing with money
            {
                cout << endl <<endl;
                cout << "**********You do not have enough money!**********"<<endl<<endl;
                goto startmenu;
            }
            else
            {
                money = money - (choiceAmount * 100);
                medkit = choiceAmount;
                goto startmenu;
            }

        }//end of if choice





    if(choice == 'e' || 'E')
    {

    cout << "Money $" << money <<endl;
    cout << "Medkits: " << medkit <<endl;

    }//end of choice exit

}//end of function 


Last edited on
I would recommend giving each menu option a separate function if you would like to reduce code inside the menu function. Also the switch statement block could reduce code in this menu function. I happened to notice that when the medkits are purchased your code simply sets the number of medkits to that amount just purchased, but some people could buy some at one time and come back and buy more later. (unless of course the medkits are used when the store function exits)
Last edited on
so are you saying my medkit variable will not be able to hold the bought amount time and time again? my original intent was to be able to have store the amount bought in (medkit variable) use it later at any time and come back and buy more and add it to the same variable.

i first attempted to use different functions as store() (the list of choices) and choice() (each possible option) but i am not too good at functions and/or returning values to and from functions.....anyway when i had store() function call to choice() function it worked, but it did not when i did vise-versa, and was unable to figure out how to return to the "main menu" (i guess you'd call it) , and at that time i had loops for my options, so i was stuck in the loop when i attempted to call store()

i deleted the function store()l and attempted it again, cuz i came back a few days later and couldnt understand all my nested loops, i then added the goto to be able to get out of choice and go back into store
Last edited on
For the medkit variable I'm going to recommend something that should not be used very often. A global variable. Global variables are declared outside of the main function the same place where includes and stuff like that goes. EX:
1
2
3
4
5
6
#include <iostream>
using namespace std;

int GLOBAL_VARIABLES_TYPICALLY_ARE_PUT_IN_ALL_CAPS = 0;

int main{...}...

Basically that integer would be able to be accessed and edited anywhere in the program. Sometimes programmers have a global variable they want to be constant or unchangeable. The static and const keywords can be used for that: ((see andy's post below for more info on the static keyword))
static const int THIS_IS_UNCHANGEABLE_ONCE_INITIALIZED = 3.1415;

What I had in mind as other functions was:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        if(choice == '1')//choice for medkits
        {
            cout << endl <<endl;
            cout << "A medical kit will heal your party's wounds. " <<endl;
            cout << "medkits: $100/each"<<endl;
            cout << "How many would you like to buy?"<<endl;

            cin >> choiceAmount;

            if(money < (100 * choiceAmount))//not enough money, will * choiceamount and value before comparing with money
            {
                cout << endl <<endl;
                cout << "**********You do not have enough money!**********"<<endl<<endl;
                goto startmenu;
            }
            else
            {
                money = money - (choiceAmount * 100);
                medkit = choiceAmount;
                goto startmenu;
            }

        }//end of if choice 

Being changed to:
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
        if(choice == '1')//choice for medkits
        {
            medkit();
        }

...

void medkit()
{
   cout << endl <<endl;
   cout << "A medical kit will heal your party's wounds. " <<endl;
   cout << "medkits: $100/each"<<endl;
   cout << "How many would you like to buy?"<<endl;

   cin >> choiceAmount;

   if(money < (100 * choiceAmount))//not enough money, will * choiceamount and value before comparing with money
   {
       cout << endl <<endl;
       cout << "**********You do not have enough money!**********"<<endl<<endl;
       goto startmenu;
   }
   else
   {
       money = money - (choiceAmount * 100);
       medkit = choiceAmount;
       goto startmenu;
   }
}


Goto is something that is not recommended by any programmer I know. Because when you go back to that code and forget what each goto is for you have to follow them and it gets confusing. Instead of using gotos see if you can reorder the code or even write a new function to be called.
Last edited on
i actually put ALL my variables in Global scope until i read that it was bad programming usage, cuz originally i was thinking that it would be easy to just plain use them and not worry about it during functions, etc.

ahh ok that makes sense.(your change code from...to..)
is it usual practice to spread code into a lot of functions, or is that where you would put store function and all choice functions in a header file and just call store when you need it? or is that not necessary for a newbie?

originally i was going to put one "thing" in each choice and move on, and maybe later change it. So (for Example) weapons , i could see numerous weapons coming up and then i would easily see why you would want a function for each choice. I wasnt thinking ahead though.

does a function update a global variable without returning anything? Cuz i thought a function had to return a variable to be able to use it in another function (if it changed the value)

for the goto, i had already exhausted my limits of knowledge and just said "screw it , i'll use the goto" ...cuz i knew that was also bad pragramming usage. It seems though that goto is so easy to understand and follow for me, especially if i was to comment on each goto. The only other way (that i found) to back out of the choice and go to the store was to use loops , but then i wouldnt be able to use your functions (and i got confused with all the loops). I'm assuming that you cannot start a loop in one function and then end the loop in another, but i dont know for sure.

I'll try your idea , i think i can figure that out to work proper, all except for the goto, (which i want to change, but frustrated with other options i used)
wow thanks kevinkjt2000

it looks so much cleaner and easier to maintain and understand
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
void store ()
{
    char choice;//needs to be char for choices cuz of '1'

    //display money amount
    cout << "**************" <<endl;
    cout << "  Money $" << MONEY <<endl;
    cout << "**************" <<endl <<endl;


    cout << "Choose which supply you would like to purchase" << endl;
    cout << "E) ***Exit Menu***"  <<endl<<endl;

    cout << "1) Medkit" << 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<<endl;

    cin >> choice;


        if(choice == '1')//choice for items
                {
                        store_medkit();
                }

        if(choice == '2')
                {

                }

        if(choice == '3')
                {

                }

        if(choice == '4')
                {

                }

        if(choice == '5')
                {

                }

        if(choice == '6')
                {

                }

        if(choice == '7')
                {

                }

        if(choice == '8')
                {

                }

        if(choice == '9')
                {

                }

        if(choice == 'e' || 'E')
                {
                        inventory();
                }//end of choice exit
}//end store function

void inventory()
{
        cout << "****INVENTORY****"<<endl;
        cout << "Money $" <<"\t\t"<< MONEY <<endl;
        cout << "Medkits: " <<"\t"<< MEDKIT <<endl;
        cout << "Lighters: "<<"\t" << LIGHTER <<endl;
        cout << "Knives: "<<"\t" << KNIFE <<endl;
        cout << "Food: "<<"\t\t" << FOOD <<endl;
        cout << "Weapons: "<<"\t" << WEAPON <<endl;
        cout << "Clothing: "<<"\t" << CLOTHING <<endl;
        cout << "Rope: "<<"\t\t" << ROPE <<endl;
        cout << "Tent: "<<"\t\t" << TENT <<endl;
        cout << "Compass: "<<"\t" << COMPASS <<endl<<endl<<endl;
}//end inventory()

void store_medkit()
{
        int choiceAmount;
                cout << endl <<endl;
        cout << "A medkit will heal your party's wounds. " <<endl;
        cout << "medkits: $100/each"<<endl;
        cout << "How many would you like to buy?"<<endl;

        cin >> choiceAmount;

        if(MONEY < (100 * choiceAmount))//not enough money, will * choiceamount and value before comparing with money
        {
            cout << endl <<endl;
            cout << "**********You do not have enough money!**********"<<endl<<endl;

            store();
        }
        else
        {
            MONEY -= (choiceAmount * 100);
            MEDKIT += choiceAmount;//store amount and put into variable for future inventory

                store();
        }
}//end store_medkit() 
one question though, how would the user be able to input (i) for example to display inventory at anytime?
I'll start with your last question first. To be able to put in i/I at anytime and get the inventory to come up would be easiest to implement by writing your own code to input from the keyboard. Since your are a beginner I would just stick to having menus for now. Besides in most games when you are at the store or some other menu, you don't have access to the inventory anyways. The point here is to have i/I be put in at anytime would require you to program that.

You can start a loop and end it. That would be the c++ keyword break found on this page of the documentation: http://cplusplus.com/doc/tutorial/control/
As far as ending a loop in one function then going to a loop in another perhaps that would just be a creative use of break. I'll let you figure out break on your own. (In my opinion it is a fun function, just be sure not to miss code you want to always be executed in a loop because of a misplaced break.)

does a function update a global variable without returning anything?
The answer is yes, but only if you actually have code that changes it inside that function. A function only returns something if the function data type is something other than void. Any program function can change a global variable. (This is why most programmers consider it bad practice to use global variables.)

I noticed how you said you weren't thinking ahead. I would suggest to get familiar with how all the loops, if-else statements, functions, etc. all work in this project. Perhaps in the next project you could make an outline of what kind of functions you will need to accomplish certain tasks. Because later on in your programming you will start using classes, and it is recommended to think ahead by that point.

It is usual practice to spread code around to different functions. I once had a teacher say a single function should print out onto one page horizontally. The reason being when a function gets super long it is just plain hard to read. (That is when all those nested loops start to get confusing.)

By the way when you read about break on that page I linked to, also look at the switch statement down at the bottom. You might end up liking switch so much you will use switch code for your menu.

Edit: Also a cool thing about if-else statements, for statements, and a few others is that they can drop the brackets if there is only one statement to execute. EX:
1
2
for(int count=1; count<11; count++)
    cout << count++;
Last edited on
Minor correction!

Basically that integer would be able to be accessed and edited anywhere in the program. Sometimes programmers have a global variable they want to be constant or unchangeable. The static keyword can be used for that:


static int THIS_IS_UNCHANGEABLE_ONCE_INITIALIZED = 3.1415;

The static keyword does not make a variable unchangeable. That requires the const keyword.

static const int THIS_IS_UNCHANGEABLE_ONCE_INITIALIZED = 3.1415;

What the static keyword does is limit the scope of the variable to "the current translation unit", which pretty much means the current cpp file. This is also referred to as internal linkage, cf. external linkage, which means the variable is available to other translation units/cpp files.

(If you used #include to add more source after the definition, it would see the variable. Hence the term "current translation unit" = everything compiled to create a single object file). But I think that's - usually - an abuse the #include mechanism.)

Notes
1. global variables marked as const have implicit internal linkage, so you can omit the static keyword. But for a const to be visible to another cpp file, it must be marked as extern.
2. the use of the static keyword within a class definition is a quite different matter.

Andy
Topic archived. No new replies allowed.