Multifunction Program Assisstance

Pages: 123
Hello Handy Andy

I remembered about the "void" removals. Just didnt get around to em yet.
I assume that because it is best to add a quit function to each main selection-admin and user, i would have to include void progEnd(); at the top with the rest, correct? or is this where the bool = {true/false}; come into play?

I am sorry if my slow work is tiring to deal with. I learn better by actually attempting/watching the material in action.

In a previous post you mentioned that, "The function definition at line 127 is a duplicate".

Does this mean that for the whole program, i only need 1 displayFile function. And this single one is accessed by each admin and user after primary choice selection?
Last edited on
Hello codingN00b2017,

I assume that because it is best to add a quit function to each main selection-admin and user, i would have to include void progEnd(); at the top with the rest, correct? or is this where the bool = {true/false}; come into play?

No a separate function to end the program is not necessary unless there is something special you want to do when the program ends then a function may be useful. Refer to my earlier post and look at the "adminControl" function for the use of a bool variable and how I used it. As you will see the bool is a way out of the do/while loop.

In a previous post you mentioned that, "The function definition at line 127 is a duplicate.
It means that you defined the same function twice. The compiler complained about the second definition.

Does this mean that for the whole program, i only need 1 displayFile function. And this single one is accessed by each admin and user after primary choice selection?
Yes. Unless you need a second function to display something different then it would need a different name to distinguish between the two.

Hope that helps,

Andy
So after tweaking the code based on advice, the only issue I am having is appendFile referenced in userAccess in the object file. I have checked spelling, and brace locations which are the usual culprits. Still nothing. The program will run just fine if i have the appendFile(s) commented but when i uncomment them it gives the same error.
Handy Andy
That actually helps a lot. And that applies to everything correct? If there are two main options, and they both have similar functions, then I only need to code for one option and both will be able to access it. This means that I would only need 1 updatePasscode as well.


Btw I do appreciate your assistance. You too keskiverto. Its getting easier to understand how this all works.
Hello codingN00b2017,

If there are two main options, and they both have similar functions, then I only need to code for one option and both will be able to access it. This means that I would only need 1 updatePasscode as well.
I would use the word same, similar implies that there is some difference. That is the nice part about OPP. A function can be call from anywhere in the program and different parts of the program can use the same function.

As for the second part yes

Now when you get the "userAccess" function to work like the "adminControl" function you can start to work on the other functions. At that point you may come to realize the you may need to define variables in main and pass them to other functions to make use of them. We can deal with that when the time comes.

Hope that helps,

Andy
So the issue with appendFile, after several attempts which included changing the name, wa that it couldn't find the function that was defined/declared at the start of this. I uncommented the function and it fixed the issue.

Programs runs.
Not everything is update(yet) in conjunction with advice, but the program works:adminControl and userAccess work alike.

Current issue with updated code: in the do loop of writeData (originally appendFile), the program will not allow me to enter information into name field, but instead starts me off in the clearance field.

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
208
209
210
211
ude <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <limits>

using namespace std;

char menu();
void adminControl();
void userAccess();
void useModify();
void displayData();
void updatePasscode();
void writeData();

const char FileName[] = "User Info.txt";

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

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

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

	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();
		case 'Q':
			cont = false;
			break;
		default:
			cout << "\n Unknown Choice! Please Try Again!" << endl;
			break;
		}
	while (cont);
}
	
	void useModify(void)
	{
		char choice = ' ';

		cout << "\n Select User: ";
		cin >> choice;

	}

	void displayData(void)
	{
		/*ifstream inMyStream(FileName);
		string lineBuffer;
		if (inMyStream.is_open())
		{
			string recBreaks = "";
			recBreaks.assign(20, '-');

			int fieldCount = 0;
			int recordCount = 1;

			fieldCount = 1;
			string fieldBuffer;
			getline(inMyStream, lineBuffer, '#');

			while (!inMyStream.eof())
			{
				getline(inMyStream, lineBuffer, '\n');
				string * theFields = split(lineBuffer, ',');
				cout << "Name........" << theFields[0] << endl;
				cout << "Clearance......" << theFields[1] << endl;
				cout << "Status........" << theFields[2] << endl;
				cout << "Location......." << theFields[3] << endl;
			}*/
	}

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

		bool cont{ true };

		cout << "What would you like to do?" << endl;
		cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode, (Q)uit" << endl;
		cin >> 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;
		}
	}
	
	void writeData(void)
	{
		char choice = ' ';
		string name = "";
		string clearance = "";
		string status = "";
		string location = "";
		ofstream outMyStream(FileName, ios::app);

		do
		{
			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();
	}

	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();*/
	}
