Multifunction Program Assisstance

Pages: 123
1
2
"main" could use a do/while loop from "choice = menu(); to the end of the switch.
The same for "adminControl" and "userAccess". Otherwise it could be a short program. 

Do you mean like the do/while used in char menu?

Would I be wrong in keeping the cout statements? I figure they would be good selection headers, like when you login to your site account and it takes you to your account page.

I try not to remove anything, only comment it out if i can/remember to. Which part(s) do you mean?
Last edited on
Hey Handy Andy

Im currently reading on how to set separate files to admin and user displayFile function.
Handy Andy

So i added some new lines:
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
void displayUserData();
void displayFileData();

const string PATH_BETA { "C:\\Users\\ImTheUser\\Desktop\\Program variable files\\EditMe.txt" };
const string FILENAME_BETA { "EditMe.txt" };
const string fileNAME_BETA { PATH_BETA + FILENAME_BETA };

switch (choice)
		{
		case 'M':
			cout << "\nModify User" << endl;
			useModify();
			break;
		case 'D':
			cout << "\nDisplay File" << endl;
			displayData();
				switch (choice)
				{
					case 'P':
						cout << "Public File" << endl;
						displayFileData();
						break;
					case 'C':
						cout << "Classified Data" << endl;
						displayUserData();
						break;
				}
			break;
		case 'U':
			cout << "\nUpdate Passcode" << endl;
			updatePasscode();
		case 'Q':
			cont = false;
			break;
		default:
			cout << "\n Unknown Choice! Please Try Again!" << endl;
			break;
		}
	while (cont);

	void displayFileData(void)
	{
		string showData;
		ifstream openfile("C:\\Users\\ImTheUser\\Desktop\\Program variable files\\EditMe.txt");
		if (openfile.is_open())
		{
			//while(getline(stream, var))
			while(! openfile.eof())			
			{
				getline(openfile, showData);
				cout << showData << endl;
			}
		}
	}

	void displayUserData(void)
	{
		string showData;
		ifstream openfile("C:\\Users\\ImTheUser\\Desktop\\Program variable files\\User Info.txt");
		if (openfile.is_open())
		{
			//while(getline(stream, var))
			while (!openfile.eof())
			{
				getline(openfile, showData);
				cout << showData << endl;
			}
		}
	}


currently only issue is linking displayData correctly within adminControl.
Hello codingN00b2017,

Do you mean like the do/while used in char menu?
Yes. The same concept.

Would I be wrong in keeping the cout statements? I figure they would be good selection headers, like when you login to your site account and it takes you to your account page.
Probably. I do not quite follow what you mean. It my also depend on which "cout" statement you mean.

I try not to remove anything, only comment it out if i can/remember to. Which part(s) do you mean?
Honestly I do not remember which part(s) I was talking about. It could have been something I did, but you had not done yet. What I do remember is that the next to last version you posted I had working on my computer and when I switched to your latest version it fell apart and needed some fixing to work. I will show you the code I have working so you can see what I have done. Check the comments in the code for what I have changed or added.

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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <limits>
#include <chrono>
#include <thread>
//#include <cmath>


using namespace std;

/*struct UserData  // <--- Wort keeping like this. May be usful in the future.
{
	int userID;
	string first_name;
	string last_name;
	string clearance;
	string status;
	string location;
};*/

char menu();
void adminControl();
void userAccess();
void useModify();
//void openInputFile(ifstream &, string);
//void print(UserData[]);
//void sort(UserData[]);
void displayData();
void updatePasscode();
void writeData();
//string * split(string, char);  // <--- This on not needed. Nofunction definition.

const std::string PATH{ "C:\\Users\\xX - VENGEANCE - Xx\\Desktop\\Program variable files\\" };
const std::string FILENAME{ "User Info.txt" };
//const std::string fileName{ PATH + FILENAME };  // <--- Change comment for the version you need.
const std::string fileName{ FILENAME };  // <--- Changed "f" in first "fileName"from capital to lower case.


