fix login code c++

please help me fix this code,i can't run the system. act, im still newbie in coding. thanks in advance. i want to put login function but, i failed.

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

void main()
{
int price = 0, total_price, mykad_dis = 0, mbocard_dis, current_total = 0, balance = 0;
double choice, mykad, discount, mbocard, payment,userPassword;
int selected = 0, exit = 0
while (login_attempt < 5)
{
cout << "Please enter your user password: ";
cin >> userPassword;

if (userPassword == 123)
{
cout << "Welcome!\n";
break;
}
else
{
cout << "Invalid login attempt. Please try again.\n" << '\n';
login_attempt++;
}
}
if (login_attempt == 5)
{
cout << "Too many login attempts! The program will now terminate.";
system("pause");
}

cout << "Thank you for logging in.\n";
}

//user selecting package

startback:
system("cls");

cout << "\t<============================MBO Allstar============================>" << endl << endl;

cout << "current total: " << current_total << "\n";

cout << "select package\t:" << endl << endl;

cout << "[1] 2 adult and 3 kid - Price RM70" << endl;
cout << "[2] Adult - Price RM30" << endl;
cout << "[3] Kid - Price RM15" << endl;
cout << "[4] Senior Citizen - Price RM20" << endl << endl;
cout << "[9] Proceed with the payment" << endl;
cin >> choice;

if (choice == 1)
{
cout << "\tYou chose: 2 adult and 3 kid pakcage" << endl << endl;
price = 70;
}

else if (choice == 2)
{
cout << "\tYou chose: adult package" << endl << endl;
price = 30;
}
else if (choice == 3)
{
cout << "\tYou chose: kid package" << endl << endl;
price = 15;
}
else if (choice == 4)
{
cout << "\tYou chose: Senior Citizen package" << endl << endl;;
price = 20;
}
else if (choice == 9)
{
goto payment;
}

current_total = current_total + price;
cout << "Current total price:\t" << current_total << endl;
repeat_question:
cout << "Want to add more?\n\n";
cout << "[1] Yes\n";
cout << "[2] No\n\n";
cin >> selected;

if (selected == 1) {
goto startback;
}
else if (selected == 2)
{

}
else {
goto repeat_question;
}

payment:
system("cls");
cout << "Current total price:\t" << current_total << endl;
cout << "[1]Got mykad" << endl;
cout << "[2]No mykad" << endl;
cin >> mykad;

if (mykad == 1)
{
mykad_dis = current_total*0.15;
}
else if (mykad == 2)
{
mykad_dis = 0;
}

cout << "[1] Got Mbo Card"<<endl;
cout << "[2] No Mbo Card" << endl;
cin >> mbocard;

if (mbocard == 1)
{
mbocard_dis = current_total*0.05;
}
else if (mbocard == 2)
{
mbocard_dis = 0;
}



total_price = current_total - mykad_dis - mbocard_dis;
cout << "Total price is\t RM" << total_price << endl;

bool payment_complete;

payment_complete = false;

while (!payment_complete) {

cout << "Please enter payment" << endl;
cin >> payment;

if (payment >= total_price)
{
payment_complete = true;

if (payment > total_price) {
balance = payment - total_price;
cout << "Your balance is:\t" << balance << endl;
}

cout << "Thanks for purchasing\n";

}
else
{
cout << "Your money is not enough. Please try again" << endl;
}
}
cout << "\n[10] Exit program" << endl;
cout << "[9] Restart system"<<endl;
cin >> exit;
if (exit == 10){
system("cls");
cout << "Thanks for using this system\n";
system("pause");
}
else if (exit== 9){
goto startback;
}
else
{
cout << "invalid input" << endl;

}
}

Last edited on
Just putting your code in code tags for better view.
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

void main()
{
int price = 0, total_price, mykad_dis = 0, mbocard_dis, current_total = 0, balance = 0;
double choice, mykad, discount, mbocard, payment,userPassword;
int selected = 0, exit = 0
while (login_attempt < 5)
{
cout << "Please enter your user password: ";
cin >> userPassword;

if (userPassword == 123)
{
cout << "Welcome!\n";
break;
}
else
{
cout << "Invalid login attempt. Please try again.\n" << '\n';
login_attempt++;
}
}
if (login_attempt == 5)
{
cout << "Too many login attempts! The program will now terminate.";
system("pause");
}

cout << "Thank you for logging in.\n";
}

//user selecting package

startback:
system("cls");

cout << "\t<============================MBO Allstar============================>" << endl << endl;

cout << "current total: " << current_total << "\n";

cout << "select package\t:" << endl << endl;

cout << "[1] 2 adult and 3 kid - Price RM70" << endl;
cout << "[2] Adult - Price RM30" << endl;
cout << "[3] Kid - Price RM15" << endl;
cout << "[4] Senior Citizen - Price RM20" << endl << endl;
cout << "[9] Proceed with the payment" << endl;
cin >> choice;

if (choice == 1)
{
cout << "\tYou chose: 2 adult and 3 kid pakcage" << endl << endl;
price = 70;
}

else if (choice == 2)
{
cout << "\tYou chose: adult package" << endl << endl;
price = 30;
}
else if (choice == 3)
{
cout << "\tYou chose: kid package" << endl << endl;
price = 15;
}
else if (choice == 4)
{
cout << "\tYou chose: Senior Citizen package" << endl << endl;;
price = 20;
}
else if (choice == 9)
{
goto payment;
}

current_total = current_total + price;
cout << "Current total price:\t" << current_total << endl;
repeat_question:
cout << "Want to add more?\n\n";
cout << "[1] Yes\n";
cout << "[2] No\n\n";
cin >> selected;

if (selected == 1) {
goto startback;
}
else if (selected == 2)
{

}
else {
goto repeat_question;
}

payment:
system("cls");
cout << "Current total price:\t" << current_total << endl;
cout << "[1]Got mykad" << endl;
cout << "[2]No mykad" << endl;
cin >> mykad;

if (mykad == 1)
{
mykad_dis = current_total*0.15;
}
else if (mykad == 2)
{
mykad_dis = 0;
}

cout << "[1] Got Mbo Card"<<endl;
cout << "[2] No Mbo Card" << endl;
cin >> mbocard;

if (mbocard == 1)
{
mbocard_dis = current_total*0.05;
}
else if (mbocard == 2)
{
mbocard_dis = 0;
}



total_price = current_total - mykad_dis - mbocard_dis;
cout << "Total price is\t RM" << total_price << endl;

bool payment_complete;

payment_complete = false;

while (!payment_complete) {

cout << "Please enter payment" << endl;
cin >> payment;

if (payment >= total_price)
{
payment_complete = true;

if (payment > total_price) {
balance = payment - total_price;
cout << "Your balance is:\t" << balance << endl;
}

cout << "Thanks for purchasing\n";

}
else
{
cout << "Your money is not enough. Please try again" << endl;
}
}
cout << "\n[10] Exit program" << endl;
cout << "[9] Restart system"<<endl;
cin >> exit;
if (exit == 10){
system("cls");
cout << "Thanks for using this system\n";
system("pause");
}
else if (exit== 9){
goto startback;
}
else
{
cout << "invalid input" << endl;

}
}
any1 please help me to make the login function work.
Line 10 : missing ;
Line 11: login_attempt is undeclared variable.
Line 38: What are you trying to do? Are you trying to call a function?
Line 82: What are you trying to do? Are you trying to call a function?
Line 92-94: Statement is empty.
Line 99: What are you trying to do? Are you trying to call a function?
Line 108 : You are having int variable = int variable * 0.15. The answer will be an int and would drop the numbers after the decimal. (i.e 0 = 5 * 0.15). If the variable was a float or double like (double a = 5 * 0.15) the answer will be 0.75.
Line 121: Same issue as line 108.
Those were the issues I could find with my time.

I would avoid the use goto. I would use loops or another flow method of the code.



this is my latest code, would you help me to simplify it?


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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