I have finally gotten to a point where I can begin introducing creating, viewing, and editing files.
It will create the file, but as for displaying/editing i am stuck.

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
#include <iostream>
#include <fstream>
#include <string>

int main() //creates file
{
	ofstream out;
	out.open("C:\\Users\\ImTheUser\\Desktop\\Program variable files\\User Info.txt");

}
void displayData(void) //reads and displays file(or is supposed to)
	{
		string line;
		ifstream myfile("C:\\Users\\ImTheUser\\Desktop\\Program variable files\\User Info.txt");
		
		if (myfile.is_open())
		{
			while (myfile.good())
			{
				getline(myfile, line);
				cout << line << endl;
			}
			myfile.close();
		}

		else cout << "unable to Open File!";

	}


Last edited on
Hello codingN00b2017,

Before line 174 of the "writeData" function add this:
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); /

What you might have is a "cin >> something" that left a new line character in the input buffer. The "getline" at line 174 if getting the new line from the input buffer and thinking it is finished moves on to the next line of code. Clearing the input buffer solves this problem.

In the "displayData" function the while loop is better written as:

1
2
3
4
while (getline(myfile, line))
{
    cout << line << endl;
}


This is much like looping on a check for "eof". By the time the while condition figures you have a problem with the file stream you have printed the last read twice.

