Multifunction Program Assisstance

Pages: 123
Only complex ideas make it easier for me to understand program intricacies.

Working on a multifunction program which starts out utilizing a case switch between 2 choices, and then expands the function availability based on the primary choice selection.

Id like to include: I/O, Variables, conditional statements, loops, modules, arrays, and be able to append and display data.

So far this is all what I have, isn't much. All commented areas are what I unsure of. Since this is a multifunction, is it possible to shorten the source by creating a header file to which the source can read and know what to do?

As for the different cases specifically in void adminControl and userAccess, do I have to declare/identify each of them as I did with admin and user at the start of 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <string>

using namespace std;

void menu(void);
void adminControl(void);
void userAccess(void);

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

void menu(void) // Select Admin or User
{
	char choice = ' ';

	cout << "Select Level of Authority: " << endl << endl;
	cout << "(A)dmin, (U)ser" << endl;
	cin >> choice;

	while (choice = 'A' || choice = 'U') //not sure why there is an error here
	{
		switch (choice)
		{
		case 'A':
			adminControl();
			break;
		case 'U':
			userAccess();
			break;
		}
	}
}

void adminControl(void)  // Modify user access, Display Files, Update passcode
{ 
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(M)odify User, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice = ' ' || choice = ' ')
	{
		switch (choice)
		{
		case 'M':
			useModify();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}

	}
}

void userAccess(void)  //Read or Append files, update own password
{
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice = ' ' || choice = ' ')
	{
		switch (choice)
		{
		case 'A':
			appendFile();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}

	}
}

/*	int main() //this code program will be used for both Admin and Users
{
		int passcode = 1234;     //how would i modify this for different user passcodes, set length and limitations for length, alphanumerical inputs, and special characters?
		int input;
		int tries = 3;

		cout << "Enter passcode";
		cin >> input;

		if (input == passcode)  //passcode loop
			cout << "Success!\n";

		else if (input != passcode)        //You need a condition for your else if statement

		{
			while (input != passcode)
			{
				cout << "Sorry try again, you have " << tries-- << " left.\n";
				cin >> input;

				if (tries == 0 && input !=passcode)
				{
					cout << "Sorry you have exceeded the maximum attempts\n";
					return 0;
				}

				else if (tries == 0 && input == passcode)    // this ensures that the program recognizes the correct input even on the last attempt otherwise I found that my program just ends if I don't add this.
				{
					cout << "Success!\n";
				}
				else if (input == passcode) // I found that if I don't have this then the program does not work if you input the correct passcode on any attempts other than the first one.
				{
					cout << "Success!\n";
				}
			}
		}
		return 0;
}*/


Any advice or assistance would be appreciated, thank you.
Hello codingN00b2017,

One of the first things I see are at lines 27, 49 and 75. Did you mean to use (==) instead of setting choice" equal to (=).

Second you are checking for capital letters only. What happens if the user enters a lower case letter?

Something you could do is:

1
2
3
4
5
cout << "Select Level of Authority: " << endl << endl;
cout << "(A)dmin, (U)ser" << endl;
cin >> choice;

choice = toupper(choice);  // <--- Added. 


Two other options are to use "toupper()" in the while condition or also check for lower case letters. I recommend using the "toupper()" or "tolower()" functions.

I think I understand the use of the while loop, but I think it could be done differently.

I have to check out the program some more and let you know if I find anything else.

Hope that helps,

Andy
@Handy Andy
Thanks for pointing out the '=='. Its always something you don't think about. Fixed the errors immediately.

I am sure that the way i am coding this could be done differently and quite possibly more efficiently lol. I am still a n00b at this, so any input (<<--haha) is appreciated.

so the toupper function specifies that only upper case letters can be used and doies nothing if lower case are used?
Last edited on
Here is the updated 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <string>

using namespace std;

void menu(void);
void adminControl(void);
void userAccess(void);
void useModify(void);
void appendFile(void);
void displayFile(void);
void updatePasscode(void);


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

void menu(void) // Select Admin or User
{
	char choice = ' ';

	cout << "Select Level of Authority: " << endl << endl;
	cout << "(A)dmin, (U)ser" << endl;
	cin >> choice;

	choice = toupper(choice);

	while (choice == 'A' || choice == 'U')
	{
		switch (choice)
		{
		case 'A':
			adminControl();
			break;
		case 'U':
			userAccess();
			break;
		}
	}
}

