Trouble deleting and line is skipping when asking input

I'm trying to add, display, search, modify and delete. I've gotten to the add it skips the adding of the artist. The code compiles with no errors. The delete isn't working also I was wondering if anyone could help.

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
  #include<fstream>
#include<iomanip>
#include <string>
#include <iostream>





using namespace std;


class Album_l
{
private:
	int x = 0;
	int num;
	int album_ID;
	int talbum;
	string album_Artist;
	string album_Genre;
	string album_Format;
	string album_Name;
	string album;

public:
	void get_Album();
	void display_Albums();
	void search_Albums();
	void delete_Album();


};

void Album_l::display_Albums()
{
 ifstream myFile;
 myFile.open("musicL.dat");
 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)
{
	getline(myFile,album);
	cout<<album<<endl;
}	

}

void Album_l::delete_Album()
{
	ifstream myFile("musicL.dat");
	ofstream temp("temp.dat");
	
	cout<<"Enter the ID of the Album you want to delete: ";
	cin>>talbum;
	
	while(myFile>>album_ID>>album_Artist>>album_Genre>>album_Name>>album_Format)
	{
		if(talbum!=album_ID){
			temp<<album_ID<<" "<<album_Artist<<" "<<album_Genre<<" "<<album_Name<<" "<<album_Format<<endl;
		}
		if(talbum==album_ID)
		{
			x=1;
		}
	}
	myFile.clear();
	myFile.seekg(0, ios::beg);
	myFile.close();
	temp.close();
	rename("temp.dat", "musicL.dat");
	if(x==0){
		cout<<"There is no album that exists. "<<endl;
	}
	else{
		cout<<"The album has been deleted"<<endl;
	}
	
}

void Album_l::search_Albums()
{}


void Album_l::get_Album()
{
	ofstream myFile;
	myFile.open("musicL.dat", ios::app | ios::binary);
	
	cout<<"Enter Album ID"<<endl;
	cin>>album_ID; 
	cout<<"Enter the Artist"<<endl;
	getline(cin, album_Artist);
	cout<<"Enter the album_Genre";
	getline(cin, album_Genre);
	cout<<"Enter the album name";
	getline(cin,album_Name);
	cout<<"Enter the album format";
	getline(cin, album_Format);
	myFile<<setw(15)<<album_ID<<setw(15)<<album_Artist<<setw(15)<<album_Genre<<setw(15)<<album_Name<<setw(15)<<album_Format<<endl;
	myFile.close();
}

int main()
{

Album_l a;
bool notDone;
int choice;
do
{
	
cout << "\n \t\t\t 1. Add    Records";
cout << "\n \t\t\t 2. List   Records";
cout << "\n \t\t\t 3. Modify Records";
cout << "\n \t\t\t 4. Delete Records";
cout << "\n \t\t\t 5. Exit   Program";
cout << "\n\n";
cout << "\t\t\t Select Your Choice :=> ";
cin>>choice;

/* cin.ignore(256, 10); */
switch(choice)
{
case 1:
a.get_Album();
break;

case 2:
a.display_Albums();

break;

case 3:
a.search_Albums();
break;
case 4:
a.delete_Album();
break;
case 5:
cout<<"Good Bye!";
break;
default:
cout<<"Please try a valid number!";
break;

} 

}

while (choice!=5);




return 0;

}
a couple of question that I do hope you answer.
- ¿what does your `Album_l' class represent? ¿one album or a collection?
- all its method have the form void foo() they don't ask for a parameter and don't return a value, ¿why?
- ¿what's the purpose these member variables: `album', `talbum', `num', `x'?
- I suppose that `.getAlbum() ' is the one that adds elements, ¿why do you open the file in binary mode?
- ¿do you allow spaces or not? ¿where? an example file would help (note that your add function seems to allow, but the delete don't)
- «The delete isn't working» is not descriptive. ¿what does happen/doesn't happen? ¿musicL.dat remains untoched? ¿dissapear? ¿what about temp.dat? ¿does it still exist? ¿was even created in the first place?
- ¿do you need to open/close the file for every operation? ¿can't just load its content in a vector or something and write only at the end?

about the skipping part:
http://www.cplusplus.com/forum/beginner/250556/#msg1103311
Album_l is supposed to represent a collection of albums.

getAlbum() does add elements. The binary mode is something I should have taken out. I do allow spaces. num was just something I had just when trying to working I had from an earlier version. talbum was just an example that I was going to use to use an input when asking for deleting the album
I want to be able to delete an album when the option selection and it seems like the musicL.dat doesn't get touched but there is a temp.dat file that is created.

I was basically doing this for the deletion.
> Album_l is supposed to represent a collection of albums.
then your member variables make no sense.
a collection shouldn't have just one `album_Name' (¿which one of all it represents?)

> I do allow spaces
in delete_Album() you have while(myFile>>album_ID>>album_Artist>>album_Genre>>album_Name>>album_Format)
>> will stop at spaces, you need to use
1
2
3
myFile>>album_ID;
myFile.ignore(); //to discard the '\n'
getline(myFile, album_Artist); //and so on 


> musicL.dat doesn't get touched but there is a temp.dat file that is created.
perhaps rename() is failing
1
2
if( rename("temp.dat", "musicL.dat") == -1 )
   perror("rename ");
Topic archived. No new replies allowed.