I've been struggling with this program for few several days now..
It's an implementation of Data structures. I'm trying to use Linked Lists, Sorting and Searching concepts.

#include<iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

class videoShopSystem{
    public: 
         bool empty() const;   
         void displayMainMenu();
         void menuChoice(int key);
         void addNewRecord();
         void displayRecord();
         void editRecord(); //editing
         void deleteRecord();//deleting
         void saveRecord(string filename);//saving
         void loadRecord(string filename);//loading
         void exit();
         
         videoShopSystem():capacity(10), size(0)
         {
             head = tail = cur = prev = -1; //list is empty
             data = new ListNode[capacity];                   
         }
    
    private: 
         class ListNode{
               public:
                   int VideoID;
                   string VideoName;
                   int next;
                   
                   ListNode():VideoID(0), next(-1){}    
          };                
          ListNode *data;
          int size, capacity;
          int head, tail, cur, prev;
        
};

void videoShopSystem::addNewRecord() //implementation of linked list
{
 
     if(size!=capacity)
     {
      if(head == tail) {head = 0; tail = 0;}
      
      cur = tail;
      tail++;
                       
      cout << "Video Record ID: ";
      cin >> data[cur].VideoID;
      cout << "Movie/Video Title: ";
      cin.ignore(1, '\n');
      getline(cin, data[cur].VideoName);
      cout <<endl;
      
      size++;
     }
     else
         cout<< "Capacity reached maximum!!!";
}           

void videoShopSystem::displayRecord()
{
     cout << "Video ID\t\tVideoName" <<endl;
     cout << "------------------------------"<<endl;
     
     for(int i=head; i<size; i++)
     cout << data[i].VideoID <<"\t\t" <<data[i].VideoName<<endl;
      
}
/*
void videoShopSystem::editRecord()
{
     cout << "Enter Record ID: ";
     cin >> data[cur].VideoID;
     



}
*/

void videoShopSystem::saveRecord(string filename)
{
   ofstream fout(filename.c_str());
   if (fout.is_open()) 
    {
     if ( empty() ) 
      {
      cout << "Nothing on List. No Save" << endl;
      system("PAUSE");
      
      }else {
      cur = head;//current is at start
      while (cur != -1)// not at end
      {
        fout << data[cur].VideoID;
        cur = data[cur].next;//loop to next until =-1(end)
      }
    }
  } 
  else 
  {
    cerr << "Can't open file " << filename << endl;//incase of error
    system("PAUSE");
    } 
 fout.close();  
  

}
void videoShopSystem::displayMainMenu()
{
    cout<< "---------------Video Shop System--------------" <<endl;
    //cout<< "-----------------" <<endl;
    cout<< "           1) Add New Record"<<endl;
    cout<< "           2) View All Records"<<endl;
    cout<< "           3) Edit Record"<<endl;
    cout<< "           4) Delete Record"<<endl;
    cout<< "           5) Save Record"<<endl;
    cout<< "           6) Load Record"<<endl;
    cout<< "           7) Quit"<<endl<<endl;
    
    
    cout<< "Enter Choice: ";

}

void videoShopSystem::menuChoice(int key)
{
    switch(key)
    {
               case 1: cout<<"# Add New Record #"<<endl;
                       addNewRecord();
               break;
               case 2: cout<<"# View All Records #"<<endl;
                       displayRecord();
               break;
               case 3: cout<<"# Edit Record #"<<endl;
                       
               break;
               case 4: cout<<"# Delete Record #"<<endl;
               break;
               case 5: cout<<"# Save Record #"<<endl;   
                       saveRecord(string filename);                        
               break;
               case 6: cout<<"# Load Record #"<<endl;
               break;
               case 7: cout<<"Game Over!"<<endl;
               break;
    }
    system("pause");
    system("cls");  
     
}


int main(){
    
    videoShopSystem obj1;
    string filename;
    int key;
    
    do{
    
    obj1.displayMainMenu();
    cin >>key;
    system("cls");
    obj1.menuChoice(key);
    
    }while(key!=7);
    
    
    //system("pause");
    return 0;
}

I'm currently working on the save function which is where I've hit a wall. At this point when I compile I get the following error..