void adminControl(void)  // Modify user access, Display Files, Update passcode
{ 
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(M)odify User, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice == 'M' || choice == 'D')
	{
		switch (choice)
		{
		case 'M':
			useModify();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}

	void useModify(void)
	{

	}

	void displayFile(void)
	{

	}

	void updatePasscode(void)
	{
		/*int main() //Password Change Code
		{
		const int MAX_CHARS_PER_LINE = 1000;
		string pepper;
		ifstream sauce("C:/users/yournamehere/desktop/Program variable files/User Info.txt");
		char charsFromFile[MAX_CHARS_PER_LINE];
		sauce.getline(charsFromFile, MAX_CHARS_PER_LINE);

		cout << "Enter the password: " << endl;
		cin >> pepper;

		if (pepper == charsFromFile)
		{
		//code
		}
		else
		{
		cout << "Intruder! Leave now!";
		}
		cin.get();
		return 0;
		}*/
	}
}

void userAccess(void)  //Read or Append files, update own password
{
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice == 'A' || choice == 'D' || choice == 'U')
	{
		switch (choice)
		{
		case 'A':
			appendFile();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}

	void appendFile(void)
	{

	}

	void displayFile(void)
	{

	}

	void updatePasscode(void)
	{
		/*int main() //Password Change Code
		{
		const int MAX_CHARS_PER_LINE = 1000;
		string pepper;
		ifstream sauce("C:/users/yournamehere/desktop/Program variable files/User Info.txt");
		char charsFromFile[MAX_CHARS_PER_LINE];
		sauce.getline(charsFromFile, MAX_CHARS_PER_LINE);

		cout << "Enter the password: " << endl;
		cin >> pepper;

		if (pepper == charsFromFile)
		{
		//code
		}
		else
		{
		cout << "Intruder! Leave now!";
		}
		cin.get();
		return 0;
		}*/
	}
}

/*	int main() //this code program will be used to verify both Admin and User password. Not sure how to specify requirements for proper password.
{
		int passcode = 1234;     //how would i modify this for different user passcodes, set length and limitations for length, alphanumerical inputs, and special characters?
		int input;
		int tries = 3;

		cout << "Enter passcode";
		cin >> input;

		if (input == passcode)  //passcode loop
			cout << "Success!\n";

		else if (input != passcode)        //You need a condition for your else if statement

		{
			while (input != passcode)
			{
				cout << "Sorry try again, you have " << tries-- << " left.\n";
				cin >> input;

				if (tries == 0 && input !=passcode)
				{
					cout << "Sorry you have exceeded the maximum attempts\n";
					return 0;
				}

				else if (tries == 0 && input == passcode)    // this ensures that the program recognizes the correct input even on the last attempt otherwise I found that my program just ends if I don't add this.
				{
					cout << "Authorized!\n";
				}
				else if (input == passcode) // I found that if I don't have this then the program does not work if you input the correct passcode on any attempts other than the first one.
				{
					cout << "Intruder!\n";
				}
			}
		}
		return 0;
}*/

Last edited on
Hello codingN00b2017,

I have found it is best to work on one small piece of the program at a time. In this case the "menu" function.

It is best for a function to do one thing and do it well. Although what you have done is workable the following code will show you a different approach.

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
char menu(void);
//void adminControl(void);
//void userAccess(void);

int main()
{
	char choice{};

	choice = menu();

	switch (choice)
	{
		case 'a':
		case 'A':
			std::cout << "\n Case A" << std::endl;
			break;

		case 'u':
		case 'U':
			std::cout << "\n Case U" << std::endl;
			break;

		case 'Q':
			std::cout << "\n Case Q" << std::endl;
			break;

		default:
			break;
		// error message.
	}

	return 0;
}

char menu(void) // Select Admin or User
{
	char choice = ' ';

	do
	{
		cout << "\nSelect Level of Authority: " << endl << endl;
		cout << "(A)dmin, (U)ser (Q)uit";
		cin >> choice;
		choice = toupper(choice);

		if (choice != 'A' && choice != 'U' && choice != 'Q')
			std::cout << "\n Invalid entry. Try again.\n";

	} while (choice != 'A' && choice != 'U' && choice != 'Q');

	return choice;
}


The use of the two case statement for lower case letters are not necessary. They just show you how you can deal with each case for the letters. Using "toupper()" or the extra case statements either way will work, but both are not needed together.

