Hello tintin123,
Been working on this most of the morning. I have not fixed everything as the comments will show, but should point you in the right direction.
Sorry I had to break this up into 2 posts.
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
|
int main()
{
bool notDone{ true }; // <--- Added.
int menuChoice{}/*, menu1*/;
int apples{}, oranges{}, cherries{}, watermelons{}, total{}, menu2{}, balance{ 100 }, fruits{}; // <--- None of these are used in "main". Is the a need for them?
std::cout <<
"___________________________________________________________________________\n"
"| |\n" // <--- Originally ended with a (,).
"| ## ## ####### ## ###### ####### ## ## ####### |\n"
"| ## ## ## ## ## ## ## ## ## ### ### ## |\n"
"| ## ## ## ## ## ## ## ## #### #### ## |\n"
"| ## ## ## ###### ## ## ## ## ## ### ## ###### |\n"
"| ## ## ## ## ## ## ## ## ## ## ## |\n"
"| ## ## ## ## ## ## ## ## ## ## ## ## |\n"
"| ### ### ######## ######## ###### ####### ## ## ######## |\n"
"|__________________________________________________________________________|\n";
std::cout << "\n" << std::string(76, '*') << "\n\n";
do
{
//std::cout << "***********************************************************\n\n\n[1]Menu\n[2]Cart\n[3]Pay-up" << endl;
std::cout <<
"[1]Menu\n"
"[2]Cart\n"
"[3]Pay-up\n"
" Enter choice: "; // <--- Needs an "Exit" choice.
std::cin >> menuChoice;
// <--- Need to check if input is NOT a number.
switch (menuChoice)
{
case 1:
//menu2 + 1; // <--- Never used. And what is your point for this?
menu();
//menuChoice = 0;
break;
case 2:
return 0;
break; // <--- This line never reached.
case 4:
notDone = false;
break;
default:
std::cout << "\n Invalid Input!\n\n";
}
} //while (menuChoice < 1 || menuChoice > 3); // <--- while (notDone);
while (notDone);
return 0; // <--- Not required, but makes a good break point.
}
|
Line is a way to make this easier to work with. Since all lines are strings the IDE and compiler considers this a 1 large string of 770 characters. Therefor you only need 1 "cout" and ";" for everything. The advantage is that it looks more like what you will eventually see on the screen. Also it makes it easier to make changes like the last line when I added a space at the beginning to make it line up better with the lines above. I also added a space between the letters and took 1 (#) off the first line of the (E). Changing the first line of the (E) is not necessary, but I thought it looked better. Your choice.
Line 19 is the same as the beginning of line 23. It makes use of the fill ctor of the string class. I find it easier to work with and adjust. Use whichever 1 works best for you.
Line 24 is the same as the first "cout", but when you add a choice for "Exit" you will see how easy it is to work with.
For line 31. I realize that you are probably in school where no one does anything wrong and nothing ever will go wrong with your program, but eventually you will learn to check for errors and other problems, so now is a good time to start. What I mean here is what is someone enters a letter or other non numeric character? If this happens, because of the formatted input
std::cin >> menuChoice;
. "cin" will become unusable the rest of the program and will not stop for any new input from the keyboard. You need to check for this and fix it before the program continues or it will not work. You could do this with a while loop.
In "cast 1", line 40, I started with this, but decided this is not the best way to accomplish what is needed. That is why I added the bool variable "notDone". The same concept can be used in the "menu" function.
In regards to your function or what should be functions.
A function should do 1 thing and return.
Then there is the name "menu". What kind of menu? The name is misleading because you do not know what kind of menu it is and it does more than just print a menu to the screen.
If you want to keep the function as is that is fine, but give it a name that better describes what it is.
The next question is that you define variables used in this function, but are they need somewhere else? Should they be passed to other functions or defined in "main" where they can be passed to the functions that need them?
End part 1.