After login works, how to get to next menu

closed account (jEb91hU5)
I am writing an ATM code for beginners using different functions. I am attempting to do it with call by reference. I tried using strings for username and password but it came up with a throw exception, so just doing int. I need to know how to get from one menu to the next once logging in works. So getting to menu2 in the code.

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>

using namespace std;

void menu();
void menu2();
void login(int &x, int &y);
void create(int &a, int &b);
void displayBalance(double &x);
double depositBalance(double &a, double &b);
double withdrawSum(double &x, double &y);

int main()
{
	menu();
}

void menu()
{
	int username = 0000, password = 0000, id, pass;
	int choice;
	cout << "::Please choose an option:\n"
		<< "1. Login\n"
		<< "2. Create a new account\n"
		<< "3. Quit\n"
		<< "Enter Choice: ";
	cin >> choice;

	while (choice != 3)
	{
		switch (choice)
		{
		case 1: 
			cout << "Please enter your user ID: ";
			cin >> id;
			cout << "Please enter your password: ";
			cin >> pass; 
			if (id == username && pass == password)
				void menu2();
			else
				cout << "Username or password incorrect\n";
			break;
		case 2: create(username, password);
			break;
		case 3: cout << "Thanks for using the ATM!\n";
			break;
		default: cout << "Please try again\n";
		}
		cout << "Enter Choice: ";
		cin >> choice;
	}
}


void menu2()
{
	double balance, deposit, withdraw;
	int choice2;
	balance = 0;
	cout << "::Please choose an option::\n"
		<< "1. Deposit Money\n"
		<< "2. Withdraw Money\n"
		<< "3. Request Balance\n"
		<< "4. Quit\n"
		<< "Enter Choice: ";
	cin >> choice2;

	while (choice2 != 4)
	{
		switch (choice2)
		{
		case 1: cout << "Enter a sum you wish to add to your account: $";
			cin >> deposit;
			balance = depositBalance(balance, deposit);
			cout << "Your new balance is: $" << balance << endl;
			break;
		case 2: cout << "Enter a sum you wish to withdraw from your account: $";
			cin >> withdraw;
			balance = withdrawSum(balance, withdraw);
			cout << "Your new balance is: $" << balance << endl;
			break;
		case 3: displayBalance(balance);
			break;
		default: cout << "Your have entered a wrong input\n";
		}
		cout << "Enter Choice: ";
		cin >> choice2;
	}
}


double depositBalance(double &a, double &b)
{
	double balance = a + b;
	return balance;
}


double withdrawSum(double &x, double &y)
{
	double balance = x - y;
	return balance;
}

void displayBalance(double &x)
{
	cout << "Your balance is $" << x << endl;
}

void create(int &a, int &b)
{
	cout << "Please create a username: ";
	cin >> a;
	cout << "Please create a password: ";
	cin >> b;
}
Last edited on
Hello JLaw21,

To start with line 39 is a nice prototype, but you have already done that on line 6. To make it a function call remove the "void" on line 39.

The "username", "password", "id" and "pass" should be "std::strings.

You have a prototype for "login", but no function to do the work.

In your function "displayBalance" you are passing the variable by reference. This is not necessary as the function does not change the variable. A simple pass by value will work.

Looking at line one I was wondering if you included the header file "<string>" to be able to work with a "std::string".

I will give it a test run and see what I come up with.

Hope that helps,

Andy
Hello JLaw21,

I did some rearranging on the program.

Added #include <string> and changed the variables to std::string username{ "Andy" }, password{ "asdf" }, id, pass; and it worked just fine. The only problem I see is the "username" and "password" can only store one name and password. A "std::map" may be worth considering.

Defining the variables as "string"s I had to change the prototype and function definition for create so that the program would compile.

In both "menu" functions the while loop works better as a do/while loop. To do this you need to include the "cout" statement for the menu and remove the prompt and "cin >>" after the switch for it to work.

Tip:

Try not to use single letter variables in your prototypes and function definitions. It makes it hard to follow when reading the code.

As an example:
1
2
3
4
5
6
7
void create(std::string &username, std::string &password)
{
	std::cout << "Please create a username: ";
	std::cin >> username;
	std::cout << "Please create a password: ";
	std::cin >> password;
}

The variables "username" and "password" become local variables to the function. That is why you can use the same names. The fact that they are being passed by reference makes no difference except that the will change the calling variables.

You can use all lower case letters for your variable names, but the "cameCase" method is more often used. So "username" would be "userName". It does help with readability, but not necessary. I would suggest staying with whatever way you choose to write your variable names. Being consistent is better than mixing.

So far I have just worked with the menus. In "menu" I would suggest making "case 1" a function call and not doing the work there.

Now I look at the other functions and see what happens.

Hope that helps,

Andy
closed account (jEb91hU5)
Andy,

Thanks so much for your timely reply.

I deleted "void" as well as the call by reference for "displayBalance" and it ran fine with what I had.

I did try and add strings again like I had previously, but when trying to run, it gives me an "Exception Thrown" error on line 20.

