How to edit and Delete records in a CSV file

Hi all, I am working on a program through Xcode, it is supposed to read a file and perform varuous functions. displayMenu, doDisplay, doView, doAdd, doEdit, and doDelete. I have gotten it all to work except the do edit and do delete functions, and I can't find anything online that helps with my particular situation. The user is supposed to pick the course they want to edit, and be abel to edit the term, year, start date, end date, etc. I don't think I am supposed to use another file as well, but if that is the only solution you guys can think of than I will do that, because at this point I'll take any help I can get. Thank you in advance.

Here is my code attached, I know some of you will tell me to fix the getters and setters but this is for class so I have to keep them to follow instructions. I also cannot change the functions from the model ex.void doAdd(vector<courseType>& list).

https://pastebin.com/HkEE9HBD

Here are instructions if you are curious:
Course Menu
=====================
d - Display list of courses
v - View course details
a - Add a course
e - Edit a course
d - Delete a course
x - Exit program
Requirements
• Given a comma-delimited text file named courses.csv Download courses.csv(click to download) that stores the starting data for the program.
A course is an organized presentation about a particular subject that includes Term, Year, Start Date, End Date, Course Name, Class ID, Section, Meeting Days, Location (online, hybrid, or on-campus), Meeting Information, Instructor, Units.
• For the view, edit, and delete commands, display an error message if the user selects an invalid course.
• Define a data structure to store the data for each course.
• When you start the program, it should read the contacts from the comma-delimited text file and store them in a vector of courses.
• When reading data from the text file, you can read the line and separate the field by an all text up to the next comma (ex. getline() function).
• When you add, edit or delete a contact, the change should be saved to the text file immediately. That way, no changes are lost, even if the program crashes later.
• For demonstration, you need to be able to view/add/edit/delete at least 3 courses listed

Last edited on
assuming this is homework:
a csv file is just a text file with commas.
To delete and modify text files, unfortunately, the usual approach requires that you read the whole file into a data structure and write that back out when done to save over the original (or to a copy if you prefer -- don't lose your original so you can revert and test with it!).
You can't just jump to the nth record and change text there, in other words, you just wipe and replace with the corrected data.

more professionally (if it were a big project or not school work), there are libraries for dealing with CSV (its very common format) but I suspect you don't want that approach.
Last edited on
So I have tried that before I think but I ran into some trouble, I read the og file into a new one, and had the user write new input into the og and obvisouly it deleted the rest of the records, how would I instert the old record back in without deleting the new input? And is this what you are reffering, like am I going about this the right way? Also would there be a way to insert the old data back in the same postions they were in? Or would ther be a way to edit the member like edit list[].getTerm()?
Last edited on
stop thinking in terms of your code for a min.
Its pretty simple, conceptually: you have some struct, that represents the columns in your data. You have some container, lets say a vector, of those structs that represents the rows.
You open the file and read into that.
the user changes some stuff. Does not matter if they add, remove, or change, whatever. You manipulate the vector to do the same.
you write the vector back out.
done.

write new input into the og and obvisouly it deleted the rest of the records

It should not. If you add a row, the other rows are unchanged. If you modify a row, the other rows are unchanged. All the old data is there.

if you have
1,2,3
4,5,6

and want to add 7,8,9..
you read a row: 1,2,3 is in row 0
you read a row: 4,5,6 is in row 1
you need to add a row, you add row 2
you populate it, row 2 has 7,8,9
you write the container out: it has
1,2,3 //unchanged still row 0
4,5,6 //unchanged, still row 1
7,8,9 //added, ok

everything keeps its position.
The only issue is if you want to insert in the middle ... do you need to do that? It just says add, but not where, so I assumed you could just tack it onto the end, which is easy.
I am so sorry, I messed up when typing I meant I was struggling with the do Edit and do Delete, I have the do add already done.
for deletes, add a field that you neither read nor write to the file, a boolean that is initially false, and set to true when you delete a record. When you go to save the file out, skip records with this value set. Also when you look for / access records that are available, ignore records with the value set.

for edits just change the value as indicated by the user.
array[selected_record].selected_field = value_from_user;

and when you write it out it will be updated, or if you access it again it will be updated.
I tried doing the following code below just to test if it would change the term but it still outputs the previous term which is spring. Is this what you mean or am I still doing it wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void doEdit(vector<courseType>& list){
    int ch;
    
    for(int i = 0; i < list.size(); i++){
        cout << i+1 << ". " << list[i].getName() << "" << endl << endl;
    }
    
        
    cout << "Please choose a course you want to edit: ";
    cin >> ch;
    cout << endl;
    if(ch<list.size()+1)
    {
        list[ch].getTerm() = "winter";
    }
    cout << list[ch].getTerm();
        
}


The enitre output is :

Course Menu
=====================
l - Display list of courses
v - View course details
a - Add a course
e - Edit a course
d - Delete a course
x - Exit program

Enter choice: e

1. Discrete Structures

2. Programming W/ Data Structures

3. Programming W/ Data Structures

4. JavaScript for Web Development

5. Beginner Java

Please choose a course you want to edit: 1

Spring
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
What is the code for getTerm()? Does it return a reference? If not, then L14 in the above code won't do what you think it does.
I asked my professor for help and he just gave me the following code and said I had to repeat that process for the rest of the variables, but this code doesn't run it says

Showing Recent Issues
Undefined symbol: doEdit(std::__1::vector<courseType, std::__1::allocator<courseType> >&)


I believe this means that I didn't write or initialize a variable right? But I am not sure what to initlize, declare, etc. or where to do it, sorry for seeming so stupid but this course is way different than my previous one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void doEdit(vector<courseType>& list, string courseName){
    bool found = false;
    for(int i = 0; i < list.size(); i++){
        if (list[i].getName() == courseName) {
            cout << "Enter term: ";
            string term;
            cin >> term;
            list[i].setTerm(term);
            // repeat above process for other fields.
            found = true;
        }
        cout << list[i].getName() << "" << endl << endl;
    }
    
}
Last edited on
Here is my code:
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
////myApp.cpp
 
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include "course.h"
#include "utils.h"
 
