Call objects by integer?

I have to create a program where I track types and amounts of candy. I have 4 types of candy that can be called with integers 1-4. When those are called their overall values should be changed based on if candy was added or eaten. This is what I have so far but am lost on how to call and alter these candy types and amounts. I do not think I should be doing an array but possibly reading/writing files?

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
#include <iostream>

using namespace std;

//variables:
char menuOption;
int candyType;
int eatenCandy;

void menu() {
    cout<<"Options Menu:"<<endl;
    cout<<"E - Eat a piece of candy."<<endl;
    cout<<"F - You found an Egg! Get a piece of candy."<<endl;
    cout<<"I - Current inventory list."<<endl;
    cout<<"C - List Candy Types."<<endl;
    cout<<"M - Print this command menu."<<endl;
    cout<<"Q - Quit this program."<<endl;
}

int main(){
 cout<<"----------"<<endl;
 cout<<"** My Candy Database **"<<endl;
 menu();
 do{
     cout<<"Enter your menu selection: ";
     cin>>menuOption;
     if(menuOption=='E' || 'e') {
         cout<<"Please enter the candy type (1-4): ";
         cin>>candyType;   
     }
 }
    return 0;
}
Last edited on
You will need some sort of container to store the various items. There are may ways of doing this but a simple way, albeit using arrays, is one way to start:

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
#include <iostream>

using namespace std;

//variables:
char menuOption;

const int MAX_NO_CANDY_TYPES{12};
int candyType[MAX_NO_CANDY_TYPES];
int candy_selection{0};

const int MAX_NO_CANDIES{10};
int eatenCandy[MAX_NO_CANDIES];
int counter{0};

void menu() {
    cout<<"Options Menu:"<<endl;
    cout<<"E - Eat a piece of candy."<<endl;
    cout<<"F - You found an Egg! Get a piece of candy."<<endl;
    cout<<"I - Current inventory list."<<endl;
    cout<<"C - List Candy Types."<<endl;
    cout<<"M - Print this command menu."<<endl;
    cout<<"Q - Quit this program."<<endl;
}

int main()
{
    cout<<"----------"<<endl;
    cout<<"** My Candy Database **"<<endl;
    menu();
    
    do{
        cout<<"Enter your menu selection: ";
        cin>>menuOption;
        
        if(toupper(menuOption) =='E')
        {
            cout<<"Please enter the candy type (1-4): ";
            cin>>candy_selection;
            eatenCandy[counter] = candy_selection;
            counter++;
        }
        
    }while(counter < MAX_NO_CANDIES);
    
    return 0;
}
Last edited on
Line 38: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

possibly reading/writing files?

Do you need to save program information between runs?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.