int main() //First Menu Options <--- Actually it is the start of the program.
{
	ofstream out;
	bool cont{ true };  // <--- Added.
	char choice{};

	out.open(fileName, std::ios::app);  // <--- Changed to use the vriable from what you had. It makes it eaier.

	do  // <--- Added the whole do/while.
	{
		choice = menu();

		switch (choice)
		{
		case 'A':
			cout << "\nAdministrative Control" << endl;
			adminControl();
			break;
		case 'U':
			cout << "\nUser Access" << endl;
			userAccess();
			break;
		case 'Q':
			cout << "\nCase Q" << endl;
			cont = false;  // <--- Added.
			break;
		default:
			break;
		}
	} while (cont);  // <--- Added.

	return 0;
}

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

	do
	{
		cout << "Select Level of Authority: " << endl << endl;
		cout << "(A)dmin, (U)ser, (Q)uit" << 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 = ' ';
	bool cont{ true };

	do  // <--- Added the do and opening { along with the closing } before the while.
	{
		cout << "What would you like to do?" << endl;
		cout << "(M)odify User, (D)isplay Files, (U)pdate Passcode, (Q)uit" << endl;
		cin >> choice;
		choice = toupper(choice);

		switch (choice)
		{
		case 'M':
			cout << "\nModify User" << endl;
			useModify();
			break;
		case 'D':
			cout << "\nDisplay File" << endl;
			displayData();
			break;
		case 'U':
			cout << "\nUpdate Passcode" << endl;
			updatePasscode();
			break;  // <--- Added.
		case 'Q':
			cont = false;
			break;
		default:
			cout << "\n Unknown Choice! Please Try Again!" << endl;
			break;
		}
	} while (cont);  // <--- Added the closing }
}

void useModify(void)
{
	/*const int SIZE = 9;
	const int INFO = 4;

	//Variables
	UserData arr[SIZE];

	ifstream inFile;
	string inFileName = "C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txt";

	// Call function to read file
	openInputFile(inFile, inFileName);

	//Read data into array
	for (int count = 0; count < SIZE; count++);
	{
	/*inFile >> arr[count].userID >> arr[count].first_name >> arr[count].last_name >>
	arr[count].clearance >> arr[count].status >> arr[count].location;
	}
	inFile.close();

	//Print Unsorted Data
	print(arr);
	cout << "\n";

	//Sort
	sort(arr);

	//Print Sorted
	print(arr);*/

}

void displayData(void)
{
	ifstream inMyStream(fileName);
	string name;
	string clearance;
	string status;
	string location;

	if (!inMyStream)
	{
		cout << "File Did Not Open!!" << endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	while (getline(inMyStream, name, ','))  // <--- Removed semicolon.
	{
		getline(inMyStream, clearance, ',');
		getline(inMyStream, status, ',');
		getline(inMyStream, location);  // <--- Removed third parameter or change it to'\n'.

		cout << "Name........" << name << endl;
		cout << "Clearance......" << clearance << endl;
		cout << "Status........" << status << endl;
		cout << "Location......." << location << endl << std::endl; // <--- Added second endl.
	}
	inMyStream.close();

	//  <--- This bit of code you do not really need to keep.
	/*string line;
	string lineBuffer;
	//ifstream myfile("C:\\Users\\xX - VENGEANCE - Xx\\Desktop\\Program variable files\\User Info.txt");
	ifstream myfile("C:\\Users\\xX - VENGEANCE - Xx\\source\\repos\\Passcode\\Passcode\\Debug\\User Info.txt");
	if (myfile.is_open())
	{
	while (getline(myfile, line))
	{
	getline(myfile, lineBuffer);
	cout << line << endl;
	}
	myfile.close();
	}
	else cout << "unable to Open File!";*/


}

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

	bool cont{ true };

	do  // <--- Added whole do/while
	{
		cout << "What would you like to do?" << endl;
		cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode, (Q)uit" << endl;
		cin >> choice;
		choice = std::toupper(choice);

		switch (choice)
		{
		case 'A':
			cout << "\nEdit File" << endl;
			writeData();
			break;
		case 'D':
			cout << "\nDisplay File" << endl;
			displayData();
			break;
		case 'U':
			cout << "\nUpdate Passcode" << endl;
			updatePasscode();
			break;
		case 'Q':
			cont = false;
			break;
		}
	} while (cont);
}