using namespace std;
 
char menu();
 
int main() {
 
    vector<courseType> list;
    readData(list);
 
    char ch = 'x';
    do {
        cout << endl;
        
        ch = menu();
        switch (ch) {
        case 'l':
            // function handler
            doDisplay(list);
            break;
        case 'v':
            doView(list);
            break;
        case 'a':
            doAdd(list);
            break;
        case 'e':
            doEdit(list);
            break;
        case 'd':
            doDelete(list);
            break;
        default:
            cout << "Please enter valid choice" << endl;
        }
    } while (ch != 'x');
 
    cout << "Exit the program" << endl;
 
    return 0; // exit the program successfully
}
 
/**
* Display application menu
*/
char menu() {
 
    cout << "Course Menu" << endl;
    cout << "=====================" << endl;
    cout << "l - Display list of courses" << endl;
    cout << "v - View course details" << endl;
    cout << "a - Add a course" << endl;
    cout << "e - Edit a course" << endl;
    cout << "d - Delete a course" << endl;
    cout << "x - Exit program" << endl << endl;
    cout << "Enter choice: ";
    char ch;
    cin >> ch;
    cout << endl;
    return ch;
}
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
//courseImp.cpp
 
#include <string>
#include "course.h"
#include <iostream>
 
using namespace std;
 
courseType::courseType() {
    term = "";
    year = 0.0;
    startDate = "";
    endDate = "";
    courseName = "";
    section = "";
    classID = 0.0;
    meetingDays = "";
    location = "";
    meetingInfo = "";
    instructor = "";
    units = 0.0;
}
courseType::~courseType() {
}
 
