Is there a way to work around this? (Return Value Function)

closed account (yR9wb7Xj)
Getting an ERROR on the new code. I've approached it a different way letting the user re attempt to get the correct password to access whatever they needed to access.
EDIT: I know why the default statement is executing when the user inputs a letter not a # option listed on the display because the do while loop everything inside execute once. I understand that .My question is how can I work around it using a switch with a while loop that way the user can re enter the password if it's incorrect.
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
#include <iostream>
using namespace std;

int displayMenu() {

	int choice;

	cout << "Welcome to Chase financial security!" << endl;
	cout << "Choose the option you want, below: \n" << endl;
	cout << "1. Access your bank account? " << endl;
	cout << "2. Access your deposit account?" << endl;
	cout << "3. Access your transfer account?" << endl;
	cin >> choice;

	return choice;
}

bool accessedMenu() {
	string password = "codex23jv";
	string input;
	cout << "Enter password to access this option." << endl;
	cin >> input;
	if (input == password) {
		return true;
	}

	return false;
}
int main() {
do{


	switch (displayMenu()) {
	case 1:
	case 2:
	case 3:
		while(!accessedMenu())
		{

			cout << "Access Denied!!!!!!!!! ------ TRY AGAIN!" << endl;
		}
		break;

	default:
		cout << "The option you have enter is invalid." << endl;


	}
}
	while(false);
	cout << "Access Accepted!!!---- GOOD JOB!";
	return 0;
} 


Last edited on
closed account (yR9wb7Xj)
Can anyone please help? I really want to know if it possible to have this solution.
what is your q?
the code work fine.

Try and keep anything related to the menu together in a nice tidy function, it makes your code easier to follow when it grows in size.. at the moment, your program calls the menu, the menu then returns whatever you typed and inside your main you check with a switch statement..

Logically the switch code is related to the menu so you should incorporate that with the menu function, and have that menu function call up the individual functions depending on what option the user chooses.

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

#include <iostream>
#include <string>

void displayMenu();
void accessBank();
void accessDeposit();
void accessTransfer();
void displayMenu();
bool passwordCheck();


int main()
{
	displayMenu();
	return 0;
}


void displayMenu()
{
	int choice;
	bool running = true;

	while (running)
	{
		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);

		// we get here on a valid option
		// so check what was selected.

		switch (choice)
		{
		case	1:
			if (passwordCheck()) accessBank();
			break;
		case	2:
			if (passwordCheck()) accessDeposit();
			break;
		case	3:
			if (passwordCheck()) accessTransfer();
			break;
		case	4:
			running = false;
			break;
		}
	}

}

bool passwordCheck()
{
	// 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
}

@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. ...
Last edited on
closed account (yR9wb7Xj)
Thank you guys!

@ Andy

A difference of opinion!?

Yeah :) - no harm though, everyone has their own coding style :)

Its personal preference really, I prefer to keep anything related to Menu code within the same function, i.e. call displayMenu and take care of anything related to the menu such as taking the option, acting on the option selected such as calling the appropriate function.. doesn't make sense to me writing a function to show menu, return a value to main and then act on it...

A lot of the code and function names were left untouched. However, Point 5 wasn't an issue for his password so getline wasn't needed - ofc if two or more words were needed then we would have an issue here :)

I agree with your point 6 in terms of debugging - no argument there.. a newline for accessBank allows a breakpoint :) - picky picky mr westken haha ;-)


picky picky mr westken haha ;-)

Yep - I was in a picky mood at the time.

But the latter comments weren't esp directed at you -- I even said "directed generally!" -- I assumed your answer was focused on the original question.

I usually try and stick to the main point of the thread (there are so many side issues in some posts!) But from time to time I feel I should draw people's attention (esp. the keener new programmers) to other issues so they start to really get how to write code which handles the unexpected gracefully.

Given the size of the above program it's very likely that, if this was a little utility program I was writing for personal use, I would have just left the contents of your displayMenu() in main().

Andy
Last edited on
Topic archived. No new replies allowed.