I HAVE PROBLEM WITH THIS

I HAVE BEEN THINKING WHAT IS WRONG WITH THIS PROBLEM FOR THE PAST TWO DAYS..
*
*/
//Header include statement
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>

//Namespace statement
using namespace std;



//Declaring structure
struct student {
string name, course, id;
char gender;
int age;
};//Structure is unnamed yet

//Function prototype
void check_name();
void check_course();
void check_gender();
void check_id();

//Start main function
int main (void) {
int student_sent;
cout << "Welcome to the student record program!" << endl;
cout << "How many students' data do you want to key in?" << endl;
cin >> student_sent;
if (student_sent>0&&student_sent<=50) {
student *p= new student [50];
//Input data for each student
//Initial length checking included
for (int i=0; i<student_sent; i++) {
cout << "Please enter information for student ";
cout << i+1 << endl;

//First input for name
fflush(stdin);
cout << "Enter name : ";
getline (cin, (p+i)->name, '\n');
//Input again if name is too long
while ((p+i)->name.length()>30) {
cout << "Name is too long, please enter a name";
cout << "that is not exceeding 30 letters,";
cout << "including space" << endl;
fflush(stdin);
cout << "Enter name : ";
getline (cin, (p+i)->name, '\n');
}

//First input for course
fflush(stdin);
cout << "Enter course : ";
getline (cin, (p+i)->course, '\n');
//Input again if course is not 2 char-digit combination
while ((p+i)->course.length()!=2) {
cout << "Please enter a two letter-digit conbination";
cout << "as a course" << endl;
fflush(stdin);
cout << "Enter course : ";
getline (cin, (p+i)->course, '\n');
}

//First input for gender
cout << "Enter gender : ";
cin >> (p+i)->gender;
switchgen:
switch ((p+i)->gender) {
case 'm':
case 'f':
(p+i)->gender=toupper((p+i)->gender);
break;
case 'M':
case 'F':
break;
default:
cout << "Invalid gender, please enter a valid one." << endl;
cout << "Enter gender : ";
cin >> (p+i)->gender;
goto switchgen;
}

//First input for id
fflush(stdin);
cout << "Enter id : ";
getline (cin, (p+i)->id, '\n');
//Input again if ID length is not equal to 10
while ((p+i)->id.length()!=10) {
cout << "ID length is invalid.";
cout << "Please make sure ID length is exactly 10";
cout << endl;
cout << "Enter id : ";
getline (cin, (p+i)->id, '\n');
}
//First input for age
cout << "Enter age : ";
cin >> (p+i)->age;
}

fstream file_1;
file_1.open("StudentInput.txt", ios::out | ios::trunc);

//Output student data
for (int j=0; j<student_sent; j++) {
cout << "Information for Student " << j+1 << endl;
cout << (p+j)->name << endl;
cout << (p+j)->course << endl;
cout << (p+j)->gender << endl;
cout << (p+j)->id << endl;
cout << (p+j)->age << endl;
cout << endl;
if (file_1) {
file_1 << (p+j)->name << endl;
file_1 << (p+j)->course << endl;
file_1 << (p+j)->gender << endl;
file_1 << (p+j)->id << endl;
file_1 << (p+j)->age << endl;
}
else if (file_1.bad()) {
cout << "Unable to write data to file " << endl;
}
}
file_1.close();
delete [] p;
}

//Call function defined below
check_name();
check_course();
check_gender();
check_id();


return (0);
}
//Capitalize first letter of the name

void check_name() {
string namer = "michael james mary";
char a[30];
//Convert string to array
for (int k=0 ; k<(char)namer.length(); k++) {
a[k]=namer.at(k);
}
char *tokenPtr;
tokenPtr = strtok(a," ");
string new_name;
//Capitalize array
while( tokenPtr != NULL ) {
char q= toupper(*tokenPtr);
tokenPtr[0] = q;
new_name.append(tokenPtr);
tokenPtr = strtok(NULL, " ");
}
string new_name2;
//Convert array back to string
for (int k=0; k<(char)namer.length(); k++) {
new_name2.append(1, a[k]);
}
cout << new_name2 << endl;
}

//Capitalize course, if number, no need capitalize

void check_course() {
string course2= "ec";
char b[3];
//Convert string to array
for (int l=0 ; l<(char)course2.length(); l++) {
b[l]=course2.at(l);
//Capitalize array
if (isalpha(b[l])&&islower(b[l])) {
b[l]=toupper(b[l]);
}
}
string new_course2;
//Convert array back to string
for (int m=0; m<(char)course2.length(); m++) {
new_course2.append(1, b[m]);
}
cout << new_course2 << endl;
}

