採用數組儲存中間變數,修改後一次性重新寫入
#include "stdafx.h"
#include <cstdlib>
// Write a program that makes a virtual GET system.
// Write a program that saves and alters Student records saved to and from a file.
// A student should consist of a first name, last name,age, GPA (double), and units completed (int).
// Write a program that evaluates student information based on teacher input (of grades and units completed).
// Students must be defined in a class.
// Teacher input: What should be input is a numerical grade as well as units entered for a class.
// Professors must also be part of a class. Each professor, contains: a first name, last name, age, password.
// Note: Both Student and Professor Records Must be saved to files.
# include <cmath> // header file for math functions
# include <iostream> // header file for all basics ie class, cin, cout, void
using namespace std; // standard namespace helps compiler translate program
#include <fstream>
#include <string>
#include <iomanip>
class student// class declaration
{
public : // Encapsulating via data hiding; makes sure no external function can touch this data
int units; // any variable in this case scores
double gpa;
string firstname;
string lastname;
int age;
};
class Professor
{
private:
string firstname;
string lastname;
int age;
string password;
public:
Professor( string fn, string ln, int a, string pw )
{
this->firstname = fn;
this->lastname = ln;
this->age = a;
this->password = pw;
}
string getpassword()
{
return this->password;
}
};
int main()
{
Professor * pr = new Professor("Lee", "Joel" , 30, "password");
string pw = "";
//while ( pw.compare(pr->getpassword())!=0 )
{
// cout << "This is professor " << "Joel Lee, 30 years old, " << "Please enter your password: ";
// cin >> pw;
}
cout << "This is professor " << "Joel Lee, 30 years old,no password " << endl;
ifstream Indata; //for the file
Indata.clear();
string filename;
filename = "studentrecord.txt";
int i = 0;
int line = 0;
string temp_word;
Indata.open (filename.c_str());
// 1. get the line of the file
while(Indata.good())
{
if ( i != 0 && i % 4 == 0)
{
line ++;
}
Indata >> temp_word;
i++;
}
Indata.close();
//2. make the temporary memort to store
student * stu = new student[line];
ifstream Indata2; //for the file
Indata2.clear();
Indata2.open (filename.c_str());
i = 0;
int nowline = 0;
// 3. output and change the scores
while(Indata2.good() && nowline < line)
{
int u;
double g;
if (i == 0)
{
Indata2 >> stu[nowline].firstname;
cout << stu[nowline].firstname;
}
else if (i == 1)
{
Indata2 >> stu[nowline].lastname;
cout << stu[nowline].lastname;
}
else if (i == 2)
{
Indata2 >> stu[nowline].units;
cout << stu[nowline].units;
}
else if (i == 3)
{
Indata2 >> stu[nowline].gpa;
cout << stu[nowline].gpa;
}
i++;
if ( i == 4 )
{
cout << endl;
cout << "input the new unit: ";
cin >> u;
stu[nowline].units = u;
cout << "input the new gpa: ";
cin >> g;
stu[nowline].gpa = g;
nowline++;
i = 0;
}
cout << " " ;
}
Indata2.close();
// 4. write the new scores to file
ofstream f1;
// string fileout = "tabular.txt";
f1.open(filename.c_str());
if(!f1) return 1;
for (int i = 0; i < line; i++)
{
f1<< stu[i].firstname << " ";
f1<< stu[i].lastname << " ";
f1<< stu[i].units << " ";
f1<< setprecision(2) << stu[i].gpa << " " << endl;
}
f1.close();
// end
cout << endl;
system("pause");
return 0;
}
檔案格式:
paul lee 1 1
jack wa 1 1
joel joe 1 1