//List of Acessor and Getter Methods For Each Variable
void courseType::setTerm(string term) {
    this ->term = term;;
}
 
 
string courseType::getTerm() {
    return term;
}
void courseType::setYear(string temp) {
    this ->temp = temp;
}
 
 
string courseType::getYear() {
    return temp;
}
void courseType::setStart(string startDate) {
    this ->startDate = startDate;
}
 
 
string courseType::getStart() {
    return startDate;
}
void courseType::setEnd(string endDate) {
    this ->endDate = endDate;
}
 
 
string courseType::getEnd() {
    return endDate;
}
void courseType::setName(string courseName) {
    this ->courseName = courseName;
}
string courseType::getName() {
    return courseName;
}
 
void courseType::setSection(string section) {
    this ->section = section;
}
string courseType::getSection() {
    return section;
}
void courseType::setID(string tempC) {
    this->tempC = tempC;
}
string courseType::getID() {
    return tempC;
}
 
void courseType::setDays(string meetingDays) {
    this ->meetingDays = meetingDays;;
}
string courseType::getDays() {
    return meetingDays;
}
 
void courseType::setLocation(string location) {
    this ->location = location;
}
 
string courseType::getLocation() {
    return location;
}
 
void courseType::setInfo(string meetingInfo) {
    this ->meetingInfo = meetingInfo;
}
 
string courseType::getInfo() {
    return meetingInfo;
}
 
void courseType::setInstructor(string instructor) {
    this ->instructor = instructor;
}
 
string courseType::getInstructor() {
    return instructor;
}
void courseType::setUnits(string tempU) {
    this ->tempU = tempU;
}
string courseType::getUnits() {
    return tempU;
}
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//utils.cpp
#include <iostream>
#include <iomanip>
#include "utils.h"
#include "course.h"
 
 
#include <cstdlib>
#include <iostream>
#include <utility>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
 
 
using namespace std;
 
//Function Dislays list of Courses
void doDisplay(vector<courseType>& list) {
    cout << left << setw(10) << "Course List" << endl;
    cout << "==============================" << endl;
    for(int i = 0; i < list.size(); i++){
        cout << left << setw(10) << list[i].getName() << "" << endl << endl;
    }
}
//Function views individual course
void doView(vector<courseType>& list) {
    int chv = 0;
    
    for(int i = 0; i < list.size(); i++){
        cout << i+1 << ". " << list[i].getName() << "" << endl << endl;
    }
    cout << "Please choose a course you want to view: ";
    cin >> chv;
    cout << endl;
    if(chv<list.size()+1)
    {
        cout << left << setw(10) << "Term" << setw(10) << "Year"
        << setw(15) << "Start Date" << setw(15) << "End Date"
        << setw(35) <<"Course Name" << setw(15) << "Section"
        << setw(12) << "Class ID" << setw(17) << "Meeting Days"
        << setw(15) << "Location" << setw(20) << "Meeting Info"
        << setw(15) << "Instructor" << "Units" << endl;
        cout << "=============================================================================================";
        cout << "=============================================================================================" << endl;
        
        chv=chv-1;
        cout << left << setw(10) << list[chv].getTerm() << "" << setw(10) << list[chv].getYear() <<
        "" << setw(15) << list[chv].getStart() << "" << setw(15) << list[chv].getEnd() << "" <<
        setw(35) << list[chv].getName() << "" << setw(15) << list[chv].getSection() << "" <<
        setw(15) << list[chv].getID() << "" << setw(15) << list[chv].getDays() << "" <<
        setw(17) << list[chv].getLocation() << "" << setw(20) << list[chv].getInfo() << "" <<
        setw(15) << list[chv].getInstructor() << "" << setw(10) << list[chv].getUnits() << "" << endl;
 
        
    }
 
    }
 