void main()
{
	int price = 0, total_price, mykad_dis = 0, mbocard_dis, current_total = 0, balance = 0;
	double choice, mykad, discount, mbocard, payment, password;
	int selected = 0, exit = 0, x = 1;

	while (1)
	{
		system("cls");
		cout << "Please enter password: ";
		cin >> password;

		if (password == 123456) //This sets the condition for success
		{
			cout << "Access Granted!!!!";
		}

		else if (password != 123456) //This sets the condition for failure

		{
			cout << "Wrong password" << "\n" << x << " " << "wrong attempts" << "\n";
			++x;
		}
		if (x > 3)
		{
			goto exit;
		}
	}

	//user selecting package

startback:
	system("cls");

	cout << "\t<============================MBO Allstar============================>" << endl << endl;

	cout << "current total: " << current_total << "\n";

	cout << "select package\t:" << endl << endl;

	cout << "[1] 2 adult and 3 kid - Price RM70" << endl;
	cout << "[2] Adult             - Price RM30" << endl;
	cout << "[3] Kid               - Price RM15" << endl;
	cout << "[4] Senior Citizen    - Price RM20" << endl << endl;
	cout << "[9] Proceed with the payment" << endl;
	cin >> choice;

	if (choice == 1)
	{
		cout << "\tYou chose: 2 adult and 3 kid pakcage" << endl << endl;
		price = 70;
	}

	else if (choice == 2)
	{
		cout << "\tYou chose: adult package" << endl << endl;
		price = 30;
	}
	else if (choice == 3)
	{
		cout << "\tYou chose: kid package" << endl << endl;
		price = 15;
	}
	else if (choice == 4)
	{
		cout << "\tYou chose: Senior Citizen package" << endl << endl;;
		price = 20;
	}
	else if (choice == 9)
	{
		goto payment;
	}

	current_total = current_total + price;
	cout << "Current total price:\t" << current_total << endl;
repeat_question:
	cout << "Want to add more?\n\n";
	cout << "[1] Yes\n";
	cout << "[2] No\n\n";
	cin >> selected;

	if (selected == 1) {
		goto startback;
	}
	else if (selected == 2)
	{

	}
	else {
		goto repeat_question;
	}

payment:
	system("cls");
	cout << "Current total price:\t" << current_total << endl;
	cout << "[1]Got mykad" << endl;
	cout << "[2]No mykad" << endl;
	cin >> mykad;

	if (mykad == 1)
	{
		mykad_dis = current_total*0.15;
	}
	else if (mykad == 2)
	{
		mykad_dis = 0;
	}

	cout << "[1] Got Mbo Card" << endl;
	cout << "[2] No Mbo Card" << endl;
	cin >> mbocard;

	if (mbocard == 1)
	{
		mbocard_dis = current_total*0.05;
	}
	else if (mbocard == 2)
	{
		mbocard_dis = 0;
	}



	total_price = current_total - mykad_dis - mbocard_dis;
	cout << "Total price is\t RM" << total_price << endl;

	bool payment_complete;

	payment_complete = false;

	while (!payment_complete) {

		cout << "Please enter payment" << endl;
		cin >> payment;

		if (payment >= total_price)
		{
			payment_complete = true;

			if (payment > total_price) {
				balance = payment - total_price;
				cout << "Your balance is:\t" << balance << endl;
			}

			cout << "Thanks for purchasing\n";

		}
		else
		{
			cout << "Your money is not enough. Please try again" << endl;
		}
	}
	cout << "\n[10] Exit program" << endl;
	cout << "[9] Restart system" << endl;
	cin >> exit;
	if (exit == 10){
		system("cls");
		cout << "Thanks for using this system\n";
	exit:
		system("pause");
	}
	else if (exit == 9){
		goto startback;
	}
	else
	{
		cout << "invalid input" << endl;

	}
}


This code will cause an infinitive loop. What is the criteria to get out of the loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	while (1) // Since it is stating the obvious, then it will keep looping.
	{
		system("cls");
		cout << "Please enter password: ";
		cin >> password;

		if (password == 123456) //This sets the condition for success
		{
			cout << "Access Granted!!!!";
		}

		else if (password != 123456) //This sets the condition for failure

		{
			cout << "Wrong password" << "\n" << x << " " << "wrong attempts" << "\n";
			++x;
		}
		if (x > 3)
		{
			goto exit;
		}
	}


For example:
In the loop below, If number had been initialized with
the value 5 or greater, as shown in the following program segment, the loop would never
execute:
1
2
3
4
5
6
int number = 6;
while (number < 5)
{
cout << "Hello\n";
number++;
}


An important characteristic of the while loop is that the loop will never iterate if the test
expression is false to start with. If the variable number is less than 5, then the loop will execute.
Last edited on
Topic archived. No new replies allowed.