In member function `void videoShopSystem::menuChoice(int)':
147 expected primary-expression before "filename"

What's wrong with my code ?

Recommended Answers

All 11 Replies

line 147 calls a function which expects an string as input: saveRecord(string filename); You can't declare a string like that. You could solve the problem in two ways:

Change line 147 to:

saveRecord("c:\\a_file.txt");

or:

string filename = "c:\\a_file.txt");
saveRecord(filename);

Another problem is that you never define the function bool empty() const;

line 147 calls a function which expects an string as input: saveRecord(string filename); You can't declare a string like that. You could solve the problem in two ways:

Change line 147 to:

saveRecord("c:\\a_file.txt");

or:

string filename = "c:\\a_file.txt");
saveRecord(filename);

Another problem is that you never define the function bool empty() const;

Tried that and I get a linker error..

[Linker error] undefined reference to `videoShopSystem::saveRecord(std::string)'
ld returned 1 exit status

Tried that and I get a linker error..

That shouldn't happen.. Could you post the code that gives you that error?

#include<iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

class videoShopSystem{
    public: 
         bool empty() const;   
         void displayMainMenu();
         void menuChoice(int key);
         void addNewRecord();
         void displayRecord();
         void editRecord(); //editing
         void deleteRecord();//deleting
         void saveRecord(string filename);//saving
         void loadRecord(string filename);//loading

         
         videoShopSystem():capacity(10), size(0)
         {
             head = tail = cur = prev = -1; //list is empty
             data = new ListNode[capacity];                   
         }
    
    private: 
         class ListNode{
               public:
                   int VideoID;
                   string VideoName;
                   int next;
                   
                   ListNode():VideoID(0), next(-1){}    
          };                
          ListNode *data;
          int size, capacity;
          int head, tail, cur, prev;
        
};

void videoShopSystem::addNewRecord() //implementation of linked list
{
 
     if(size!=capacity)
     {
      if(head == tail) {head = 0; tail = 0;}
      
      cur = tail;
      tail++;
                       
      cout << "Video Record ID: ";
      cin >> data[cur].VideoID;
      cout << "Movie/Video Title: ";
      cin.ignore(1, '\n');
      getline(cin, data[cur].VideoName);
      cout <<endl;
      
      size++;
     }
     else
         cout<< "Capacity reached maximum!!!";
}           

void videoShopSystem::displayRecord()
{
     cout << "Video ID\t\tVideoName" <<endl;
     cout << "------------------------------"<<endl;
     
     for(int i=head; i<size; i++)
     cout << data[i].VideoID <<"\t\t" <<data[i].VideoName<<endl;
      
}
/*
void videoShopSystem::editRecord()
{
     cout << "Enter Record ID: ";
     cin >> data[cur].VideoID;
     



}
*/

void videoShopSystem::saveRecord(string filename)
{
   ofstream fout(filename.c_str());
   if (fout.is_open()) 
    {
     if ( empty() ) 
      {
      cout << "Nothing on List. No Save" << endl;
      system("PAUSE");
      
      }else {
      cur = head;//current is at start
      while (cur != -1)// not at end
      {
        fout << data[cur].VideoID;
        cur = data[cur].next;//loop to next until =-1(end)
      }
    }
  } 
  else 
  {
    cerr << "Can't open file " << filename << endl;//incase of error
    system("PAUSE");
    } 
 fout.close();  
  

}
void videoShopSystem::displayMainMenu()
{
    cout<< "---------------Video Shop System--------------" <<endl;
    //cout<< "-----------------" <<endl;
    cout<< "           1) Add New Record"<<endl;
    cout<< "           2) View All Records"<<endl;
    cout<< "           3) Edit Record"<<endl;
    cout<< "           4) Delete Record"<<endl;
    cout<< "           5) Save Record"<<endl;
    cout<< "           6) Load Record"<<endl;
    cout<< "           7) Quit"<<endl<<endl;
    
    
    cout<< "Enter Choice: ";

}