//Function Adds Courses
void doAdd(vector<courseType>& list) {
 
    char choice;
    char delim = ',';
    string term = "";
    float year = 0.0;
    string start = "";
    string end = "";
    string name = " ";
    string section = "";
    float id = 0.0;
    string days = "";
    string location = "";
    string info = "";
    string instructor = "";
    float units = 0.0;
    ofstream outMyStream (FILE_NAME, ios::app);
     
         do {
             cout << "Enter term: " ;
             cin.ignore();
             getline(cin, term);
                 
                 cout << "Enter year: ";
             cin >> year;
             cin.ignore();
             
                 cout << "Enter start date: ";
             getline(cin, start);
             
                 cout << "Enter end date: ";
             getline(cin, end);
             
                 cout << "Enter course name: ";
             getline(cin, name);
             
                 cout << "Enter course section: ";
             getline(cin, section);
             
                 cout << "Enter course ID: ";
             cin >> id;
             cin.ignore();
             
                 cout << "Enter meeting Days : ";
             getline(cin, days);
             
                 cout << "Enter meeting location: ";
             getline(cin, location);
             
                 cout << "Enter meeting information: ";
             getline(cin, info);
             
                 cout << "Enter instructor name: ";
             getline(cin, instructor);
             
                 cout << "Enter units: ";
             cin >> units;
             cin.ignore();
                outMyStream << term << delim << year << delim << start << delim << end << delim << name << delim << section << delim
             << id << delim << days << delim << location << delim << info << delim << instructor << delim << units << endl;
  
             cout << "\nEnter another Record? (Y/N) ";
             cin >> choice;
             if (choice != 'Y' and choice != 'y' and choice != 'N' and choice != 'n') // if needed add input
                        cout << choice << " is not a valid option. Try agian" << endl;
             
            
    }while (choice != 'N' && choice != 'n');
    {
        outMyStream.close();
 
    }
 
    
}
 
//Function Edits Courses
void doEdit(vector<courseType>& list) {
    
}
        
//Function Deletes Courses
void doDelete(vector<courseType>& list) {
 
 
}
//Function Reads Data from "courses.csv"
void readData(vector<courseType>& list) {
 
    ifstream inFile;
    fstream fin;
    inFile.open(FILE_NAME);
    string line;
    while (getline(inFile, line)) {
        
        string term;
        float year;
        string startDate;
        string endDate;
        string courseName;
        string section;
        float classID;
        string meetingDays;
        string location;
        string meetingInfo;
        string instructor;
        float units;
        string temp;
        string tempU;
        string tempC;
        
        stringstream ss(line);
        getline(ss, term, ',');
        getline(ss, temp, ',');
        year = atoi(temp.c_str());
        getline(ss, startDate, ',');
        getline(ss, endDate, ',');
        getline(ss, courseName, ',');
        getline(ss, section, ',');
        getline(ss,tempC,',');
        classID = atoi(tempC.c_str());
        getline(ss, meetingDays, ',');
        getline(ss, location, ',');
        getline(ss, meetingInfo, ',');
        getline(ss, instructor, ',');
        getline(ss,tempU,',');
        units = atoi(tempU.c_str());
      
        
        courseType c;
        c.setTerm(term);
        c.setYear(temp);
        c.setStart(startDate);
        c.setEnd(endDate);
        c.setName(courseName);
        c.setSection(section);
        c.setID(tempC);
        c.setDays(meetingDays);
        c.setLocation(location);
        c.setInfo(meetingInfo);
        c.setInstructor(instructor);
        c.setUnits(tempU);
        list.push_back(c);
        
        
        
    }
        
    inFile.close();
 
    }
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
//course.h
#pragma once
#include <string>
#include <sstream>
 
using namespace std;
 
class courseType {
public:
    // mutator methods - set
    void setTerm(string term);
    void setYear(string temp);
    void setStart(string startDate);
    void setEnd(string endDate);
    void setName(string courseName);
    void setSection(string section);
    void setID(string tempC);
    void setDays(string meetingDays);
    void setLocation(string location);
    void setInstructor(string instructor);
    void setInfo(string meetingInfo);
    void setUnits(string tempU);
    // ...
 
    // accessor methods - get
    string getTerm();
    string getYear();
    string getStart();
    string getEnd();
    string getName();
    string getSection();
    string getID();
    string getDays();
    string getLocation();
    string getInstructor();
    string getInfo();
    string getUnits();
    
    // ...
 
    // Other member methods
    // ...
 