void writeData(void)
{
	char choice = ' ';
	string name = "";
	string clearance = "";
	string status = "";
	string location = "";
	ofstream outMyStream(fileName, ios::app);

	do
	{
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "\nEnter their Name: ";
		getline(cin, name);
		cout << "\nEnter their Clearance: ";
		getline(cin, clearance);
		cout << "\nEnter their Status: ";
		getline(cin, status);
		cout << "\nEnter their Location: ";
		getline(cin, location);

		outMyStream << name << "," << clearance << "," << status << "," << location;

		cout << "\nAdd Another Record?";
		cin >> choice;

	} while (choice == 'Y' || choice == 'y');  // <--- Changed right side of || to lower case.

	outMyStream.close();
}

void updatePasscode()  // <--- Added to eliminate compile error.
{
	std::cout << "\n In Update Pass Code\n";
}


I think the comments in the code cover everything I did. I do not believe I have missed anything.

Hope that helps,

Andy
Updated and functional 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <limits>
#include <chrono>
#include <thread>
//#include <cmath>


using namespace std;

/*struct UserData
{
	int userID;
	string first_name;
	string last_name;
	string clearance;
	string status;
	string location;
};*/

char menu();
void adminControl();
void userAccess();
void useModify();
void openInputFile(ifstream &, string);
//void print(UserData[]);
//void sort(UserData[]);
void displayClassifiedData();
void displayPublicData();
void updatePasscode();
void writeData();
//string * split(string, char);

const string PATH_ALPHA { "C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txt" };
const string FILENAME_ALPHA { "User Info.txt" };
const string fileNAME_ALPHA{ PATH_ALPHA + FILENAME_ALPHA };
const string PATH_BETA { "C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\EditMe.txt" };
const string FILENAME_BETA { "EditMe.txt" };
const string fileNAME_BETA { PATH_BETA + FILENAME_BETA };


