How to put in record numbers for file operations?

Hey guys, I'm having a problem with my program. The idea of the program is to have the user use a attendance record for each student. A problem I am having is I don't know how to write the code so that each Student the user puts in is a record. So the program can grab the record out of the file. I just need to write it so each Student Information is a separate record. Can anyone help me out?

Here's what I have so far.

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
#include <iostream>
#include <fstream>
#include <conio.h>
#include <iomanip>
using namespace std;

//Array Sizes
const int NAME_SIZE = 51, ATTEN_SIZE = 51;

//Functions


struct Info
{
	char name[NAME_SIZE];
	char attendance1[ATTEN_SIZE];
	char attendance2[ATTEN_SIZE];
	char attendance3[ATTEN_SIZE];
	char attendance4[ATTEN_SIZE];
	char attendance5[ATTEN_SIZE];
	char attendance6[ATTEN_SIZE];
	char attendance7[ATTEN_SIZE];
	char attendance8[ATTEN_SIZE];
	char attendance9[ATTEN_SIZE];
	char attendance10[ATTEN_SIZE];
};

int main()
{
	Info student;
	int choice = 0;
	char again;
	long byteNum;
	fstream dataFile;
	
    dataFile.open("Attendance.txt", ios::in);
	if (dataFile.fail())
	{
		//The File does not open so create it
		dataFile.open("Attendance.txt",ios::out);
	}
	else
	{
		dataFile.close();
		std::cout << "The file Attendance.txt already exists.\n";
	}

	do
	{
	
		std::cout << "Enter the Attendance for a student:\n";
		std::cout << "Name: ";
		cin.getline(student.name, NAME_SIZE);
		std::cout << "Enter Attendance for day 1: ";
		cin.getline(student.attendance1, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 2: ";
		cin.getline(student.attendance2, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 3: ";
		cin.getline(student.attendance3, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 4: ";
		cin.getline(student.attendance4, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 5: ";
		cin.getline(student.attendance5, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 6: ";
		cin.getline(student.attendance6, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 7: ";
		cin.getline(student.attendance7, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 8: ";
		cin.getline(student.attendance8, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 9: ";
		cin.getline(student.attendance9, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 10: ";
		cin.getline(student.attendance10, ATTEN_SIZE);

		dataFile.write(reinterpret_cast<char *>(&student),
			           sizeof (student));
	std::cout << "Do you want to enter another student? ";
	cin >> again;
	cin.ignore();
	} while (again == 'Y' || again =='y');
	// Close the File
	dataFile.close();

	std::cout << "This program allows you the user to select\n"
			  << "what you want to do for your attendance sheet" << endl;
	std::cout << "---------------------------------------------" << endl;
	std::cout << "1. Print attendane results.                  \n";
	std::cout << "2. Add a student to the sheet.               \n";
	std::cout << "3. Adjust a students attendance.             \n";
	std::cout << "---------------------------------------------\n";
	
	std::cout << "Choice: ";


	std::cin >> choice;

	if(choice==1)
	{
		
		std::cout << setw(15) << left << "Name"
				  << setw(5) << left << "1"
				  << setw(5) << left << "2"
				  << setw(5) << left << "3"
				  << setw(5) << left << "4"
				  << setw(5) << left << "5"
				  << setw(5) << left << "6"
				  << setw(5) << left << "7"
				  << setw(5) << left << "8"
				  << setw(5) << left << "9"
				  << setw(5) << left << "10" << endl;
		
		std::cout << setw(15) << left << student.name
			      << setw(5) << left << student.attendance1
				  << setw(5) << left << student.attendance2
				  << setw(5) << left << student.attendance3
				  << setw(5) << left << student.attendance4
				  << setw(5) << left << student.attendance5
				  << setw(5) << left << student.attendance6
				  << setw(5) << left << student.attendance7
				  << setw(5) << left << student.attendance8
				  << setw(5) << left << student.attendance9
				  << setw(5) << left << student.attendance10;
	};

				  
	if(choice == 2)
	{
		std::cout << "Enter the Attendance for a student:" << endl;
		std::cout << "Name: ";
		cin.getline(student.name, NAME_SIZE);
		std::cout << "Enter Attendance for day 1: ";
		cin.getline(student.attendance1, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 2: ";
		cin.getline(student.attendance2, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 3: ";
		cin.getline(student.attendance3, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 4: ";
		cin.getline(student.attendance4, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 5: ";
		cin.getline(student.attendance5, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 6: ";
		cin.getline(student.attendance6, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 7: ";
		cin.getline(student.attendance7, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 8: ";
		cin.getline(student.attendance8, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 9: ";
		cin.getline(student.attendance9, ATTEN_SIZE);
		std::cout << "Enter Attendance for day 10: ";
		cin.getline(student.attendance10, ATTEN_SIZE);

		dataFile.write(reinterpret_cast<char *>(&student),
			           sizeof (student));
	}

	if( choice == 3)
	{
		std::cout << "Which student record do you want to change?\n";
	}

	_getch();
}
Topic archived. No new replies allowed.