    courseType();
    ~courseType();
 
protected:
    // tbd
 
private:
    // data structures
    string term;
    float year;
    string startDate;
    string endDate;
    string courseName;
    string section;
    float classID;
    string meetingDays;
    string location;
    string meetingInfo;
    string instructor;
    float units;
    string temp;
    string tempU;
    string tempC;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//utils.h

#pragma once
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "course.h"
 
using namespace std;
 
const string FILE_NAME = "courses.csv";
 
// Prototype functions...
void doDisplay(vector<courseType>& list);
void doView(vector<courseType>& list);
void doAdd(vector<courseType>& list);
void doEdit(vector<courseType>& list);
void doDelete(vector<courseType>& list);
void readData(vector<courseType>& list);
Last edited on
1
2
3
4
5
6
7
//courses.csv
Spring,2022,1/24/2022,5/20/2022,Discrete Structures,CS-113-02,085689,T TH,On-Campus,Newark,Pham,3
Spring,2022,1/24/2022,5/20/2022,Programming W/ Data Structures,CS-124-02,084371,M W,On-Campus,Zoom,Pham,3
Spring,2022,1/24/2022,5/20/2022,Programming W/ Data Structures,CS-124-03,085698,T TH,Online,Newark,Pham,3
Spring,2022,1/24/2022,5/20/2022,JavaScript for Web Development,CS-175-01,084377,M,Online,Zoom,J. Pham,4
Spring,2022,1/24/2022,5/20/2022,Beginner Java,CS-125-05,089434,TH,Online,Zoom,Gao,3
 
Last edited on
@OP
Depends what the purpose of this exercise is. Reading the .csv file can be done this way without the need for gets and sets in such large volumes whether the attributes are private in a class or a struct.

(For all I know the exercise requires the Course data-line to be normalized in which case you have a lot more classes involved.)

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

struct Course
{
    std::string Term;
    int Year;
    std::string Start_Date, End_Date, Course_Name;
    std::string Section;
    int Class_ID;
    std::string Meeting_Days, Location, Meeting_Information, Instructor;
    int Units;
    