int main() //First Menu Options
{
	ofstream out;
	bool cont{ true };
	char choice{};

	out.open(fileNAME_ALPHA, std::ios::app);
	out.open(fileNAME_BETA, std::ios::app);

	do
	{
		choice = menu();

		switch (choice)
		{
		case 'A':
			cout << "\nAdministrative Control" << endl;
			adminControl();
			break;
		case 'U':
			cout << "\nUser Access" << endl;
			userAccess();
			break;
		case 'Q':
			cout << "\nCase Q" << endl;
			cont = false;
			break;
		default:
			break;
		}
	} while (cont);
	return 0;
}

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

	do
	{
		cout << "Select Level of Authority: " << endl << endl;
		cout << "(A)dmin, (U)ser, (Q)uit" << 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 = ' ';
	bool cont{true};

	do
	{
		cout << "What would you like to do?" << endl;
		cout << "(M)odify User, (P)ublic File, (C)lassified Data, (U)pdate Passcode, (Q)uit" << endl;
		cin >> choice;
		choice = toupper(choice);

		switch (choice)
		{
		case 'M':
			cout << "\nModify User" << endl;
			useModify();
			break;
		case 'P':
			cout << "Public File" << endl;
			displayPublicData();
			break;
		case 'C':
			cout << "Classified Data" << endl;
			displayClassifiedData();
			break;
		case 'U':
			cout << "\nUpdate Passcode" << endl;
			updatePasscode();
		case 'Q':
			cont = false;
			break;
		default:
			cout << "\n Unknown Choice! Please Try Again!" << endl;
			break;
		}
	} while (cont);
}
	
	void useModify(void)
	{
		/*const int SIZE = 9;
		const int INFO = 4;

		//Variables
		UserData arr[SIZE];

		ifstream inFile;
		string inFileName = "C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txt";

		// Call function to read file
		openInputFile(inFile, inFileName);

		//Read data into array
		for (int count = 0; count < SIZE; count++);
		{
			/*inFile >> arr[count].userID >> arr[count].first_name >> arr[count].last_name >>
				arr[count].clearance >> arr[count].status >> arr[count].location;
		}
		inFile.close();

		//Print Unsorted Data
		print(arr);
		cout << "\n";

		//Sort
		sort(arr);

		//Print Sorted
		print(arr);*/

	}

	void displayPublicData(void)
	{
		string showData;
		ifstream openfile("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\EditMe.txt");
		if (openfile.is_open())
		{
			//while(getline(stream, var))
			while(! openfile.eof())			
			{
				getline(openfile, showData);
				cout << showData << endl;
			}
		}
	}

	void displayClassifiedData(void)
	{
		string showData;
		ifstream openfile("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txt");
		if (openfile.is_open())
		{
			//while(getline(stream, var))
			while (!openfile.eof())
			{
				getline(openfile, showData);
				cout << showData << endl;
			}
		}
	}

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

		bool cont{ true };

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

			switch (choice)
			{
			case 'A':
				cout << "\nEdit File" << endl;
				writeData();
				break;
			case 'D':
				cout << "\nDisplay File" << endl;
				displayPublicData();
				break;
			case 'U':
				cout << "\nUpdate Passcode" << endl;
				updatePasscode();
				break;
			case 'Q':
				cont = false;
				break;
			}
		} while (cont);
	}
	
	void writeData(void)
	{
		ofstream myFile("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\EditMe.txt");
		if (myFile.is_open())
		{
			myFile << "This is a test of my project.\n";
			myFile << "I have learned much since the start.\n";
			myFile.close();
		}
		else cout << "Unable to Access File!! Try Again Later!!";	
		
		/*char choice = ' ';
		string name = "";
		string clearance = "";
		string status = "";
		string location = "";
		ofstream outMyStream(FileName, ios::app);

		do
		{
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cout << "\nEnter their Name: ";
			getline(cin, name);
			cout << "\nEnter their Clearance: ";
			getline(cin, clearance);
			cout << "\nEnter their Status: ";
			getline(cin, status);
			cout << "\nEnter their Location: ";
			getline(cin, location);
			
			outMyStream << name << "," << clearance << "," << status << "," << location ;

			cout << "\nAdd Another Record?";
			cin >> choice;

		} while (choice == 'Y' || choice == 'Y');
		outMyStream.close();*/
	}
	
	/*string * split(string theLine, char theDeliminator);
	{
		int splitCount = 0;
		for (unsigned int i = 0; i < theLine.size(); i++);
		{
			if (theLine[i] == ',')
				splitCount++;
		}
		splitCount++;

		string * theFieldArray;
		theFieldArray = new string[splitCount];

		string theField = "";
		int commCount = 0;

		for (unsigned int i = 0; i < theLine.size(); i++);
		{
			if (theLine[i] != theDeliminator)
			{
				theField += theLine[i];
			}
			else
			{
				theFieldArray[commaCount] = theField;
				theField = "";
				commaCount++;
			}
		}
		theFieldArray[commaCount] = theField;
		return theFieldArray;*/


	void updatePasscode(void)//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();*/
	}
So now that I have it all working, I have 2 works to deal with.
1-useModify: i do not know if will keep this since it would be easier to see the editing working through a console text editor.
2-passcode functions: i have already built the passcode check function(tho commented them out) and designed the linker for them. When i uncomment one, it turns every cout into a wrong code..each cout becomes underlined in red. I have also somewhat written a password update/reset code which will be accessible from both Admin and User. I did find a preset password code, but i do not know how to integrate it. I would assume through the header file.

Last edited on
Hello Handy Andy and anyone watching this thread.

So I have setup my header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main()   //how would i modify this for different user passcodes, set length and limitations for length, alphanumerical inputs, and special characters?
{
	string stringfromfile = "";
	ifstream inputFile("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\Password.txt");
	if (inputFile.is_open())
	{
		getline(inputFile, stringfromfile);
		inputFile.close();
		cout << "Password is: " << stringfromfile << endl;
	}
	else
	{
		cout << "File Inaccessible!!" << endl;
	}

	return 0; 

}