The case statements in main will need to be changed to call the necessary functions.

so the toupper function specifies that only upper case letters can be used and does nothing if lower case are used?
No. The "toupper()" function only changes the case of the character and it only works on one character at a time. If the case is correct nothing happens Otherwise the case is changed. The opposite would be "tolower()" to end up with lower case letters.

Hope that helps,

Andy
@Handy Andy
So the toupper and tolower functions act as converters, changing input state to required state?

how should they be changed? I thought that line 48 is a call to line 39?
Hello codingN00b2017,

So the toupper and tolower functions act as converters, changing input state to required state?

Yes "toupper()" and "tolower()" are converters. It changes the input form cin >> choice; for the while condition you have
while (choice == 'A' || choice == 'U'). Since you are using capital letters in the while condition "toupper()" will save you from writing
while (choice == 'A' || choice == 'a' || choice == 'U' || choice == 'u' || choice == 'Q' || choice == 'q') to cover both upper and lower case letters. Another alternative is
while (toupper(choice) == 'A' || toupper(choice) == 'U'). That is why I like the "toupper()" to follow the "cin >>".

I thought that line 48 is a call to line 39?
No, you are thinking backwards. Line 39 is the function call to line 48 is the function definition.

I will load up your new code and see how it works.

Hope that helps,

Andy
@Handy Andy
So then anything after adminControl or userAccess, are just the definitions of the functions when they are called?

As in: At the start of this, if (A) is selected then everything within the void adminControl(void) is called to be operated? Same with the userAccess(void)?

Now if that is the case, then how would it work to access, create or modify files within a location on the computer? is this where ofstream and ifstream come into play?

as an example: at line 17 i would add const char FileName[] = "TestAddress.txt"; correct?
Last edited on
Hello codingN00b2017,

Before I get to your latest questions.
You should compile the program and try to fix any errors before posting. At least the errors may bring up other questions. This is what your program should look like when set up properly. I left out the commented section at the bottom.

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
#include <iostream>
#include <fstream>
//#include <cstring>  // <--- With "<string>" there is no need for this line. And if you need it then something is wrong.
#include <cctype>
#include <string>

//using namespace std;  // <--- Best not to use.

void menu(void);
void adminControl(void);
void userAccess(void);
void useModify(void);
//void appendFile(void);
void displayFile(void);
void updatePasscode(void);

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

void menu(void) // Select Admin or User
{
	char choice = ' ';

	std::cout << "Select Level of Authority: " << std::endl << std::endl;
	std::cout << "(A)dmin, (U)ser" << std::endl;
	std::cin >> choice;

	choice = toupper(choice);

	while (choice == 'A' || choice == 'U')
	{
		switch (choice)
		{
		case 'A':
			adminControl();
			break;
		case 'U':
			userAccess();
			break;
		}
	}
}