    void extract_data(std::vector<Course>&, std::string);
    bool IsFound (int ID);
};


std::ostream& operator<< (std::ostream& os, Course& cs)
{
    os
    << "COURSE DETAILS\n"
    << "TERM: " << cs.Term << '\n'
    << "YEAR: " << cs.Year << '\n'
    << "START DATE: " << cs.Start_Date << " END DATE: " << cs.End_Date << '\n'
    << "COURSE NAME: " << cs.Course_Name << " SECTION: " << cs.Section << '\n'
    << "CLASS ID: " << cs.Class_ID << '\n'
    << "MEETING DAYS: " << cs.Meeting_Days << ' '
    << "LOCATION: " << cs.Location << '\n'
    << "MEETING INFORMATION: " << cs.Meeting_Information << '\n'
    << "INSTRUCTOR: " << cs.Instructor << " UNITS: " << cs.Units << "\n\n";
    
    return os;
}


void Course::extract_data(std::vector<Course>& vc, std::string fn)
{
    const int MAX_NO_TOKENS{12};
    
    std::string line;
    std::stringstream iss;
    std::string token[MAX_NO_TOKENS];
    
    std::ifstream myfile(fn);
    
    Course tmp;
    
    int count{0};
    
    if (myfile.is_open())
    {
        while( getline(myfile, line, '\n') and !line.empty())
        {
            iss.str(line);
            int i = 0;
            
            while( getline(iss, token[i], ',') )
            {
                i++;
            }
            
            tmp.Term = token[0];
            tmp.Year = stoi(token[1]);
            tmp.Start_Date = token[2];
            tmp.End_Date = token[3];
            tmp.Course_Name = token[4];
            tmp.Section = token[5];
            tmp.Class_ID = stoi(token[6]);
            tmp.Meeting_Days = token[7];
            tmp.Location = token[8];
            tmp.Meeting_Information = token[9];
            tmp.Instructor = token[10];
            tmp.Units = stoi(token[11]);
            
            iss.clear();
            count++;
            
            vc.push_back(tmp);
        }
        myfile.close();
    }
    
    else std::cout << "Unable to open file";
}


int main ()
{
    Course temp;
    std::vector<Course> vc;
    temp.extract_data(vc, "courses.csv");
    
    // DISPLAY ALL COURSES
    for(auto i: vc)
        std::cout << i << '\n';
    
    // FIND A COURSE
    int ID{0};
    std::cout << "Enter Class ID: ";
    std::cin >> ID;
    auto it = find_if(vc.begin(), vc.end(),
                      [&ID](const Course& obj){return obj.Class_ID == ID;});
    if(it != vc.end())
        std::cout << *it << '\n';
    else
        std::cout << "Course not found ...\n";
    
    return 0;
}


COURSE DETAILS
TERM: Spring
YEAR: 2022
START DATE: 1/24/2022 END DATE: 5/20/2022
COURSE NAME: Discrete Structures SECTION: CS-113-02
CLASS ID: 85689
MEETING DAYS: T TH LOCATION: On-Campus
MEETING INFORMATION: Newark
INSTRUCTOR: Pham UNITS: 3


COURSE DETAILS
TERM: Spring
YEAR: 2022
START DATE: 1/24/2022 END DATE: 5/20/2022
COURSE NAME: Programming W/ Data Structures SECTION: CS-124-02
CLASS ID: 84371
MEETING DAYS: M W LOCATION: On-Campus
MEETING INFORMATION: Zoom
INSTRUCTOR: Pham UNITS: 3


COURSE DETAILS
TERM: Spring
YEAR: 2022
START DATE: 1/24/2022 END DATE: 5/20/2022
COURSE NAME: Programming W/ Data Structures SECTION: CS-124-03
CLASS ID: 85698
MEETING DAYS: T TH LOCATION: Online
MEETING INFORMATION: Newark
INSTRUCTOR: Pham UNITS: 3


COURSE DETAILS
TERM: Spring
YEAR: 2022
START DATE: 1/24/2022 END DATE: 5/20/2022
COURSE NAME: JavaScript for Web Development SECTION: CS-175-01
CLASS ID: 84377
MEETING DAYS: M LOCATION: Online
MEETING INFORMATION: Zoom
INSTRUCTOR: J. Pham UNITS: 4


COURSE DETAILS
TERM: Spring
YEAR: 2022
START DATE: 1/24/2022 END DATE: 5/20/2022
COURSE NAME: Beginner Java SECTION: CS-125-05
CLASS ID: 89434
MEETING DAYS: TH LOCATION: Online
MEETING INFORMATION: Zoom
INSTRUCTOR: Gao UNITS: 3


Enter Class ID: 84377
COURSE DETAILS
TERM: Spring
YEAR: 2022
START DATE: 1/24/2022 END DATE: 5/20/2022
COURSE NAME: JavaScript for Web Development SECTION: CS-175-01
CLASS ID: 84377
MEETING DAYS: M LOCATION: Online
MEETING INFORMATION: Zoom
INSTRUCTOR: J. Pham UNITS: 4


Program ended with exit code: 0
Last edited on
I suggest you create these methods in courseType:
1
2
    bool readCSV(istream &in);
    bool writeCSV(ostream &out);


These should be a matching pair. If you write a courseType with writeCSV() then you should be able to read it with readCSV.

Then simplify readData to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
void readData(vector<courseType>& list) {

    ifstream inFile;
    fstream fin;
    inFile.open(FILE_NAME);
    string line;
    courseType course;
    while (course.readCSV(inFile)) {
        list.push_back(course);
    }

    inFile.close();
}


Next create a writeData function that writes a vector of courseType objects.

Finally test this. Temporarily add this in main, right after the call to readData():
1
2
writeData(list);
return 0;


Run the program. You should end up with an identical copy of courses.csv.

Once you can read and write the csv, you can simply add a call to writeData() at the end of the program.
Topic archived. No new replies allowed.