Now I also have the Passcode function for Admin and User written but commented out for now.

If i #include "header.h" in to the source, is it just one line needed to connect the two function and complete the verification system?

Passcode Authorization within source:
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
void adminPasscode(void); //this code (when dual created) will test authoirization of Admin and User
{
	int input;
	int tries = 3;

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

	if (input == passcode)    //I know that this line should be: if(input == stringfromfile)
		cout << "Success!\n";
		adminControl();
		break;

	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";				
			}

			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;
}


The integration of the header code into this verification system is what I am having issue with. I am thinking that there is some <fstream> command line i have to use, but at the same time I feel that if the header file is included, the if(input...) line should be sufficient to bridge the connection.
Hello codingN00b2017,

I will try to cover your last three posts. here.

The two function that are left I am planning on looking into those today.

In the last post the first bit of code should not be a header file. And if you do something like this it should never contain main. This function is in the main file that was created when you started. Any file that contains code would have a ".cpp" extension.

Header files generally contain code for structs, classes and prototypes and rarely code, but not what you are trying to do.

The lines 2, 3 and 4 should never be in a header file and if you need one it would be rare.

As a note it is int main() not the void main() you have.

The second bit of code "adminPasscode" could be in a separate ".cpp" file, but with what you have done so far it is not necessary.

I would get the program working properly first before I would consider using a header file or other ".cpp" files. You could save doing that for a later project.

The integration of the header code into this verification system is what I am having issue with. I am thinking that there is some <fstream> command line i have to use, but at the same time I feel that if the header file is included, the if(input...) line should be sufficient to bridge the connection.
No. to include a header file all you need is #include "headre.h" . Notice the double quotes in place of the <>s. This tells the compiler that the file is local,i.e., where the ".cpp" files are stored, and not in the normal include directory. There is no special header file, like ( <fstream> )needed to include or use a header file.

Correct me if I am wrong, but I am guessing you are using some version of VS (Visual Studio)? I thought I read somewhere that you said some "cout" statements were underlined in red and that it was causing a problem. I had the same problem with the last code I worked on. It turned out to be caused be a missing closing } of a function.

In the full program code lines 36 - 41 have the right idea, but you went about it the wrong way. The variable name is "PATH_ALPHA" or "PATH_BETA". First you only need the path not the file name at the end. Think about you will get with "const string fileNAME_ALPHA{ PATH_ALPHA + FILENAME_ALPHA };". It would end up looking like C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txtUser Info.txt . Notice the extra file name. The idea here is to separate the path and file name into two variables so you can use what you need. When you look at "PATH_ALPHA" or "PATH_BETA" the only difference is the file name, so in the end only one path is needed and two different variables for the file names.

In the end I would stay away from header file or any extra ".cpp" files for this program and we can work on that concept with the next assignment unless you need them for this assignment.

Hope that helps,

Andy
Hello codingN00b2017,

As I started working on the "updatePasscode" function I have some questions.

It looks like you want to read a file of pass codes to compare with what is entered. If so would you post a sample of that file so I know what I am working with.

Why use char charsFromFile[MAX_CHARS_PER_LINE]; when a std::string will work fine. Did you have a point in using this C style array?

From what I have worked with so far I can see a better way of using this function.

Andy

Yes I am using VS 2017 Community, too broke to get a legit licensed copy haha.

1
2
3
4
5
const string PATH { "C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files" };
const string FILENAME_ALPHA { "User Info.txt" };
const string fileNAME_ALPHA{ PATH + FILENAME_ALPHA };
const string FILENAME_BETA { "EditMe.txt" };
const string fileNAME_BETA { PATH + FILENAME_BETA };


^^Did you mean in this manner?^^

Yes, when i uncommented the adminPasscode all couts would underline in red. I had looked for missing braces best I could considering id been up late and slept few hours through these days.

 
char charsFromFile[MAX_CHARS_PER_LINE];

This is an old product of reading and searching. it is no longer in the verification process.