void videoShopSystem::menuChoice(int key)
{
    switch(key)
    {
               case 1: cout<<"# Add New Record #"<<endl;
                       addNewRecord();
               break;
               case 2: cout<<"# View All Records #"<<endl;
                       displayRecord();
               break;
               case 3: cout<<"# Edit Record #"<<endl;
                       
               break;
               case 4: cout<<"# Delete Record #"<<endl;
               break;
               case 5: cout<<"# Save Record #"<<endl;   
                       saveRecord("c:\a_file.txt");                       
               break;
               case 6: cout<<"# Load Record #"<<endl;
               break;
               case 7: cout<<"Game Over!"<<endl;
               break;
    }
    system("pause");
    system("cls");  
     
}


int main(){
    
    videoShopSystem obj1;
    string filename;
    int key;
    
    do{
    
    obj1.displayMainMenu();
    cin >>key;
    system("cls");
    obj1.menuChoice(key);
    
    }while(key!=7);
    
    
    //system("pause");
    return 0;
}

That's the code that gives me the linker error

You forgot to #include <string> Also, you need to define the 'empty' function. Something like:

bool videoShopSystem::empty() const
{
    //do stuff
    return true;
}

And last but not least: "c:\a_file.txt" is not the same as "c:\\a_file.txt" The extra slash is there for a reason. It's called an escape-character

Thanks.. The errors are gone.

But the funny thing is when I save a few records for example..

1 Titanic
2 Star Wars

On opening the file I only see the number "1"

Do you know why that happens ? Then maybe I can work from there.

First of all you are only outputting the videoID to the file.

Second: in the displayRecord method you use a for-loop to iterate through all the nodes in the list, but in the saveRecord you use a while-loop.

If the for-loop works, why not re-use that piece of code. Except when outputting to the screen, output to the file.

Nice :) Thanks

One hurdle down...

I'll keep you posted

This is how far I've gone with the Load (from a file) function... Line 188

#include<iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

class videoShopSystem{
    public: 
         bool empty() const;   
         void displayMainMenu();
         void menuChoice(int key);
         void push_back(char ch);
         void addNewRecord();
         void displayRecord();
         void editRecord(); //editing
         void deleteRecord();//deleting
         void saveRecord(string filename);//saving
         void loadRecord(string filename);//loading
         void clear();
         int size1() const;
         
         videoShopSystem():capacity(10), size(0)
         {
             head = tail = cur = prev = -1; //list is empty
             data = new ListNode[capacity];                   
         }
    
    private: 
         int getFirstEmptySlot();
         void increaseCapacity();
             
         class ListNode{
               public:
                   int VideoID;
                   string VideoName;
                   int next;
                   
                   ListNode():VideoID(0), next(-1){}    
          };                
          ListNode *data;
          int size, capacity;
          int head, tail, cur, prev;
        
};

bool videoShopSystem::empty() const //checks if list is empty
{
     if (head == -1) return true;
     return false;
}

void videoShopSystem::clear() // Function to clear list before reading data
{
 size = 0;
 head = tail = cur = prev = -1;

 for (int i=0; i<capacity; i++)
    data[i].VideoID = '\0';
}

int videoShopSystem::size1() const //Size function
{
 return size;
}

void videoShopSystem::push_back(char ch) //push Back
{
 if ( empty() )
  {
   data[0].VideoID = ch;
   data[0].VideoID = -1;
   head = tail = 0;
  } 
  else 
    {
     int slot = getFirstEmptySlot();
     data[slot].VideoID = ch;
     data[slot].next = -1;

     data[tail].next = slot;
     tail = slot;
    }
 size++;    
}

int videoShopSystem::getFirstEmptySlot() //return first empty slot in list
{
 for (int i=0;i<capacity;i++)
   if (data[i].VideoID == '\0')
      return i;
 int firstempty = capacity;
 increaseCapacity();
 return firstempty;    
}

void videoShopSystem::increaseCapacity() //increase storage/list capacity
{
 ListNode* newdata = new ListNode[ capacity*2 ];
 for (int i=0; i<capacity; i++)
    newdata[i] = data[i];
 for (int i=capacity; i<capacity*2; i++)
   {
    newdata[i].VideoID = '\0';
    newdata[i].next = -1;
   } 
 delete[] data;
 data = newdata;
 capacity *= 2;
}

