hi guys......i need to write a C++ program to allow the user to store new albums in a text file, be ale to view all his records at any time, to delete any particular record in the text file, to search for a particular record n decrement the quantity in stock of an album when there is a sale......here is what i managed to do until now

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <cstdio>

using namespace std;
 
void do_again(),direction(int ans);
void option_1(),option_2(),option_3(),option_4(),option_5(),option_6();
void choice_1();

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, main remains hidden @ the back until everything else is done running
 
     return 0;
} //end function
 
void do_again()
{
     int ans; //declare ans variable
     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 exit is picked!
} //end function
 
void direction(int ans)
{
     switch (ans) //roll through options
     {
          case 1: option_1(); //go to function 1
               break;
          case 2: option_2(); //go to function 2
               break;
          case 3: option_3(); //go to function 3
               break;
          case 4: option_4(); //go to function 4
               break;
          case 5: option_5(); //go to function 5
               break;
          case 6: option_6(); //go to function 6
               break;
          default: cout << "Please read and follow all directions\n"; //if directions aren't followed
               break;
     }
} //end function
 
//Functions

void option_1()
{    
    system("CLS");
    
    item *list,*temp2;
    
    list = new item;
	
	cout<<"Album details\n";
	cout<<"-------------\n\n";

	cout<<"Album Name: ";
	getline(cin, list->name);
	
	cout<<"Album Artist: ";
	getline(cin, list->artist);
	
	cout<<"Category: ";
	cin>>list->category;
	
	cout<<"Amount Bought: ";
	cin>>list->stock;
	
	cout<<"Price: ";
	cin>>list->price;	

    list->nxt=NULL;
    
    if (p == NULL)
    {
         p = list;
         current=p;
    }
    else
    {
         temp2 = p;
         
         while (temp2->nxt != NULL)
           {  
               temp2 = temp2->nxt;
           }
         temp2->nxt = list;        
    }
    
    item *temp;
    temp = p;
    
  	ofstream f;
	f.open("database.txt", ios::app); //opens the file database.txt and appends it....else creates it if it doesnt exist
      	
    while (temp != NULL)
    {
        f<<"Album Name : " <<temp->name<<endl;
        f<<"Album Artist : " <<temp->artist<<endl;
        f<<"Category : " <<temp->category<<endl;
        f<<"Amount in Stock : " <<temp->stock<<endl;
        f<<"Price : " <<temp->price<<endl;
        f<<"----------------------------------";
        f<<"\n\n";
  		temp = temp->nxt;         
    }
    
    f<<endl;
    
    // displaying item entered
	cout<<"\nAlbum " << list->name<<" added successfully.\n\n";
	
	f.close(); //closes the text file
	    
    choice_1(); //go to function choice_1
   
    system("CLS");

}//end function
 
void option_2()
{
    system("CLS");
    //opens the text file database.txt or displays error message if not found
    ifstream in_file("database.txt");
    if (!in_file.is_open()) 
    {
        system("CLS");
        cerr << "Error: could not find the database file\n" << endl;
        system("pause");
        system("CLS");
        return;
    }
    // read album name to look for
    item album_info;
    cout << "Enter the 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(); //closes the text file 
    
    //display error message if album name entered is not found
    if (album_info.artist.length() == 0)
    {
            cout << "\n\nThe album doesnt exist or the name has been mispelled"<< endl;
            cin.get();
            system("CLS");
            return;
    }
    
    //display required album information onto screen
    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 << "\n\n";
    
    system("pause");
    system("CLS");
}//end function
 
void option_3()
{
      
}//end function
 
void option_4()
{
  system("CLS");  
  
  string line;
  
  ifstream f("database.txt");
  if (!f.is_open()) 
  {
        system("CLS");
        cerr << "Error: could not find the database file\n" << endl;
        system("pause");
        system("CLS");
        return;
  }
  
  if (f.is_open())
  {
    //while the end of file is not encountered store everything in text file in string "line" and output "line" to the screen
    while (! f.eof() )
    {
      getline (f,line);
      cout << line << endl;
    }
    f.close();
    
    system("pause");
    system("CLS");  
  }

  else cout << "Unable to open file";
}//end function
 
void option_5()
{
}//end function
 
void option_6()
{
}//end function

void choice_1()
{
    char ans;
	
	cout<<"Do you want to enter another item?(y/n)";
	cin>>ans;
	if((ans=='Y') || (ans=='y'))
	{
          option_1();
    }
    
    if((ans=='N') || (ans=='n'))
    {
    
    }
    
    else
    {
        cout << "\nWrong choice!!\n\n";
        choice_1();
    }
}

i am sort of new to programming and i wrote all this from examples i found here and there and adjusted the codes for my program

there is a problem with my insert code....that is the option_1() function.....whenether i add a new album...i get the message whether to add another album or not
if i press "y"....i can't enter data for the first element of my struct...that is the album name
Also...if press "n" and returns to the main menu and then decides to enter a new album and press "1" again....it rewrites all the details of the previous album i entered again in the text file in addition to the new details i enter

please help!!!

>>i wrote all this from examples i found here and there and adjusted the codes for my program

That's one approach I guess, but I sure hope you understood the code you found or you're likely to be in over your head pretty fast.

>>whenether i add a new album...i get the message whether to add another album or not
if i press "y"....i can't enter data for the first element of my struct...that is the album name

Are you sure the input buffer is empty before calling getline()?

>>if press "n" and returns to the main menu and then decides to enter a new album and press "1" again....it rewrites all the details of the previous album i entered again in the text file in addition to the new details i enter

Don't write to file until you have to! Restructure the program so that in main() it opens the file, reads the information in the file to a list and then manipulates that list by passing that list to the functions that add, find, print, deletes, etc from that list. When the manipulation of the list is completed then rewrite the whole, changed, manipulated list, back to the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.