As for the inputFile for the passcode, it is just a .txt document with a single word in it.
From what I read on such an input technique i wouldn't need anything else with the word in the file.
Hello codingN00b2017,

Yes. Dealing with the path and file name separate is what I meant.

I use VS 2015 right now and when I uncommented the code for "adminPasscode" function I did not have the problem with the "cout" statements, but I did have questions about the code.

I am not sure what you want to do with this function, so here is my idea:

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
void updatePasscode()
{
	std::string aPassCodes[MAXPASSCODES];
	std::string passCodeCheck;
	std::size_t index{};
	std::ifstream inFile(PASSCODESFILE);  // <--- I do not need the path.

	while (std::getline(inFile, aPassCodes[index]))
	{
		index++;
	}

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

	for (size_t lc = 0; lc < MAXPASSCODES; lc++)
	{
		if (passCodeCheck == aPassCodes[lc])
		{
			//code
			std::cout << "\n Pass Code accepted.\n" << std::endl;
			std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread"
			return;  // <--- No need to continue with the for loop or the rest of the function.
		}
	}

	cout << "Intruder! Leave now!\n" << std::endl;
	
	// <--- Could use a prompt here to let the user know that the program will be waiting.
	cin.get();
}


I hope the comments help you understand what I did.

Hope that helps,

Andy
Handy Andy
That previous post is to update the passcode.

I am trying to get the program to match and verify the preset passcode stored in a txt file with the input.

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
void userAuthVer() //this code (when dual created) will test authoirization of Admin and User
{
	string input;
	bool found = false;
	int tries = 3;

	{
		cout << "Enter Passcode: ";
		cin >> input;
	}

	string stringfromfileB;
	ifstream inputFile("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\Password 2.txt");
	getline (inputFile, stringfromfileB);
	if (input == stringfromfileB)  //passcode loop
	{
		cout << "Success!\n";
		adminControl();
	}

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

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

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

			else if (tries == 0 && input == stringfromfileB)    //ensures program recognizes correct input, program just ends if I don't add this.
			{
				cout << "Authorized!\n";
			}
			else if (input != stringfromfileB) // 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";
			}
		}
	}
}


this is the code to verify the input == filed preset passcode
FINALLY got the passcode verification system working
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
void adminAuthVer() //this code will test Authorization of Admin
{
	string input;
	bool found = false;
	
	{
		cout << "Enter Passcode: \n";
		cin >> input;
	}

	string stringfromfileB;
	ifstream openFile("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\Password 1.txt");
	getline(openFile, stringfromfileB);
	if (input.compare(stringfromfileB))  //passcode loop
	{
		found = true;
		cout << "Authorized!! \n" << endl;
		adminControl();
	}

	else if (input != stringfromfileB)
	{
		cout << "Access Denied!!\n" << endl;
	}
}

I tried about 5-7 different times to make it work more more, spanning about 2-3 hours lol. Ended up restarting this section completely. When i got to a certain point I realized where i could add a proper compare function...and in 5 minutes, had it working.
Last edited on
Hello codingN00b2017,

That is a good start, but if I had to choose between thee two versions I would get the first version working. It is a better version and it just needs teeked a little to work right.

For a start consider this for the beginning of the 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
void userAuthVer() //this code (when dual created) will test authoirization of Admin and User
{
	string input;
	bool found = false;
	int tries = 3;

	{  // <--- This is not needed, but OK if you leave it.
		cout << "Enter Passcode: ";  // <--- Not to be picky, but it should be "pass code",
		cin >> input;
	}  // <--- This is not needed, but OK if you leave it.

	string stringfromfileB;
	//ifstream inputFile(PATH + "Password 2.txt");  // <--- What your version should be.
	ifstream inputFile("PassCodes.txt");  // <--- Waht I am set up to use.

	if (!inputFile)  // <--- You should always check if the input file is open.
	{
		std::cout << "\n File did not open." << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	getline(inputFile, stringfromfileB);  // <--- Reads the first line of the file, but nothig else.

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

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

	{
		while (input != stringfromfileB)
		{
			cout << "Sorry try again, you have: " << tries-- << " left!\n";  // <--- "tries" is  good idea, but used in the wrong way.
			cout << "Enter Passcode: ";
			cin >> input;

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

			else if (tries == 0 && input == stringfromfileB)    //ensures program recognizes correct input, program just ends if I don't add this.
			{
				cout << "Authorized!\n";
			}
			else if (input != stringfromfileB) // 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";
			}
		}
	}
}