//Capitalize letter for sex, not yet determine if not male or female
void check_gender() {
//Using switch to capitalize gender because gender is char type
char sex= 'F';
switch (sex) {
case 'm':
case 'f':
sex=toupper(sex);
cout << sex << endl;
break;
case 'M':
case 'F':
cout << sex << endl;
break;
default:
cout << "Invalid gender" << endl;
}
}

//Capitalize the letter in the id, not yet determine if id is invalid or not

void check_id() {
string idd= "09ueb21453";
char c[11];
//Convert string to array
for (int n=0 ; n<(char)idd.length(); n++) {
c[n]=idd.at(n);
//Capitalize array
if (isalpha(c[n])&&islower(c[n])) {
c[n]=toupper(c[n]);
}
}
string new_idd;
//Convert array back to string
for (int o=0; o<(char)idd.length(); o++) {
new_idd.append(1, c[o]);
}
cout << new_idd << endl;
}
What's your problem exactly?
Cause you can run your program
the problem is this,, we need to go class by class but the program cannot append


This question needs you to use a structure to store the information about a student detail. Your task in this question is to read information about students from the keyboard (maximum 50 students) and store in the input file and write out the output to an output file. The program will ensure that ONLY data written out satisfies all the requirements. The following are the attributes of a student:

• Name: a string with only alphabetic characters no longer than 30 characters. The first character from each word must be changed to uppercase letters.
• Course: a string with 2 characters only and must be changed to uppercase letters.
• Sex: a character M (male) and F (female) only and must be changed to uppercase letters.
• ID Number: a string that consist of 10 characters, with digits in positions 1, 2, 6, 7, 8, 9, and 10; and the alphabets in positions 3, 4 and 5. The alphabets must be changed to uppercase letters.
• Age: an integer. You may assume this field will always be an integer, but the age must be a positive integer and not more than 55.

You program must prompt the user for an input file name and an output file name. When reading from the input file, check each field for validity. If the data on a line is valid, you should add the student to the array. Make sure that you report any errors of formatting, and ignore the student record in question. Also, no two students can have the same ID number. So, before a student is added, make sure that there is no student with the same ID number in the collection already. Such an error should also be reported. Your program should report the total number of records read and the total number of invalid records found. The valid data must be sorted in ascending order and written the result to the output file. (Note: if a record has multiple errors, only one has to be reported.)

A sample of input shown below:
Sofea amani Ahmad 3e f 09ueb11111 23
Tan kok Lim EC M 09ueb11112 22
Priya A/P Muthusamy 3E f 09ueb11112 22
Kathy Anderson 3E f 09ueb11113 20
Karim Maarof ec m 09ueb11114 56
Chan Lai Mei ec f 0ueb11234 19
Ang Choon Kit 3e m 9ueb11235 20
Fatimah Abdullah ec f 09ueb11115 21
Chan Ah Kow ec m 09ueb11114 22
A sample of output to the screen:
STUDENT RECORD PROGRAM
Enter input file name: studentIn.dat
Enter output file name: studentOut.dat,
Ignoring Record Number 3 – duplicate ID number
Ignoring Record Number 5 – bad age
Ignoring Record Number 6 – bad ID number
Ignoring Record Number 7 – bad ID number
Ignoring Record Number 9 – duplicate ID number
Processed 9 student records
5 invalid records were found
Written output to student output file

A sample of the output file for the above data is shown below:
Fatimah Abdullah EC F 09UEB11115 21
Kathy Anderson 3E F 09UEB11113 20
Sofea Amani Ahmad 3E F 09UEB11111 23
Tan Kok Lim EC M 09UEB11112 22

Well, I am not able to complete the requirements of the question. I can input the data correctly but when i try to go through the check_name, check_course etc. function with my own data, it goes through a runtime bug.

anyone here?
closed account (z05DSL3A)
First edit your first post, at the start of your code type [code] and at the end type[/code]. Please do not post the entire code again.

Second, you will need to give more information about the error you are having, "it goes through a runtime bug" is not very help full.


The following may be helpful in asking you questions:
http://www.cplusplus.com/forum/beginner/1/
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
/Header include statement
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>

//Namespace statement
using namespace std;



//Declaring structure
struct student {
string name, course, id;
char gender;
int age;
};//Structure is unnamed yet

//Function prototype
void check_name();
void check_course();
void check_gender();
void check_id();