void adminControl(void)  // Modify user access, Display Files, Update passcode
{
	char choice = ' ';

	std::cout << "What would you like to do?" << std::endl;
	std::cout << "(M)odify User, (D)isplay Files, (U)pdate Passcode" << std::endl;
	std::cin >> choice;

	while (choice == 'M' || choice == 'D')
	{
		switch (choice)
		{
		case 'M':
			useModify();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}
}

void useModify(void)
{

}

void displayFile(void)
{

}

void updatePasscode(void)
{
	/*int main() //Password Change Code
	{
	const int MAX_CHARS_PER_LINE = 1000;
	string pepper;
	ifstream sauce("C:/users/yournamehere/desktop/Program variable files/User Info.txt");
	char charsFromFile[MAX_CHARS_PER_LINE];
	sauce.getline(charsFromFile, MAX_CHARS_PER_LINE);

	std::cout << "Enter the password: " << std::endl;
	std::cin >> pepper;

	if (pepper == charsFromFile)
	{
	//code
	}
	else
	{
	std::cout << "Intruder! Leave now!";
	}
	std::cin.get();
	return 0;
	}*/
}


void userAccess(void)  //Read or Append files, update own password
{
	char choice = ' ';

	std::cout << "What would you like to do?" << std::endl;
	std::cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode" << std::endl;
	std::cin >> choice;

	while (choice == 'A' || choice == 'D' || choice == 'U')
	{
		switch (choice)
		{
		case 'A':
			//appendFile();  // <--- Not defined yet.
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}
}


In the "adminControl" function:
Line 71 should be the closing } of the function.

Lines 72 - 106 are separate functions that need to be outside of the "adminControl" function.

Line 107 needs removed. You are closing the "adminControl" function to late.

In the "userAccess" function case A there is no definition for this function yet. I just commented it out for now.

This should get you to a point where you have code that compiles and runs so you can test it.

Hope that helps,

Andy
Hello codingN00b2017,

So then anything after adminControl or userAccess, are just the definitions of the functions when they are called?
Yes.

As in: At the start of this, if (A) is selected then everything within the void adminControl(void) is called to be operated? Same with the userAccess(void)?
Yes. The use of "void" in place of the parameters is not needed. Just adminControl() will suffice.

Now if that is the case, then how would it work to access, create or modify files within a location on the computer? is this where ofstream and ifstream come into play?
I would first things first. Get the program working before you try working with files. If you are not use to using files it will not be easy trying to everything at once. Some part may need to be skipped for now or get the case statements to just call the needed functions first.

as an example: at line 17 i would add const char FileName[] = "TestAddress.txt"; correct?
I would use a "std::string" for this not a C string. If your compiler is set to use the C++11 standards a "std::string" will work. But yes and it would look like: const std::string fileName{"TestAddress.txt"};. With the {}s known as a uniform initializer.

While I am here. The prototypes on lines 25 - 31 along with their function definitions do not need the "void" in the ()s. The use of "void" in the parameters list was changed in the C++98 or later standards. Sorry I am not sure when that happened. These days "void" us mostly used for the return type of functions that return nothing.

Last the "menu" function is not right. The while loop will be an endless loop because the value of choice is never changed. So as long as "choice" is either 'A' or 'U' the while will always be true. Refer back to the code I showed you for main and menu.

Hope that helps,

Andy
Hello codingN00b2017,

In my last post I mentioned the while loops being endless loops. As an example of what I mean put this line std::cout << "\n In useModify"; in the "useModify" function run your program and see what happens. It is a good learning experience.

Andy
Last edited on
Hello Handy Andy

I didn't get to use the code snippet you offered yet.

Tried building the code first and received 3 errors:
1-unresolved external symbol "void_cdecl useModify(void)" (?useModify@@YAXXZ referenced
in function "void_cdecl adminCOntrol(void)"(adminControl@@YAXXZ)
2-unresolved external symbol "void_cdecl displayFile(void)" (?displayFile@@YAXXZ referenced
in function "void_cdecl adminCOntrol(void)"(adminControl@@YAXXZ)
3-unresolved external symbol "void_cdecl updatePasscode(void)" (?updatePasscode@@YAXXZ
referenced in function "void_cdecl adminCOntrol(void)"(adminControl@@YAXXZ)

Each error is directed at line 1 which is #include <iostream>
Those are linker errors. (Not about iostream.)

Like: "You gave me object file that implements function adminCOntrol() and it calls functions useModify(), displayFile() and updatePasscode(), but you did not give me any object file that would contain the (binary) code of those three functions. Give me more to link!"
Last edited on
Hello codingN00b2017,

You should look at the error message closer. On my VS 2015 an error message like that says it points to line 1 of the ".obj" file not in main. Usually this means that something is misspelled or you are trying to call a file that has not been defined yet.

I had the problem in the "userAccess" function with case 'A' calling a function that is not yet defined.

The errors are not that bad. Just define the functions that you do not have like you did with "useModify()". You could also define the function like this:

1
2
3
4
5
void useModify(void)
{
	std::cout << "\n In useModify function.";
}


Which will let you know where you are when testing.

Hope that helps,

Andy
keskiverto
I figured it wasn't about the iostream. I was just offering information so i could be pointed in the right direction as to what went wrong.

Handy Andy
So i do not fully know what i did, but the errors were gone after: I commented each of the prototypes, the list of void items, one by one and built the code. When none were commented, it still built the code with no issues or warnings.

I also comment paragraphed everything after the char menu code., Figured it would help find any errors easier as i went thru 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

char menu(void);
void adminControl(void);
void userAccess(void);
void useModify(void);
void appendFile(void);
void displayFile(void);
void updatePasscode(void);


int main() //First Menu Options
{
	char choice{};

	choice = menu();

	switch(choice)
	{
	case 'A':
		cout << "\nCase A" << endl;
	case 'U':
		cout << "\nCase U" << endl;
	case 'Q':
		cout << "\nCase Q" << endl;

	default:
		break;
	}
	return 0;
}

char menu(void) // Select Admin or User
{
	char choice = ' ';

	do
	{
		cout << "Select Level of Authority: " << endl << endl;
		cout << "(A)dmin, (U)ser" << endl;
		cin >> choice;
		choice = toupper(choice);

		if (choice != 'A' && choice != 'U' && choice != 'Q')
			cout << "\n Invalid Entry! Please Try Again!\n";
	} 
	while (choice != 'A' && choice != 'U' && choice != 'Q');
	return choice;
}

void adminControl(void)  // Modify user access, Display Files, Update passcode
{
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(M)odify User, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice == 'M' || choice == 'D')
	{
		switch (choice)
		{
		case 'M':
			useModify();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}
}
	
	void useModify(void)
	{
		cout << "\n In useModify Function.";

	}

	void displayFile(void)
	{
		cout << "Display File." << endl;
	}

	void updatePasscode(void)//Password Change Code
	{

	}

void userAccess(void)  //Read or Append files, update own password
{
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice == 'A' || choice == 'D' || choice == 'U')
	{
		switch (choice)
		{
		case 'A':
			appendFile();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}


	void appendFile(void)
	{
		cout << "Append File" << endl;
	}

	void displayFile(void)
	{
		cout << "Display File." << endl;
	}

	/*void updatePasscode(void)
	{
		/*int main() //Password Change Code
		{
		const int MAX_CHARS_PER_LINE = 1000;
		string pepper;
		ifstream sauce("C:/users/yournamehere/desktop/Program variable files/User Info.txt");
		char charsFromFile[MAX_CHARS_PER_LINE];
		sauce.getline(charsFromFile, MAX_CHARS_PER_LINE);

		cout << "Enter the password: " << endl;
		cin >> pepper;

		if (pepper == charsFromFile)
		{
		//code
		}
		else
		{
		cout << "Intruder! Leave now!";
		}
		cin.get();
		return 0;
		}
	}*/
}

/*	int main() //this code program will be used for both Admin and Users
{
		int passcode = 1234;     //how would i modify this for different user passcodes, set length and limitations for length, alphanumerical inputs, and special characters?
		int input;
		int tries = 3;

		cout << "Enter passcode";
		cin >> input;

		if (input == passcode)  //passcode loop
			cout << "Success!\n";

		else if (input != passcode)        //You need a condition for your else if statement

		{
			while (input != passcode)
			{
				cout << "Sorry try again, you have " << tries-- << " left.\n";
				cin >> input;

				if (tries == 0 && input !=passcode)
				{
					cout << "Sorry you have exceeded the maximum attempts\n";
					return 0;
				}

				else if (tries == 0 && input == passcode)    //ensures program recognizes correct input, program just ends if I don't add this.
				{
					cout << "Authorized!\n";
				}
				else if (input == passcode) // if I don't have this, program does not work if you input the correct passcode on any attempts other than the first one.
				{
					cout << "Intruder!\n";
				}
			}
		}
		return 0;
}*/


/*int main() //Password Set Code
{
	string password;
	
	cout << "Set the password" << endl;
	ofstream myfile("C:/users/youtube area/desktop/Program variable files/User Info.txt");
	
	if (myfile.is_open())
	{
		cin >> password;
		myfile << password;
		cout << "Your new password is " << password << endl;
	}
	return 0;
}*/

Errors occur within user access for appendFile and displayFile, saying local function definitions are illegal. I am guessing it has something to do with the brackets? I've looked multiple time and I do not seem to be missing any..
Last edited on
1. Where is the closing brace of the function userAccess() ?

2. What is on line 157?
keskiverto
Line 157 is the closing brace for userAccess. Or it is supposed to be. Unless i am wrong in thinking that the functions for userAccess are supposed to braced in the userAccess function.

(userAccess(Append,Display, Update))<--right?
Last edited on
Error: local function definitions are illegal.

Suppose that the compiler disagrees with you.

Implementing a function inside other function is illegal.
1
2
3
4
5
6
7
8
9
void foo()
{
  // code

  void bar() // Error: local function definition
  {
    // code
  }
}


You must write:
1
2
3
4
5
6
7
8
9
void foo()
{
  // code
}

void bar()
{
  // code
}



Besides, you are not consistent. If functions are supposed to be inside functions, why are lines 81-95 outside of adminControl() ? The compiler is happy about that. Why are not all of your functions nested inside the body of main() ? Surely, if inside is supposed, then you must go all the way.

1
2
3
4
int main() {
  string password; // this line has implicit function call.
  // Where is the implementation of that function?
}




No. Functions are not supposed to be inside. Each function is separate, independent block of code. Linker connects them, if one function calls an another.



Exception: C++11 lambda closures. They are inside.
Last edited on
codingN00b2017,

Line 120 is missing the closing } of the "userAccess" function.

The function definition at line 127 is a duplicate. you need to delete that one.

Once the program compiles why not tell me what is wrong. It looks like you have taken a step backwards. The switch in main just prints what case it is and ends. Not what you had before.

Your function and prototypes like "char menu(void)" all contain "void" inside the (). This is not needed, but if you insist someone else will eventually tell you the same thing.

At least now that the program does not work you can get the switch in main to work for the "adminControl()" function then work on the "adminControl()" function and get it to work. As it is in the "adminControl()" function the while loop is an endless loop that needs fixed. Now is a good time to fix this before the other function are written.

Hope that helps,

Andy
Hello codingN00b2017,

To give you an idea of what I mean about working on one part at a time. The "menu" function is working properly, I would consider adding an option for ending the program at this point.

The following code is from your latest post with corrections and some comments. It will give you an idea of how to work on one part at a time. The concept of the "adminControl" function can be duplicated in the "userAccess" function.

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
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <limits>  // <--- Added.

using namespace std;

char menu(void);  // <--- "void" not needed inside the (). Same for all.
void adminControl(void);
void userAccess(void);
void useModify(void);
void appendFile(void);
void displayFile(void);
void updatePasscode(void);


int main() //First Menu Options  < --- Entry point of prgram.
{
	char choice{};

	choice = menu();

	switch (choice)
	{
	case 'A':
		//cout << "\nCase A" << endl;
		adminControl();
		break;
	case 'U':
		cout << "\nCase U" << endl;  // <--- Missing "break" statement.
	case 'Q':
		cout << "\nCase Q" << endl;  // <--- Missing "break" statement.

	default:
		break;
	}

	std::cout << "\n\n\n Press enter to continue. ";  // <--- used to keep the console window open.

	// <--- This one clears the input buffer after a "cin >> to a variable.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	// <--- This one waits for enter.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	return 0;
}

char menu(void) // Select Admin or User
{
	char choice = ' ';

	do
	{
		cout << "\nSelect Level of Authority: " << endl << endl;
		cout << "(A)dmin, (U)ser: ";
		cin >> choice;
		choice = toupper(choice);

		if (choice != 'A' && choice != 'U' && choice != 'Q')
			cout << "\n Invalid Entry! Please Try Again!\n";

	} while (choice != 'A' && choice != 'U' && choice != 'Q');

	return choice;
}

void adminControl()  // Modify user access, Display Files, Update passcode
{
	char choice = ' ';
	bool cont{ true };

	do
	{
		cout << "\nWhat would you like to do?" << endl;
		cout << "(M)odify User, (D)isplay Files, (U)pdate Passcode, (Q)uit: ";
		cin >> choice;
		choice = toupper(choice);  // <--- Added.

		switch (choice)
		{
		case 'M':
			useModify();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		case 'Q':
			cont = false;
			break;
		default:  // <--- Shoul have a default.
			std::cout << "\n Invalid choice. Try again" << std::endl;
			break;
		}
	} while (cont);
}

void useModify(void)
{
	cout << "\n In useModify Function." << std::endl;

}

void displayFile(void)
{
	cout << "Display File." << endl;  // This is a function not a file.
}

void updatePasscode(void)//Password Change Code
{
	std::cout << "\n In Update Passcode function\n";
}

void userAccess(void)  //Read or Append files, update own password
{
	char choice = ' ';

	cout << "What would you like to do?" << endl;
	cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode" << endl;
	cin >> choice;

	while (choice == 'A' || choice == 'D' || choice == 'U')
	{
		switch (choice)
		{
		case 'A':
			appendFile();
			break;
		case 'D':
			displayFile();
			break;
		case 'U':
			updatePasscode();
			break;
		}
	}
}   // End userAccess function  <--- Missing closing }. 

void appendFile(void)
{
	cout << "Append File" << endl;
}

// <--- Duplicate definition.
//void displayFile(void)
//{
//	cout << "Display File." << endl;
//} 


Hope that helps,

Andy
Pages: 123