Question: #include #include #include int main() { using namespace std; // Student class to represent each student record class Student { public: string name; int id;
#include
using namespace std;
// Student class to represent each student record class Student { public: string name; int id; float gpa; char grade;
void read(ifstream& in) { getline(in, name); in >> id >> gpa >> grade; in.ignore(); // ignore the newline character }
void write(ofstream& out) { out << name << endl; out << id << " " << gpa << " " << grade << endl; } };
int main(); string in_filename, out_filename; cout << "Please enter the name of your data file: "; getline(cin, in_filename);
// try opening the input file ifstream in(in_filename); while (!in.is_open()) { cout << "I'm sorry, I could not open '" << in_filename << "'. "; cout << "Please enter another name: "; getline(cin, in_filename); in.open(in_filename); } cout << "File '" << in_filename << "' opened successfully! ";
cout << "Please enter the name of the destination file: "; getline(cin, out_filename);
// try opening the output file ofstream out(out_filename); while (!out.is_open()) { cout << "I'm sorry, I could not open '" << out_filename << "'. "; cout << "Please enter another name: "; getline(cin, out_filename); out.open(out_filename); } cout << "File '" << out_filename << "' opened successfully! ";
// copy the data from input to output file Student student; cout << "Copying data from '" << in_filename << "' to '" << out_filename << "'... "; in.peek(); while (!in.eof()) { student.read(in); student.write(out); in.peek(); } cout << "Done copying data! ";
// close the files in.close(); out.close();
} this is my code!!
Instructions
NOTE: THE ASSIGNMENT IS NOT TO SIMPLY COPY FILE A TO FILE B
Write a program that transfers the records of DATA in one file to a second file. The data is structured data.
This means that if you write a program to simply copy the contents of file A to file B, it will be incorrect.
The first file will contain an arbitrary (unknown) number of data groups.
A data group will consist of a person's:
Name (a string ** containing spaces **)
Student ID (a large integer)
GPA (a floating-point number)*
Letter grade (a single character).
You can design the order of the data within each group and the actual values to use. (It would be educational for you to experiment with different orders for the data values.)
Remember that your program cannot know how many data groups are in the file ahead of time!
You will read the files' names from the user.
Protect your program against any errors that may occur during the opening of the files.
Use classes to organize and improve your code. You must use classes to represent students.
Sample Data
Richard Grayson
123456
9.2
B
Lois Lane
123457 11.2 A
Bruce Wayne
123458
5.6 D
Clark Joseph Kent
110121 8.4
B
A run of your programmight look like:
Please enter the name of your data file: bob.dat
I'm sorry, I could not open 'bob.dat'.
Please enter another name: students
File 'students' opened successfully!
Please enter the name of the destination file: /invalidname
I'm sorry, I could not open '/invalidname'.
Please enter another name:students.bak
File 'students.bak' opened successfully!
Copying data from 'students' to 'students.bak'...
Done copying data!
The second file will be an exact data copy of the first. (Not a character-by-character copy, but a copy of the DATA from the file. The data types in the file are described as well as their meanings)
Your main should contain something like:
in.peek();
while (!in.eof())
{
student.read(in);
student.write(out);
in.peek();
}
Note that this says SOMETHING like. Not exact. You may change it.
TIPS:
Make sure to follow the coding standards document in blackboard. You will lose points if you don't.
The input and output should match the sample executable precisely. No embellishments. Do what the problem description says, and do only that. Otherwise there will be point penalties.
How can I add the names??
ASAP PLS!!!!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