//Start main function
int main (void) {
int student_sent;
cout << "Welcome to the student record program!" << endl;
cout << "How many students' data do you want to key in?" << endl;
cin >> student_sent;
if (student_sent>0&&student_sent<=50) {
student *p= new student [50];
//Input data for each student
//Initial length checking included
for (int i=0; i<student_sent; i++) {
cout << "Please enter information for student ";
cout << i+1 << endl;

//First input for name
fflush(stdin);
cout << "Enter name : ";
getline (cin, (p+i)->name, '\n');
//Input again if name is too long
while ((p+i)->name.length()>30) {
cout << "Name is too long, please enter a name";
cout << "that is not exceeding 30 letters,";
cout << "including space" << endl;
fflush(stdin);
cout << "Enter name : ";
getline (cin, (p+i)->name, '\n');
}

//First input for course
fflush(stdin);
cout << "Enter course : ";
getline (cin, (p+i)->course, '\n');
//Input again if course is not 2 char-digit combination
while ((p+i)->course.length()!=2) {
cout << "Please enter a two letter-digit conbination";
cout << "as a course" << endl;
fflush(stdin);
cout << "Enter course : ";
getline (cin, (p+i)->course, '\n');
}

//First input for gender
cout << "Enter gender : ";
cin >> (p+i)->gender;
switchgen:
switch ((p+i)->gender) {
case 'm':
case 'f':
(p+i)->gender=toupper((p+i)->gender);
break;
case 'M':
case 'F':
break;
default:
cout << "Invalid gender, please enter a valid one." << endl;
cout << "Enter gender : ";
cin >> (p+i)->gender;
goto switchgen;
}

//First input for id
fflush(stdin);
cout << "Enter id : ";
getline (cin, (p+i)->id, '\n');
//Input again if ID length is not equal to 10
while ((p+i)->id.length()!=10) {
cout << "ID length is invalid.";
cout << "Please make sure ID length is exactly 10";
cout << endl;
cout << "Enter id : ";
getline (cin, (p+i)->id, '\n');
}
//First input for age
cout << "Enter age : ";
cin >> (p+i)->age;
}

fstream file_1;
file_1.open("StudentInput.txt", ios::out | ios::trunc);

//Output student data
for (int j=0; j<student_sent; j++) {
cout << "Information for Student " << j+1 << endl;
cout << (p+j)->name << endl;
cout << (p+j)->course << endl;
cout << (p+j)->gender << endl;
cout << (p+j)->id << endl;
cout << (p+j)->age << endl;
cout << endl;
if (file_1) {
file_1 << (p+j)->name << endl;
file_1 << (p+j)->course << endl;
file_1 << (p+j)->gender << endl;
file_1 << (p+j)->id << endl;
file_1 << (p+j)->age << endl;
}
else if (file_1.bad()) {
cout << "Unable to write data to file " << endl;
}
}
file_1.close();
delete [] p;
}

//Call function defined below
check_name();
check_course();
check_gender();
check_id();


return (0);
}
//Capitalize first letter of the name

void check_name() {
string namer = "michael james mary";
char a[30];
//Convert string to array
for (int k=0 ; k<(char)namer.length(); k++) {
a[k]=namer.at(k);
}
char *tokenPtr;
tokenPtr = strtok(a," ");
string new_name;
//Capitalize array
while( tokenPtr != NULL ) {
char q= toupper(*tokenPtr);
tokenPtr[0] = q;
new_name.append(tokenPtr);
tokenPtr = strtok(NULL, " ");
}
string new_name2;
//Convert array back to string
for (int k=0; k<(char)namer.length(); k++) {
new_name2.append(1, a[k]);
}
cout << new_name2 << endl;
}

//Capitalize course, if number, no need capitalize

void check_course() {
string course2= "ec";
char b[3];
//Convert string to array
for (int l=0 ; l<(char)course2.length(); l++) {
b[l]=course2.at(l);
//Capitalize array
if (isalpha(b[l])&&islower(b[l])) {
b[l]=toupper(b[l]);
}
}
string new_course2;
//Convert array back to string
for (int m=0; m<(char)course2.length(); m++) {
new_course2.append(1, b[m]);
}
cout << new_course2 << endl;
}

//Capitalize letter for sex, not yet determine if not male or female
void check_gender() {
//Using switch to capitalize gender because gender is char type
char sex= 'F';
switch (sex) {
case 'm':
case 'f':
sex=toupper(sex);
cout << sex << endl;
break;
case 'M':
case 'F':
cout << sex << endl;
break;
default:
cout << "Invalid gender" << endl;
}
}

//Capitalize the letter in the id, not yet determine if id is invalid or not

void check_id() {
string idd= "09ueb21453";
char c[11];
//Convert string to array
for (int n=0 ; n<(char)idd.length(); n++) {
c[n]=idd.at(n);
//Capitalize array
if (isalpha(c[n])&&islower(c[n])) {
c[n]=toupper(c[n]);
}
}
string new_idd;
//Convert array back to string
for (int o=0; o<(char)idd.length(); o++) {
new_idd.append(1, c[o]);
}
cout << new_idd << endl;
}



The question is up there .....
closed account (z05DSL3A)
Okay, you still have not said what your error is, but I think you need to re-read your requirements because you program is far from doing what is asked for.
Topic archived. No new replies allowed.