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
|
// main.cpp
// Week_7
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#include "person.h"
#include "myfile.h"
int addStudent();
int main(int argc, char *argv[])
{
char *file1, *file2, *file3;
string outStr, buf;
int length = 0, where = 0, addStud = 0;
ifstream input;
ofstream output, outfile;
file1 = new char[_MAX_FNAME];
file2 = new char[_MAX_FNAME];
file3 = new char[_MAX_FNAME];
filePrompt(file1, file2, file3);
input.open(file1);
if(!input.is_open()){
cerr << "Error opening input file " << file1 << " . . .\n";
system("pause");
exit(1);
}
output.open(file2);
if(!output.is_open()){
cerr << "Error opening input file " << file2 << " . . .\n";
system("pause");
exit(1);
}
outfile.open(file3);
if(!outfile.is_open()){
cerr << "Error opening input file " << file3 << " . . .\n";
system("pause");
exit(1);
}
where = input.tellg();
cout << "\nThe file pointer is "<< where << " here\n";
while(getline(input, buf)){
output << buf << "\n";
}
output.close();
input.clear();
input.seekg(0,ios::beg);
where = input.tellg();
cout << "\nThe file pointer is "<< where << " here\n";
while(getline(input,buf)){
outStr += buf + "\n"; length++;
}
cout << outStr;
where = input.tellg();
cout << "\nThe file pointer is "<< where << " here\n";
cout << "\nThe file length is " << length << endl;
input.clear();
input.seekg(0,ios::beg);
where = input.tellg();
cout << "\nThe file pointer is "<< where << " here\n";
system("pause");
Person *SRD = new Person[length];
for(int i = 0; i < length; i++){
getline(input,SRD[i].lastname,',');
getline(input,SRD[i].firstname,',');
input >> SRD[i].gender; input.ignore(1);
getline(input,SRD[i].STUD.major,',');
input >> SRD[i].STUD.gpa; input.ignore(1);
}
for(int i = 0; i < length; i++){
cout << SRD[i].firstname << " " << SRD[i].lastname << endl;
if(SRD[i].gender)
cout << "Gender: Female\n";
else
cout << "Gender: Male\n";
cout << "Major: " << SRD[i].STUD.major << endl
<< "GPA: " << SRD[i].STUD.gpa << endl << endl;
}
input.close();
system("pause");
char value = ' ';
addStud = addStudent();
Person *temp = new Person[addStud];
cin.ignore(1);
for(int i = 0; i < addStud; i++){
system("cls");
cout << "Enter Student's Last Name: ";
getline(cin,temp[i].lastname,'\n');
cout << "Enter Student's First Name: ";
getline(cin,temp[i].firstname,'\n');
cout << "Enter Student's Gender M or F: ";
cin >> value; cin.ignore(1);
value = toupper(value);
temp[i].gender = (value == 'F')? true: false;
cout << "Enter Student's Major: ";
getline(cin,temp[i].STUD.major,'\n');
cout << "Enter Student's GPA: ";
cin >> temp[i].STUD.gpa; cin.ignore(1);
}
Person *temp2 = new Person[length+addStud];
for(int j = 0; j < length; j++){
temp2[j].lastname = SRD[j].lastname;
temp2[j].firstname = SRD[j].firstname;
temp2[j].gender = SRD[j].gender;
temp2[j].STUD.major = SRD[j].STUD.major;
temp2[j].STUD.gpa = SRD[j].STUD.gpa;
}
for(int j = 0, k = length; k < length+addStud; k++, j++){
temp2[k].lastname = temp[j].lastname;
temp2[k].firstname = temp[j].firstname;
temp2[k].gender = temp[j].gender;
temp2[k].STUD.major = temp[j].STUD.major;
temp2[k].STUD.gpa = temp[j].STUD.gpa;
}
system("cls");
for(int i = 0; i < length+addStud; i++){
outfile << temp2[i].lastname << ','
<< temp2[i].firstname << ','
<< temp2[i].gender << ','
<< temp2[i].STUD.major << ','
<< temp2[i].STUD.gpa << endl;
if((i+1)< 10)
cout << " " << i+1 << " " << temp2[i].lastname << ','
<< temp2[i].firstname << ','
<< temp2[i].gender << ','
<< temp2[i].STUD.major << ','
<< temp2[i].STUD.gpa << endl;
else
cout << i+1 << " " << temp2[i].lastname << ','
<< temp2[i].firstname << ','
<< temp2[i].gender << ','
<< temp2[i].STUD.major << ','
<< temp2[i].STUD.gpa << endl;
}
outfile.close();
system("PAUSE");
return EXIT_SUCCESS;
} // end of main
// function definitions
int addStudent(){
system("cls");
int value = 0;
cout << "How many students would you like to add? ";
cin >> value;
return value;
}
|