The comments in the code should help a bit. Line 13 of your code is going about defining the stream the hard way. That is what the variable "PTAH" is set up for. Notice my example of how you can use "PATH".

Line 14 reads the first line of the file and that is all it ever reads. A while loop to read the file like: while (getline(inputFile, stringfromfileB)) to read each line of the file and then check it against "input" against "stringfromfileB".

The while loop using "tries" is a good idea and useful, but it is not working the way it should. The way it is now it is saying that you have 3 tries left, but you have already used one try before the loop. So, you are off by one.

I will not say that the function will look the same by the time it is working properly, but you will be able to use most of what you have just in a different way.

As I showed you in my last post it can make the function much to read the entire file into an array or even better into a vector if you know about vectors. If you do not know about vectors I can help you learn.

This way in the function you will not have to deal with reading the file one line at a time to check every possibility before finding a match or not.

I am going to work on the first version and get it to work because I feel that it is the better version of the two. The second version still has some of the problems that the first version has and you will find that in the end even it will not give you what you want.

Either version is a good learning experience, so do not think that you did anything wrong. You just need to put some more thought into the code before you write it. Most of that will come from experience.

Hope that helps,

Andy
Handy,

I am not using code for int tries = 3; from my verification system. It is gonna be one try only.

I would like to use an array, just not with the pass code verification.

More like being able to call a file into an array, and being able to edit and save.
Hello codingN00b2017,

OK, if you ever want to use the function with "tries" in in you have a working version.

Putting the passwords into an array would be helpful, but reading the file each time you need it also works.

If I understand what you are saying, you want to put the information from "User Info.txt" into an array. For this I would use the struct that you have commented out and create an array of those structs to hold your information. This way you could use, add or edit the information. When loading the array you will need to keep a variable to know how much of the array you used. Also a variable like bool save{ false }; would let you know if the file should be saved before the program ends. This variable could be changed any time you add, edit or delete information.

As for the "adminAuthVer" function I hate to say it is about half of what it should be.

The function does not read the entire file to compare to "input".

Line 13 only reads the first line in the file and nothing else.

Line 14 the use of ".compare" is half of what it should be. It is written correctly, but not completely. This should help explain how to use the ".compare":
http://www.cplusplus.com/reference/string/string/compare/

Inside the if block you need a little more code than you have. When a match is found you need to leave the function because no further processing is needed.

The if in the "else if" is not needed. When the if condition is false it goes to the else part. There is no need to check that the two strings are not equal. This is implied because you are at the else part. Actually the else part does not need to be there. It should be outside the loop that reads the file, hint hint.

The function is a good start, but needs a little work. Refer to message
http://www.cplusplus.com/forum/beginner/226681/3/#msg1034267
for some tips to help improve this function. Especially the part about checking if the file is open.

Hope this helps,

Andy
Sorry about my absence. Class ended, got this turned in, as it currently functions. Recuperated, and will get back to to working on it.

Any and all continued, and new help is greatly appreciated.
Hello HandyAndy
Hope you had a good Christmas.

Just opened up the program, and already discovered an issue. The password comparer is accepting any entry as the correct password.
Hello codingN00b2017,

I believe your problem comes from if (input.compare(stringfromfileB)). What you have is allowing some invalid passwords to be considered as valid.

As I said earlier take a look at this:
http://www.cplusplus.com/reference/string/string/compare/

Or you could write the if statement as: if (input == stringfromfileB). They both do the same thing.

The ".compare" function has three possible return values. You need to understand how there values work before you can use the function properly.

Hope that helps,

Andy
Topic archived. No new replies allowed.
Pages: 123