file reading problem

I am not able to display the name at the output screen, for example: i gv the name "ting", it shows ing only, whats wrong ?

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
void manage_staff() // manage staff menu, add or delete
{
	int ans;
	cout << "Manage Staff";
	cout << "\n1. Add staff";
	cout << "\n2. Staff list";
	cout << "\n3. Back to admin menu";
	cout << "\nSelect your choice (1-3):  ";
	cin >> ans;
	getchar();
	switch (ans)
	{
	case 1: add_staff(); break;
	case 2: staff_list(); break;
	case 3: admin_menu(); break;
	}
}

void add_staff()
{
	int ans;
	char ans2;
	staffRec staffAdd[s];
	int size;
	ofstream staff;

	cout << "Enter number of records";
	cin >> size;
	getchar();
	staff.open("d:\\staff.txt");

	for (int i = 0; i < size; i++){
		cout << "Enter name: ";
		cin >> staffAdd[i].staff_name;
		cout << "Enter Login ID: ";
		cin >> staffAdd[i].login_id;
		cout << "Enter Job: ";
		cin >> staffAdd[i].job;
		staff << staffAdd[i].staff_name << "\t" << staffAdd[i].login_id << "\t" << staffAdd[i].job << endl;
	}
	staff.close();
		

		cout << "\nChoose your next page. ";
		cout << "\n1. Manage Staff";
		cout << "\n2. Admin Menu";
		cout << "\n3. Log Out";
		cout << "\nChoose your answer (1-3)";
		cin >> ans;
	
	switch (ans)
	{
	case 1: manage_staff(); break;
	case 2: admin_menu(); break;
	case 3: main(); break;
	}
	
}


void staff_list()
{
	ifstream staff;
	int ans;
	staff.open("d:\\staff.txt");

	staffRec staffs[s];
	int x = 0;

	if (staff.fail())
	{
		cout << "File does not exist.\n";
	}
	else{
		cout << "File is exist\n";
		while (!staff.eof() && staff.get() != '\0'){
			getline(staff, staffs[x].staff_name, '\t');
			getline(staff, staffs[x].login_id, '\t');
			getline(staff, staffs[x].job, '\n');
			x++;
		}
	}

	for (int i = 0; i < x; i++)
	{
		cout << "Staff Record #" << i + 1 << endl;
		cout << "Name: " << staffs[i].staff_name << endl;
		cout << "Login ID: " << staffs[i].login_id << endl;
		cout << "Job: " << staffs[i].job << endl;
	}
	staff.close();
Why do you use getchar? What is it there for?
I'd think he's using the getchar to dispose of the \n that is left in the buffer after a std::cin call. Although, I think cin.get() is probably a better OO approach. But I'm not 100% sure.
Neither getchar or cin.get are correct.
cin >> ws;
This suffices for the purpose.
cin.ignore(numeric_limits<streamsize>::max());
Is more correct.
But my main problem is not getchar(), I have problem with the output of the file on the console screen.
For example, I cin "ting" at
cout << "Enter name: ";
cin >> staffAdd[i].staff_name;

but the output was like
Name : ing
Oh yes your file does contain "ing".
Check it out.
getchar is eating the first character off the words when you input them, so you are only reading and storing "ing".
1
2
3
4
5
6
while (!staff.eof() && staff.get() != '\0'){
			getline(staff, staffs[x].staff_name, '\t');
			getline(staff, staffs[x].login_id, '\t');
			getline(staff, staffs[x].job, '\n');
			x++;
		}


I might be reading this wrong (I'm tired, forgive me), but it looks as though you're discarding the first character of the staff file by calling staff.get() != '\0'). Staff.get() would get a character from the file, then you compare that character with the null character and then that character gets discarded.

I'm unsure what SGH was saying when he said,

Oh yes your file does contain "ing".
Check it out.
getchar is eating the first character off the words when you input them, so you are only reading and storing "ing".


As I don't see a getchar that is eating the first character of input anywhere. . .

EDIT: Try staff.peek() there. That looks at the character next in line but doesn't discard it.
Last edited on
Topic archived. No new replies allowed.