Ok, I skimmed over and here what I noticed immideately:
1) Non-existent formatting. Formatting your code is important because it allows you to easily get code structure and generally makes code easy to read.
2) Using system(). It is bad. It is slow, it is possible security hole, it is unportable.
Define own clear console function and use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <windows.h>
void cls()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
return;
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten ) ||
!GetConsoleScreenBufferInfo( hConsole, &csbi ) ||
!FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten ))
return;
SetConsoleCursorPosition( hConsole, coordScreen );
}
|
If you would want to port your program on other OS, you will need to only make changes to this function.
3) Overuse of goto. Usage of goto implies inability to properly plan program flow. It makes harder to see program structure and harder to make changes.
4) Code duplication. Ideally you should write something only once and then just repeat said code (by calling a function or looping for example).
In your main():
* In first several cout statements:
- you are outputting number
- then some operation name
* in switch:
- selecting operation based on number
- clearing screen
- calling some function.
This makes you wonder: couldn't you link number and name/function in some way to use loops in first case and call function by number in second? You actually can. Look at rewritten main:
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
|
//Additional headers:
#include <vector>
#include <functional>
#include <string>
//...
int main()
{
//or if you prefer function pointers: void (*)() ↓
const std::vector<std::pair<std::string, std::function<void(void)>>> menu = { {"", nullptr}, //Dummy to avoid index convertion
{"Lottery", Lottery}, {"Money Converting", moneycalculator},
{"Age Calculator", AgeCaluclator}, {"Super Market", SuperMarket},
{"Basic Calculator", basicCalculator}, {"Multi-Numbers Calculator", AdvencedCalculator},
};
std::cout << "Choose what do you want to do...\n\n";
for(unsigned i = 1; i < menu.size(); ++i) {
const int margin = 23;
const int item_margin = margin - menu[i].first.size() / 2;
std::cout << std::string(margin - 3/*<-{ length*/, ' ') << "<-{" << i << "}->\n" <<
std::string(item_margin, ' ') << menu[i].first << "\n\n";
}
int choice;
std::cin >> choice;
if(std::cin && choice > 0 && choice <= menu.size()) {
cls();
menu[choice].second(); //Call function stored in vector by index "choice"
}
}
|
(Also you missed one break statement in your main :P )