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?
#include <iostream>
usingnamespace 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;
}
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:
#include <iostream>
usingnamespace std;
//variables:
char menuOption;
constint MAX_NO_CANDY_TYPES{12};
int candyType[MAX_NO_CANDY_TYPES];
int candy_selection{0};
constint 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;
}
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.