数组跟结构结合基础题
数组和结构结合基础题
--------------------------------------------------------
Your program will first define the following C++ structure:
struct StudentRec
{
string Name; // in the format of "Last name, First Name"
string ID;
double GPA;
};
You will then define an array of StudentRec’s. The array size is 10.
You will then open then file "studentrecord.txt", read the file (using the read_a_record ()
function mentioned below) to populate the array you’ve defined. Each line in the file contains one
student record: First Name, Last Name, ID, and GPA. Note that you must check the array
boundary to prevent array overflow if the file contains more records than the array can hold, and
discard the extra records. You will lose 10% of your points if you do not check this. To test this,
add a few lines of records in the text file and run your program.
Note: You need to create and call a function named bool read_a_record(StudentRec&)
to read one record from the file. It returns true if successfully read a record, and return false
if fails (i.e., end of file).
After reading the records into the array, create a function
print_a_record(StudentRec)to print a student record in one line. You then write a loop
to print out the entire array. The output should be formatted like the following:
No. Name ID GPA
--------------------------------------------------------
1 Adams, Jone M123456 3.65
2 Smith, Mike M452355 2.65
3 Ann, Mary M456712 4.00
------解决方案--------------------
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
struct StudentRec
{
string "Adams,Jone";
string M123456;
double 3.65;
};
int main()
{
const int size=10
int values[size];
int count;
bool read_a_record(StudentRec&)
ifstream inputFile;
string filename;
cout << "Enter the filename:";
getline(cin, filename);
inputFile.open(filename.c_str());
if(!inputFile){
cout << "Error opening the file.\n";
For (int i=0; i<number_of_students; i++)
cout<<our_students[i].GPA<<endl;
}
--------------------------------------------------------
Your program will first define the following C++ structure:
struct StudentRec
{
string Name; // in the format of "Last name, First Name"
string ID;
double GPA;
};
You will then define an array of StudentRec’s. The array size is 10.
You will then open then file "studentrecord.txt", read the file (using the read_a_record ()
function mentioned below) to populate the array you’ve defined. Each line in the file contains one
student record: First Name, Last Name, ID, and GPA. Note that you must check the array
boundary to prevent array overflow if the file contains more records than the array can hold, and
discard the extra records. You will lose 10% of your points if you do not check this. To test this,
add a few lines of records in the text file and run your program.
Note: You need to create and call a function named bool read_a_record(StudentRec&)
to read one record from the file. It returns true if successfully read a record, and return false
if fails (i.e., end of file).
After reading the records into the array, create a function
print_a_record(StudentRec)to print a student record in one line. You then write a loop
to print out the entire array. The output should be formatted like the following:
No. Name ID GPA
--------------------------------------------------------
1 Adams, Jone M123456 3.65
2 Smith, Mike M452355 2.65
3 Ann, Mary M456712 4.00
------解决方案--------------------
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct StudentRec
{
string Name; // in the format of "Last name, First Name"
string ID;
double GPA;
};
StudentRec student[10]; //defination of student
static int size = 0;
bool read_a_record(StudentRec& s){ //read a record
fstream in("StudentRecords.txt");
if (size == 10) //check boundry
return false;
if (in){
string drop;
for (int i = 0; i < size; i++) //jump from former records
getline(in, drop);
string name;
in >> name;
if (name.empty()) //check the end of file
return false;
in >> s.Name;
s.Name += ',' + name; //union name
in >> s.ID;
in >> s.GPA;
++size; //control size
return true;
}
else{
cout << "File open fail!" << endl;
exit(1);
}
return false;
}
void print_a_record(StudentRec& s){ //print a record
cout << s.Name << '\t' << s.ID << '\t' << s.GPA << endl;
}
void print_all_records(){ //print all records
cout << "No. Name ID GPA \n";