@Softrix
A difference of opinion!?
To my mind, your version of displayMenu() is (a) doing too much, and (b) not doing (just) what it says it's going. I'd refactor it to something like this (i.e. more like Codex23jv's version):
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <string>
void accessBank();
void accessDeposit();
void accessTransfer();
int getUserChoice(); // rather than displayMenu()
bool checkPassword(); // swapped function name about so verb-based
int main()
{
int choice = 0;
do // swap to do-while loop using choice to control it
{
choice = getUserChoice();
// we get here on a valid option
// so check what was selected.
switch (choice)
{
case 1:
if (checkPassword())
accessBank();
break;
case 2:
if (checkPassword())
accessDeposit();
break;
case 3:
if (checkPassword())
accessTransfer();
break;
case 4:
{/* nothing to do - about to exit */}
break;
// could handle the default here with an assert() for debug build?
}
}
while (choice != 4);
return 0;
}
int getUserChoice()
{
int choice = 0; // best to init all variables
std::cout << std::endl << "Welcome to Chase financial security!" << std::endl;
std::cout << "Choose the option you want, below: \n" << std::endl;
std::cout << "1. Access your bank account? " << std::endl;
std::cout << "2. Access your deposit account?" << std::endl;
std::cout << "3. Access your transfer account?" << std::endl;
std::cout << "4. Exit" << std::endl << std::endl;
// dont let me move on if not between 1-4
do {
std::cout << "Enter Choice: ";
std::cin >> choice;
} while (choice < 1 || choice > 4);
return choice;
}
bool checkPassword()
{
// in the real world you wouldnt want it to keep
// repeating until you got the password right but
// since you want it that way i have done it using
// a do while loop.
// obviously this function would never return false
// the way you want to do it, but I have left it
// this way so that you can easily change your mind
// and have it asking once.
std::string password = "codex23jv";
std::string input;
do {
std::cout << std::endl << "Enter password to access this option." << std::endl;
std::cin >> input;
} while (input != password);
return true;
}
void accessBank()
{
// do your stuff
}
void accessDeposit()
{
// do your stuff
}
void accessTransfer()
{
// do your stuff
}
|
Andy
PS Misc other stuff (directed generally!) ...
1. If the contents of main grew I would factor the do-while loop into it's own function.
2. It looks like a enum should be used for the choices? See "Enumerated types (enum)" section here:
http://www.cplusplus.com/doc/tutorial/other_data_types/
3. lines 27-32 could be factored out to a function call displayMenu() if the menu grew enough to justify it
4. the choice loop on lines 32-38 is not safe. To start with it goes into an infinite loop if a word is given rather than a number.
5. the password loop on lines 76-79 doesn't handle spaces very gracefully. It might be better to use std::getline here, though you would need to trim the input string.
6. I changed
if (checkPassword()) accessBank();
to
1 2
|
if (checkPassword())
accessBank();
|
(etc) because I find the latter form to be more debugger friendly (when setting breakpoints.)
7. I swapped passwordCheck() to checkPassword() so the function name is verb-based - to be consistent with the other functions in this program.
8. ...