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