Question

When doing the File IO_struct program, did you notice that the design of the data structure...

When doing the File IO_struct program, did you notice that the design of the data structure can be improved? The problem is that some data are redundant in the struct/array structure. For example, the word “Amy” appears three times in the data structure. In real industry systems, no redundant data is allowed, since data in this kind of systems cannot be easily updated, deleted, added or saved. It is extremely error-prone. Thus in practice this type of designs should be avoided.

Now we can redesign the data structure to avoid this severe design problem. We can use either array or vector to do the redesign. After the data structure is loaded with the data, this is what it should look like.

12546

Amy

CS1

4

81

CS2

4

90

CS3

3

90

13455

Bill

CS1

4

76

CS2

4

85

CS3

3

75

14328

Jim

CS1

4

64

CS2

4

71

CS3

3

69

14388

Henry

CS3

3

80

15667

Peter

CS3

3

45

The 11 rows of data belong to 5 students. Thus, after the data are loaded, there should be exactly 5 student records, not 11 records, in the data structure. And each student record can hold multiple course records.     

Ideally the system should allow any number of students to be added and each student should be allowed to take any number of courses. (Do you think that vector is the better choice? Why?)

In this assignment we will redo the File IO/struct/array program by using vector, instead of array. You should feel that the coding is easier and the program is shorter than the array version.

=========================================================

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct Course
{
string course;
int credit;
int score;
};

struct Student
{
int id;
string name;
vector <Course> vec; // means you can have any courses.
};

int main()
{
vector <Student> Students;
  
fstream inputFile;
string fileName = "StudentRecords.txt";
  
inputFile.open(fileName.c_str(), ios::in);
if (inputFile.is_open())
{
while(!inputFile.eof())
{
while(!inputFile.eof())
{
  
}
  
}
inputFile.close();
}
else
cout << "File cannot be opened.";

return 0;
}

=====================================================

12546 Amy CS1 4 81
13455 Bill CS1 4 76
14328 Jim CS1 4 64
14388 Henry CS3 3 80
15667 Peter CS3 3 45
12546 Amy CS2 4 90
13455 Bill CS2 4 85
14328 Jim CS2 4 71

12546 Amy CS3 3 90
13455 Bill CS3 3 75
14328 Jim CS3 3 69

***StudentRecords.txt***

Homework Answers

Answer #1

#include <iostream>

#include <fstream>

#include <vector>

using namespace std;

struct Course

{

string course;

int credit;

int score;

};

struct Student

{

int id;

string name;

vector <Course> vec; // means you can have any courses.

};

int main() {

       vector <Student> Students;

       fstream inputFile;

       string fileName = "StudentRecords.txt";

       inputFile.open(fileName.c_str(), ios::in);

       if (inputFile.is_open())

       {

             while(!inputFile.eof())

             {

                    Student s;

                    Course c;

                    bool found = false;

                    // read the id and name of student

                    inputFile>>s.id>>s.name;

                    // read the course name, credit and score

                    inputFile>>c.course>>c.credit>>c.score;

                    // loop to check if the student with id already exists in vector

                    for(unsigned int i=0;i<Students.size();i++)

                    {

                           // if student exist in vector, add the course to its vector of courses

                           if(Students[i].id == s.id)

                           {

                                 Students[i].vec.push_back(c);

                                 found = true;

                                 break;

                           }

                    }

                    // if student doesn't exist in the vector, add the student

                    if(!found)

                    {

                           s.vec.push_back(c); // push the course to the students vector of courses

                           Students.push_back(s); // push the student in vector of students

                    }

             }

             // output the number of students in vector

             cout<<"Number of students : "<<Students.size()<<endl;

             // loop to output the student and courses details

             for(unsigned int i=0;i<Students.size();i++)

             {

                    cout<<"Id : "<<Students[i].id<<" Name : "<<Students[i].name<<endl;

                    for(unsigned int j=0;j<Students[i].vec.size();j++)

                           cout<<"\tCourse : "<<Students[i].vec[j].course<<" Credit : "<<Students[i].vec[j].credit<<" Score : "<<Students[i].vec[j].score<<endl;

             }

             inputFile.close(); //close the input file

       }else

             cout << "File cannot be opened.";

       return 0;

}

//end of program

Output:

Input file:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to...
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to generate and populate Parallel-Array Data Structure: const int NG = 4; //Number of Grades string names[] = {"Amy Adams", "Bob Barr", "Carla Carr", "Dan Dobbs", "Elena Evans" }; int exams[][NG]= { {98,87,93,88}, {78,86,82,91}, {66,71,85,94}, {72,63,77,69}, {91,83,76,60} };    --------------------------------------------------------------------------------- 1 A) Create and Populate a Parallel-Array Data Structure using the code described in "StudentDataStructure.txt". B) Define a Record Data Structure for student records. It...
c++ Create a program that saves data beyond the life time of the program. Utilize the...
c++ Create a program that saves data beyond the life time of the program. Utilize the following struct: struct PetRock { string name; int age; }; When the program runs, it first checks to see if a file contains data (PetRocks). If the file contains data then the data will be read from the file into an array of size 3 and will be printed (assume there will be no more than 3 objects saved in the data file). If...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
You should make up a transaction file with at least 3 records of students. Write a...
You should make up a transaction file with at least 3 records of students. Write a main program that creates a collection of students. Your transaction file contains a set of information about students. Your program should contain four classes: StudentProfile, Person, Student, and Course. StudentCollection has the following attributes:             vector<StudentProfile>           StCollection; StudentProfile class has the following attributes: Person                         PersonalInfo Student            StdInfo Person class has the following attributes: long                 SSN string               Fname string              ...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
Create a header file (lastname_employeerec.h) that defines an employee data structure (sEMPLOYEE) that can be linked...
Create a header file (lastname_employeerec.h) that defines an employee data structure (sEMPLOYEE) that can be linked onto a linked list. The data structure should have the following fields: a. First Name (firstName) b. Last Name (lastName) c. Employee ID (id) d. Start Year (startYear) e. Starting Salary (startSalary) f. Current Salary (currentSalary) g. next Create a library of functions that operate on this data structure. The source code for the functions should be in lastname_employeerec.c and the function prototypes should...
C++ PROGRAM 1. Open the file input called "info.txt" read it and store the data in...
C++ PROGRAM 1. Open the file input called "info.txt" read it and store the data in a vector. 2. Order the data by IP to perform the searches 3. Ask the user for the information search start and end IP's - Example of data on the info text: Jun 11 23:26:10 908.93.383.85:4940 Failed password for admin Aug 2 05:13:39 355.45.117.69:4551 Failed password for root Oct 24 02:32:14 852.37.168.72:6796 Illegal user 4. Display the records corresponding to those IPs 5. Store...
The following program implements a key-value data structure in which values are stored based on a...
The following program implements a key-value data structure in which values are stored based on a string keyword. Currently, the program allows new keys and values to be entered, but there is no function to retrieve the values given a search key. Complete the get_value function to retrieve a value given a search key. The function should print "INVALID KEY" if the key is not present. Do not remove any code OR add any code to main. You should only...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT