struct item
{
string name,artist,category;
int stock;
float price;
int count;
item *nxt;
};
item *p=NULL;
item *current;
int main()
{
do_again(); //call do_again function, say goodbye to main until everything else is done running
return 0;
} //end function
void do_again()
{
int ans; //declare ans variable, this will be used OVER AND OVER
do
{
cout << "\t\t\t\t Main Menu\n";
cout << "\t\t\t\t ---------\n\n";
cout << " *******************************************************************\n";
cout << " * *\n";
cout << " * Add a new album ......................................... [1] *\n";
cout << " * *\n";
cout << " * Search for a particular album ........................... [2] *\n";
cout << " * *\n";
cout << " * Delete an album ......................................... [3] *\n";
cout << " * *\n";
cout << " * Print all albums ........................................ [4] *\n";
cout << " * *\n";
cout << " * Sales ................................................... [5] *\n";
cout << " * *\n";
cout << " * Exit .................................................... [6] *\n";
cout << " * *\n";
cout << " *******************************************************************\n";
cout << "\n -> Enter Selection : ";
cin >> ans;
cin.ignore(80,'\n'); //take in an answer
direction(ans); //send that answer to direction!
}
while(ans!=6); //keep on keepin on till they pick exit!
} //end function
void direction(int ans)
{
switch (ans) //roll through options
{
case 1: option_1(); //go to function 1
break;
case 2: option_2();
break;
case 3: option_3();
break;
case 4: option_4();
break;
case 5: option_5();
break;
case 6: option_6();
break;
default: cout << "Please read and follow all directions\n"; //if directions aren't followed
break;
}
} //end function
void option_2()
{
system("CLS");
ifstream in_file("database.txt");
if (!in_file.is_open()) {
cerr << "Error: could not find database.txt" << endl;
return;
}
// read album name to look for
item album_info;
cout << "Album Name: ";
if (!getline(cin, album_info.name)) {
cerr << "Error while reading from keyboard.\n";
cin.clear();
return;
}
// read line for line as long no error occurs
string line;
while (getline(in_file, line)) {
size_t pos = line.find(album_info.name);
if (pos == string::npos) {
continue;
}
// if you pass here, you found your album by name
const string kPrefixAlbumLines[] = {
"Album Artist : ",
"Category : ",
"Amount in Stock : ",
"Price : "
};
string album_lines[4];
for (int i = 0; i < 4; i++) {
if (!getline(in_file, album_lines[i])) {
cerr << "An error occured while reading from file.\n";
return;
}
album_lines[i].erase(0, kPrefixAlbumLines[i].length());
}
album_info.artist = album_lines[0];
stringstream converter;
converter.str(album_lines[1]);
if (!(converter >> album_info.category)) {
cerr << "Corruped data entry in category field!\n\n";
}
converter.str(album_lines[2]);
converter.clear();
if (!(converter >> album_info.stock)) {
cerr << "Corruped data entry in stock field!\n\n";
}
converter.str(album_lines[3]);
converter.clear();
if (!(converter >> album_info.price)) {
cerr << "Corruped data entry in price field!\n\n";
}
}
in_file.close();
// format your output as you wish. Maybe you should define an << operator to ostream for
// your info structure.
// if you keep output like this, it would be easier to print lines directly. With this method you
// can change the values of the info struct and blablah
cout << endl << endl;
cout << "Album Name : " << album_info.name << endl;
cout << "Album Artist : " << album_info.artist << endl;
cout << "Category : " << album_info.category << endl;
cout << "Amount in Stock : " << album_info.stock << endl;
cout << "Price : " << album_info.price << endl;
cout << "-------------------------";
cout << "\n\n";
cin.get();
system("CLS");
} //end function
delete is much more easier, because you dont need any parsing.
Just read line by line until found the album name. If so done, skip this and next 5-6 lines and write again to a copy of the file.
Your info struct contains the information as numeric data types. But we are reading the file line by line into a string. A stringstream is possibility to make a string into an iostream (means it is handled same like your output and input cout/cin). Like this we can "convert" and extract the values (given as a string) into any data type we like to. Just like with cout and cin.
Thats why i named the stringstream "converter".
With the method str() (which is the only one different from cin/cout) i set the new buffer string, we read from. To make sure we can read without being stopped by any error flags i have to clear every time after settings the buffer. And now we can use the stringstream "converter" just like we are used to with files or with cin or cout.
You know, thats standard when converting from strings into numeric values or the other way around. In C we could only use things like atoi() but streams are more powerfull.
i need to delete a specific album from my file
for that i need to search for that particular album first
that sort of is identical to the code for the second part where i need to search for a particular album
once found i need to remove all the details for that album
how to delete a line in a text file???