Modifying Records and delete

Okay so I'm working on a program that is supposed to modify, delete, and add records I have written some of the programs but I was curious if I'm going in the right direction. It is able to display the records I and they are able to be entered but having a hard time to find out on how to delete or modify. I'm just trying to get better at this.
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
  #include<fstream>
#include<string>
#include<iomanip>

#include<iostream>
using namespace std;
void outputLine (string album_Name, string album_Artist, string  album_Genre, string album_Type) {
    cout << setiosflags (ios::left) << setw(10) << album_Name
        << setw(20) << album_Artist
        << setw(10) << album_Genre
        << resetiosflags (ios::left)
        <<  album_Type << '\n';
}
void display_Album() {
    ifstream myfile ("musiclib.txt", ios::in);
    if (!myfile) {
        cerr << "File could not be opened\n";
        exit(1);
    }
    string album_Name, album_Artist, album_Genre, album_Type;
    cout << setiosflags(ios::left) << setw(10) << "Album Name"
        << setw(20) << "Album Artist" << setw(10) << "Album Genre" << setw(10) << "Album Type\n"
        << setiosflags(ios::fixed | ios::showpoint);

    while (myfile >> album_Name >> album_Artist >> album_Genre >> album_Type) {
        outputLine (album_Name, album_Artist, album_Genre, album_Type);
    }
}
void delete_Album () {
    /*ofstream myfile ("musiclib.txt", ios::app);
    if (!myfile) {
        cout << "File could not be opened." << endl;
        exit(1);
    }*/
  string album_Name, name;
  cout << "Please Enter the name of record you want to delete: ";
  cin >> name;
  ifstream myfile;
  ofstream temp;
  myfile.open("musiclib.txt");
  temp.open("temp.txt");
  while (getline(myfile, album_Name))
  {
    if (album_Name != name)
      temp << album_Name << endl;
  }
  cout << "The record with the name " << album_Name << " has been deleted if it exsisted" << endl;
  myfile.close();
  temp.close();
  remove("musiclib.txt");
  rename("temp.txt", "musiclib.txt");
	
      
}
void add_Album() {
    ofstream myfile ("musiclib.txt", ios::app);
    if (!myfile) {
        cout << "File could not be opened." << endl;
        exit(1);
    }
	string album_Name, album_Artist, album_Genre, album_Type;
    cout <<"Please enter an album name: ";
	getline(cin, album_Name);
	cout<<"Please enter an album artist: ";
	getline(cin, album_Artist);
	cout<<"Please enter an album genre: ";
	getline(cin, album_Genre);
	cout<<"Please enter the media format: ";
	getline(cin, album_Type);
    myfile << album_Name << ' ' << album_Artist<< ' ' << album_Genre << ' ' << album_Type << '\n';
	
}
/*int choice;

int main(){
	do{
	cout <<"To add an album press [1]";
	cout <<"To modify an album press [2]: ";
	cout<<"To delete an album press [3]: ";
	cout<<"To search press [4]: ";
	
	
	string album_Name, album_Artist, album_Genre, album_Type;
	ofstream myfile;
	myfile.open("musiclib.txt");
	cout <<"Please enter an album name: ";
	getline(cin, album_Name);
	cout<<"Please enter an album artist: ";
	getline(cin, album_Artist);
	cout<<"Please enter an album genre: ";
	getline(cin, album_Genre);
	cout<<"Please enter the media format: ";
	getline(cin, album_Type);

	myfile.close();
	
	}while choice (!=5);
	
	
	return 0;
}*/
int main () {
    
    char temp;
	int choice;
    do {
        cout <<"To add an album press [1]\n";
	    cout <<"To modify an album press [2]: \n";
	    cout<<"To delete an album press [3]: \n";
	    cout<<"To search press [4]:\n";
        cin>>choice;
		cin.ignore();
        switch (choice) {
            case 1:add_Album(); 
			break;
			case 2:delete_Album();
			break;
           case 3:display_Album();
		   break;
       
            default: cout << "That is not a valid option!" << endl;
        }
        if (choice!=2) {
   	  	 	cout << "Press any key to continue" << endl;
   	  	 	cin >> temp;
      	}
    } 
	while (choice!=2);
}
Consider a couple of utility functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool readMusicLine(ifstream &myfile, 
                   string &album_Name, string &album_Artist,
                   string &album_Genre, string &album_Type ) {
    myfile >> album_Name >> album_Artist >> album_Genre >> album_Type;
    myfile.ignore(1,'\n');
    return myfile.good();
}

bool writeMusicLine(ofstream &myfile, 
                    const string &album_Name, const string &album_Artist,
                    const string &album_Genre, const string &album_Type ) {
    myfile << album_Name << album_Artist << album_Genre << album_Type << endl;
    return myfile.good();
}


The first exercise is to write the 'copy_Album' function, and make sure you end up with two identical files.


Topic archived. No new replies allowed.