-2

The object derm is out of scope in the switch statement. I tried to make it a static member function. Is there anyway I can make this work?

Here is my code:

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

using namespace std;

class Invinq {
    int menu_pick;
    string db_read_out;

public:
    Invinq();
    void menu_maker();
    int add_record();
    int change_record();
    int delete_record();
    int display_record();
    int exit_program();
};

Invinq::Invinq()
{
  cout <<"Welcome to Inventory Inquisator.\n********************************\n" << endl;

  ifstream db_invinq;                       
  db_invinq.open("Invinq_db.txt");

  if(!db_invinq)                            
  {
    cout<<"\nNot able to create a file. MAJOR OS ERROR!! \n";
  } 

  for(int i = 0; i < db_invinq.eof(); i++)
  {
    db_invinq >> db_read_out;
    cout << db_read_out;
  } 
}

//Menu maker
void Invinq::menu_maker()
{
  cout << "1. Add Record\n2. Change Record\n3. Delete Record\n4. Display Record\n5. Exit\n\nPick One: ";
  cin >> menu_pick;

      switch(menu_pick)
      {
        case 1: derm.add_record(); break;
        case 2: derm.change_record(); break;
        case 3: derm.delete_record(); break;
        case 4: derm.display_record(); break;
        default: cout << "Pick a number between 1-5, try again\n";
      }
      derm.menu_maker();  
}

int main() {      
  Invinq derm;
  derm.menu_maker();

  return 0;
}
Daniel Trugman
  • 8,186
  • 20
  • 41

2 Answers2

1

You seem to have completely missed the point. you don't need derm when you are already inside the Invinq class - just call menu_maker().

Having said that: you are using recursion for no good reason. This can have some unexpected side effects. I suggest you refactor menu_maker() - if fact right now there is no way to get out of the recursion, so that is really bad!

You main should have the loop and create a Invinq each time through, otherwise you are just overwriting the same object each time which is probably now what you want.

John3136
  • 28,809
  • 4
  • 51
  • 69
0

Inside the definition of your class method, you should refer to yourself using the keyword this.

Replace all the method calls as following:

case 1: derm.add_record(); break; > case 1: this->add_record(); break;

Note: this returns a pointer to your object, this is why we use -> rather than .

Daniel Trugman
  • 8,186
  • 20
  • 41