Hello CStudet,
I have worked on your program a little and came up with this:
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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
//#include <time.h> // <--- Covered by "ctime". You do not need both.
int main()
{
int pressMenu{};
//////// generate random numbers /////////
//std::srand(std::time(0));
srand(static_cast<unsigned int>(time(nullptr))); // <--- Only needs done once.
std::cout <<
"\n PLEASE CHOOSE WISELY!!\n"
" ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*\n\n"
" +===============+ +===============+\n"
" | (1) PROGRAMS | | (2) GAMES |\n"
" +===============+ +===============+\n"
" | (3) EXIT |\n"
" +===============+\n"
" Enteer choice: ";
std::cin >> pressMenu;
switch (pressMenu)
{
case 1:
(pressMenu == '1'); // <--- Not sure about the use of this yet.
std::cout <<
"\n\n PROGRAMS\n"
" +======================+ +======================+\n"
" | (1) ARRAY | | (2) GRADING SYSTEM |\n"
" +======================+ +======================+\n"
" +======================+ +======================+\n"
" | (3) FUNCTION | | (4) SIMPLE GROCERY |\n"
" +======================+ +======================+\n\n";
break;
|
Line 13 is the better way to write this line. Have a look at
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful Well worth the time to watch.
Writing the 2 "cout" statements this way has its advantages. First it becomes 1 very large string to print and it looks more what will display on the screen making it easier to change. You have made good use of the "\n" in each line, but you do not need a "cout" for each line. Also the insertion operator (<<) is not needed between the 2 halves of a line. You only need the (<<) when mixing strings and variables to chain them together.
Case 1 prints a nice menu, but you never use "cin" to make a choice. You just break out of the switch and go to something that looks like it should be in its own function. Case 1 should accept a choice from the menu and call the proper function before you leave the switch.
The same for case 2.
I added case 3 to exit the program. If you would use the switch properly then you could change the return to something else to exit the program.
What you should be doing is using "main" to direct the flow of the program and exit the program when you are finished.
I am thinking that the block of code that follows the switch should be in its own function and called from case 2.
This would allow you to use main to direct the program flow. Also you could put from line 15 to the end of the switch in a do/while loop to keep this running until you choose 3 from the main menu.
You could do the same with the sub-menus in cases 1 and 2 to leave the switch and return to the main menu. I have not worked on this part yet.
Andy