The other thing you most often see is:
1
2
3
4
5
if (!myfile.is_open()) // or just (!myfile)
{
    //  print error message.
    //  a pause.
    //  exit program. 


This way there is no need for the if/else statements.

In main you might want to add to out.open(...\\User Info.txt", std::ios::app);

A quick look over the bigger program it is looking better although I did notice in the "userAccess" function you are missing the do/while loop around lines 138 - 159.

I will need little more time to go through all of the program.

Hope that helps,

Andy
what exactly is the cin.ignore line used for? I am assuming that that is what helped to clear up the name field to let me input information.

right now my only issue, is it wont display the file. It'll let me write it, but not view it.

Hello codingN00b2017,

The "cin.ignore()" function is used to clear the input buffer because a "cin >> something" will leave something in the input buffer after a read. "cin >> something" will read up to the first white space or new line that it finds whichever comes first. Leaving anything that may be left in the input buffer. It also reads up the new line character, but leaves this in the input buffer.

In std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); the first parameter is just a very large number and the second parameter is the delimiter that it is looking for, usually the new line character "\n". You could just as easily write std::cin.ignore( 10000, '\n' ); This would ignore the first 10000 characters in the buffer or the new line whichever comes first or it will ignore less than 10000 if the new line character is found first.

Now that the day is fresh I will load up your latest version and see what I can find.

Hope that helps,

Andy
Last edited on
Hello codingN00b2017,

As a learning experience.

This is your function untouched:
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
void displayData(void)
	{
		ifstream inMyStream(FileName);
		string lineBuffer;
		if (inMyStream.is_open())
		{
			string recBreaks = "";
			recBreaks.assign(20, '-');

			int fieldCount = 0;
			int recordCount = 1;

			fieldCount = 1;
			string fieldBuffer;
			getline(inMyStream, lineBuffer, '#');

			while (!inMyStream.eof())
			{
				getline(inMyStream, lineBuffer, '\n');
				string * theFields = split(lineBuffer, ',');
				cout << "Name........" << theFields[0] << endl;
				cout << "Clearance......" << theFields[1] << endl;
				cout << "Status........" << theFields[2] << endl;
				cout << "Location......." << theFields[3] << endl;
			}
	}


Can you tell what is missing?

Line 26 the } is not indented enough to match the opening { of the if statement and there is no closing } for the function.

Most of the code looks like it will work, but some does not work at all.

If you are going to use the if statement this way you will need an else to express an error condition in case the if statement should fail.

To check if the file is open all you will need is:
1
2
3
4
5
6
if (!inMyStream)
{
	std::cout << "\n File did not open." << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
	exit(1);  // <--- No need  to continue with the program until you fix the problem.
}


Then you can follow this with the rest of our code.

The while loop condition testing for "eof" does not work the way you think it will. By the time the condition detects the "eof" state you will have processed the last read twice.

The "split" function you are trying to use gives me an error in my IDE. And it is not really needed in the first place. In the "cstring" header file there is "strtok()" function that will divide a string into tokens, but this is not necessary either.

The better way to read the file is:
1
2
3
4
5
6
7
8
9
10

while (getline(inMyStream, name, ','))
{
	getline(inMyStream, clearance, ',');
	getline(inMyStream, status, ',');
	getline(inMyStream, location);  // <--- Notice only one parmeter. The second is a default "\n".

	// Follow with the cout statements. Or you could use each "getline" in place of "theFields" to
	// do the same thing.
}


The second parameter of "getline" is the delimiter character. This way "getline will read up to and including the ",", but it will discard the "," before it is finished. This is the easiest way to read a "CSV" file.

Lastly you have defied these variables:
1
2
3
4
5
string recBreaks = "";
recBreaks.assign(20, '-');

int fieldCount = 0;
int recordCount = 1;


But never use them in the function.

I will work on the changes and see if I find anything else.

Hope that helps,

Andy
Hello Andy

When is usually best to put a cin.ignore line in the code?
Hello codingN00b2017,

Usually it is best to use after the last " cin >> something", but if that would prove difficult to find then before a "getline" that is being a problem works. If there i nothing to clear from the input buffer the "cin.ignore()" will just wait for you to press enter. This is a good indication that you did not need it.

I fould some minor problems like in the "adminControl" function it was missing the "do"part of the do/while loop and the "while (cont);" did nothing because the semicolon ended the while loop saying nothing is left to do.

I think this may be what you are looking for in this 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
void displayData(void)
{
	ifstream inMyStream(FileName);
	std::string name;
	std::string clearance;
	std::string status;
	std::string location;

	if (!inMyStream)
	{
		std::cout << "\n File did not open." << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		exit(1);
	}

	while (getline(inMyStream, name, ','))
	{
		getline(inMyStream, clearance, ',');
		getline(inMyStream, status, ',');
		getline(inMyStream, location);  // <--- Notice only one parameter. The second is a default "\n".

		cout << "Name..........." << name << endl;
		cout << "Clearance......" << clearance << endl;
		cout << "Status........." << status << endl;
		cout << "Location......." << location << endl;
		std::cout << std::endl;
	}

	inMyStream.close(); // <--- Good programming to close the stream even if the function does when it ends.
}


I have deleted anything that is unnecessary or was not used.

In the "writeData" function I added the "cin.ignore" before the first "getline". A "std::endl" at the end of line that writes to the stream, so each record is on one line. And I closed the file at the end of the function.

I changed the while condition because you were checking for two uppercase letters and one of them needs to be a lower case letter.

Now I can look at what else need to be addressed.

Hope that helps,

Andy
Handy Andy

I would like to apologize, i had made some changes to whole program last night but passed out before posting.

Here is the updated section. There is also a commented section that i was working on. Would it have worked? It did work, but wouldn't open the file
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
void displayData(void)
	{
		ifstream inMyStream(FileName);
		string name;
		string clearance;
		string status;
		string location;

		if (!inMyStream)
		{
			cout << "File Did Not Open!!" << endl;
			this_thread sleep_for::(chrono::second(3));
			exit(1);
		}

		while (getline(inMyStream, name, ','));
		{
			getline(inMyStream, clearance, ',');
			getline(inMyStream, status, ',');
			getline(inMyStream, location, ',');

			cout << "Name........" << name << endl;
			cout << "Clearance......" << clearance << endl;
			cout << "Status........" << status << endl;
			cout << "Location......." << location << endl;
		}
		inMyStream.close();
			
			
		/*string line;
		string lineBuffer;
		//ifstream myfile("C:\\Users\\imtheuser\\Desktop\\Program variable files\\User Info.txt");
		ifstream myfile("C:\\Users\\imtheuser\\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!";*/


	}

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
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
#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 displayData();
void updatePasscode();
void writeData();
string * split(string, char);

const char FileName[] = "C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txt";

int main() //First Menu Options
{
	ofstream out;
	out.open("C:\\Users\\xX-VENGEANCE-Xx\\Desktop\\Program variable files\\User Info.txt", std::ios::app);

	char choice{};
	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;
		break;
	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, (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};

	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();
		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 displayData(void)
	{
		ifstream inMyStream(FileName);
		string name;
		string clearance;
		string status;
		string location;

		if (!inMyStream)
		{
			/*cout << "File Did Not Open!!" << endl;
			this_thread sleep_for::(chrono::second(3));
			exit(1);*/
		}

		while (getline(inMyStream, name, ','));
		{
			getline(inMyStream, clearance, ',');
			getline(inMyStream, status, ',');
			getline(inMyStream, location, ',');

			cout << "Name........" << name << endl;
			cout << "Clearance......" << clearance << endl;
			cout << "Status........" << status << endl;
			cout << "Location......." << location << endl;
		}
		inMyStream.close();
			
			
		/*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 };

		cout << "What would you like to do?" << endl;
		cout << "(A)ppend, (D)isplay Files, (U)pdate Passcode, (Q)uit" << endl;
		cin >> 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;
		}
	}
	
	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');
		outMyStream.close();
	}

Within this update, there is commented makings for an array pulling data from a file.

The code, with commented sections works bu there are two issues:
1-Even tho Admin can view the file(FINALLY), it reads it as it is typed and formatted in the
file and not to the coded format
2-userAccess cannot view the file. Still working and tweaking
Last edited on
Hello Handy Andy

The display function was giving me issues so i commented it out and messed with some other code as shown below. This new code works to display the file information, but for some reason it only works with the Admin
1
2
3
4
5
6
7
8
9
10
11
12
13
void displayData(void)
	{
		string showData;
		ifstream openfile("C:\\Users\\ImTheUser\\Desktop\\Program variable files\\User Info.txt");
		if (openfile.is_open())
		{
			while(! openfile.eof())
			{
				getline(openfile, showData);
				cout << showData << endl;
			}
		}
         }
Last edited on
Line 7: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> (or getline) operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }

Hello codingN00b2017,

Just looking over the program first this is what I see:

Line 33 is not needed as you have no use for this function or function definition.

Line 35. The character array is not needed these days. A "std::string" works just fine. At least from C++11 on.

A suggestion of what you could do.

1
2
3
4
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";
//const std::string fileName{FILENAME}; 


By switching the comment on lines 3 and 4 you could use either. And if needed you could leave the "const" off of lines 3 and 4 should you need to change the "fileName" later i the program.

And in main you could use the same setup for the output file.

I hope I did not give you the wrong idea when we started. In main in the case sections the cout statements I used in the beginning for testing the program until the function calls were working. The main intent of using those "cout" statements is to let you know where the program flow is going when you first start. They were not meant to be permanent.

"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.

First compile gave me errors for function calls to functions not yet defined. Look at your prototypes. Each one should have function definition even if it is an empty function for now.

I noticed your latest code is missing some of what you have done earlier. Mot times it is better to build off of what you have than changing the code completely. Your latest code is still fixable, so you do not have to give up on it yet.

More as I test the program.

Hope that helps,

Andy
Hello codingN00b2017,

I forgot to mention I like the idea of the struct. Keep that around it might come in handy in the future.

Andy
Pages: 123