void videoShopSystem::addNewRecord() //implementation of linked list
{
 
     if(size!=capacity)
     {
      if(head == tail) {head = 0; tail = 0;}
      
      cur = tail;
      tail++;
                       
      cout << "Video Record ID: ";
      cin >> data[cur].VideoID;
      cout << "Movie/Video Title: ";
      cin.ignore(1, '\n');
      getline(cin, data[cur].VideoName);
      cout <<endl;
      
      size++;
     }
     else
         cout<< "Capacity reached maximum!!!";
}           

void videoShopSystem::displayRecord()
{
     cout << "Video ID\t\tVideoName" <<endl;
     cout << "----------------------------------"<<endl;
     
     for(int i=head; i<size; i++)
     cout << data[i].VideoID <<"\t\t\t" <<data[i].VideoName<<endl;
      
}
/*
void videoShopSystem::editRecord()
{
     cout << "Enter Record ID: ";
     cin >> data[cur].VideoID;
     



}
*/

void videoShopSystem::saveRecord(string filename) // Save Function
{
   ofstream fout(filename.c_str());
   if (fout.is_open()) 
    {
     if ( empty() ) 
      {
      cout << "Nothing on List. No Save" << endl;
      system("PAUSE");
      
      }else {
      cur = head;//current is at start
      /*while (cur != -1)// not at end
      {
        fout << data[cur].VideoID;
        fout << data[cur].VideoName; 
        cur = data[cur].next;//loop to next until =-1(end)
      }*/
      for(int i=head; i<size; i++) //replace while
     fout << data[i].VideoID <<"\t\t\t" <<data[i].VideoName<<endl;
    }
  } 
  else 
  {
    cerr << "Can't open file " << filename << endl;//incase of error
    system("PAUSE");
    } 
 fout.close();  

}

void videoShopSystem::loadRecord(string filename) // Load Function 
{
  ifstream fin(filename.c_str());
  char item;//declare
  if (fin.is_open()) 
    {
     clear();  //clear the list before reading data
     do 
    {
       item = fin.get();
       if( !fin.eof() )//returns true if end of file has been reached
       push_back(item);// read item
    } 
    while( !fin.eof() );//end of file has not been reached
  } 
  else 
  {
    cerr << "Can't open file " << filename << endl;
    system("PAUSE");
    } 
 fin.close();   
}


void videoShopSystem::displayMainMenu()
{
    cout<< "---------------Video Shop System--------------" <<endl;
    //cout<< "-----------------" <<endl;
    cout<< "           1) Add New Record"<<endl;
    cout<< "           2) View All Records"<<endl;
    cout<< "           3) Edit Record"<<endl;
    cout<< "           4) Delete Record"<<endl;
    cout<< "           5) Save Record"<<endl;
    cout<< "           6) Load Record"<<endl;
    cout<< "           7) Quit"<<endl<<endl;
    
    
    cout<< "Enter Choice: ";

}

void videoShopSystem::menuChoice(int key)
{
    switch(key)
    {
               case 1: cout<<"# Add New Record #"<<endl;
                       addNewRecord();
               break;
               case 2: cout<<"# View All Records #"<<endl;
                       displayRecord();
               break;
               case 3: cout<<"# Edit Record #"<<endl;
                       
               break;
               case 4: cout<<"# Delete Record #"<<endl;
               break;
               case 5: cout<<"# Save Record #"<<endl;   
                       saveRecord("c:\\a_file.txt");                       
               break;
               case 6: cout<<"# Load Record #"<<endl;
                       loadRecord("c:\\a_file.txt");
               break;
               case 7: cout<<"Game Over!"<<endl;
               break;
    }
    system("pause");
    system("cls");  
     
}


int main(){
    
    videoShopSystem obj1;
    string filename;
    int key;
    
    do{
    
    obj1.displayMainMenu();
    cin >>key;
    system("cls");
    obj1.menuChoice(key);
    
    }while(key!=7);
    
    
    //system("pause");
    return 0;
}

I've managed to run the program without any errors but It doesn't give me what I want. After I load an already saved file and try using the display function, the output is a list of numbers instead of what I have saved.

What am I doing wrong ? The load function starts at Line 188

Anyone ?

int get();
Extracts a character from the stream and returns its value (casted to an integer).

So the get function just get's a character and formats it to a string.

You probably want to use the getlinel function, and decompose the line in the ID and the number.

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.