Here is what I changed for strings (error). But if I replace all strings with int, it runs fine.

Thanks again,

JLaw21

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <string>

using namespace std;

void menu();
void menu2();
void create(string &a, string &b);
void displayBalance(double x);
double depositBalance(double &a, double &b);
double withdrawSum(double &x, double &y);

int main()
{
	menu();
}

void menu()
{
	string username = 0000, password = 0000, id, pass;
	int choice;
	cout << "::Please choose an option:\n"
		<< "1. Login\n"
		<< "2. Create a new account\n"
		<< "3. Quit\n"
		<< "Enter Choice: ";
	cin >> choice;

	while (choice != 3)
	{
		switch (choice)
		{
		case 1: 
			cout << "Please enter your user ID: ";
			cin >> id;
			cout << "Please enter your password: ";
			cin >> pass; 
			if (id == username && pass == password)
				menu2();
			else
				cout << "Username or password incorrect\n";
			break;
		case 2: create(username, password);
			break;
		case 3: cout << "Thanks for using the ATM!\n";
			break;
		default: cout << "Please try again\n";
		}
		cout << "::Please choose an option:\n"
			<< "1. Login\n"
			<< "2. Create a new account\n"
			<< "3. Quit\n" 
			<< "Enter Choice: ";
		cin >> choice;
	}
}


void menu2()
{
	double balance, deposit, withdraw;
	int choice2;
	balance = 0;
	cout << "::Please choose an option::\n"
		<< "1. Deposit Money\n"
		<< "2. Withdraw Money\n"
		<< "3. Request Balance\n"
		<< "4. Quit\n"
		<< "Enter Choice: ";
	cin >> choice2;

	while (choice2 != 4)
	{
		switch (choice2)
		{
		case 1: cout << "Enter a sum you wish to add to your account: $";
			cin >> deposit;
			balance = depositBalance(balance, deposit);
			cout << "Your new balance is: $" << balance << endl;
			break;
		case 2: cout << "Enter a sum you wish to withdraw from your account: $";
			cin >> withdraw;
			balance = withdrawSum(balance, withdraw);
			cout << "Your new balance is: $" << balance << endl;
			break;
		case 3: displayBalance(balance);
			break;
		default: cout << "Your have entered a wrong input\n";
		}
		cout << "Enter Choice: ";
		cin >> choice2;
	}
}


double depositBalance(double &a, double &b)
{
	double balance = a + b;
	return balance;
}


double withdrawSum(double &x, double &y)
{
	double balance = x - y;
	return balance;
}

void displayBalance(double x)
{
	cout << "Your balance is $" << x << endl;
}

void create(string &a, string &b)
{
	cout << "Please create a username: ";
	cin >> a;
	cout << "Please create a password: ";
	cin >> b;
}
Hello JLaw21,

On line 20 it is the way you are initializing your variables. Try this:
std::string username{ "Name" }, password{ "pass" }, id, pass;.

What you are trying is to initialize a string with an int. As you can see a string needs to be in double quotes.

I added the header file #include <iomanip> and this line in "main" before the call to "menu" std::cout << std::fixed << std::showpoint << std::setprecision(2);.

This will affect all "cout" statements that print a floating point number until you change something. The "showpoint" tells the output to print ".00" if it needs to. This will display "Your balance is $100.00" as an example.

The only function that needs its variables passed by reference is the "create" and "login" functions. All other functions do not change the variables passed to them, so passing by reference is pointless. You could also make those variables "const" as they should not be changed.

I changed some other things like adding a "\n" to the beginning of most lines to break up the output and make it more readable.

Hope that helps,

Andy
closed account (jEb91hU5)
Andy,

I added the points you suggested and everything worked perfectly. I'm quite new to this and hadn't yet learned strings, but now understand my mistake.

I really appreciate all of your help and advice.

JLaw21

Your welcome. Any time.

Given some time you will find that the "std::string" will be much easier to use.

Andy
Hello JLaw21,

Eventually you will find out that when you #include <string> "std::string is actually a class with many member functions at your disposal to help you work with the string.

Tips:

When you define a variable as a "char" the character is in single quotes when you use it.

When you define a variable as a "string" the characters are in double quotes when you use it.

When you define an "int" and initialize it with "0000" only one zero is stored in the variable. The leading zeros are ignored. So all you need for an int or any numeric variable is: int num{ 0 }; and even that can be shortened to int num{};. From C++11 on the empty {}s initialize an "int" to zero, a double, or float (not used as often), to "0.0" and a char to "\0". You can even define an array as int numArray[10]{ };. This will initialize the first element to zero and all the rest to zero.

By putting a number(s) inside the {}s you can initialize that variable to that number. For an array you can put one or more numbers (separated by a comma) inside the {}s and initialize the array with whatever you need. Should your initialization list be less then the size of the array any remaining elements are set to zero.

Some thoughts I had late last night.

Hope that helps,

Andy
Topic archived. No new replies allowed.