Results 1 to 2 of 2

Thread: member function not getting called? inside switch case

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    member function not getting called? inside switch case

    Hello everyone!!
    I'm trying to create an inventory program here, and i dont' know C, but I have been trying to learn C++. I coudn't seem to find any C++ libs that worked as well as the <stdio.h> lib, so i used that inside this project...well when i go to add a record, it seems as if the function doesn't even get called! If u compile this code, Hit 1, and then hit 1 again to create the new DB, and then hit 2, to add a record adn see for urself, i put a statement to see if teh add record function even got called but it looks as if it isn'te ven getting called! I may have made an error in the C code becuase i dont' know C, i just looked at the C syntax of the function and tried my best heh, but here is some notes if u don't understand FILE pointers...
    fp is the file pointer, and this shows where u want it to point too... FILE*fp = openf("test","w"); the "w" will create a file called test, and if there is another file called test, it will delete the old file! so for the add record i tried, fp=openf("test","a") which is supppose to let me add thigns to the file, without erasing the orginal contents...



    here is the code

    Code:
    //animals.h
    //Age, type, health, sex, characteristics, date found, location, other
    #include <iostream.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    class Animal
    {
    public:
    	int menu();
    	void createDB();
    	void addRecord();
    	void delRecord() { cout<<"delete record function\n"; }   //working on
    	void search() { cout <<"search record function\n"; }	//working on 
    	void quit() { exit(1);}
    	
    private:
    	int age;
    	char type[30];
    	char health[20];
    	char sex;
    	char discript[100];			//not used yet
    	char location[50];			//not used yet
    	char other[100];			//not used yet
    	FILE* fp;                 //file pointer
    };
    
    
    
    void Animal::createDB()
    {	
    	fp = fopen("inventory","w");          //writing privallages only
    	cout<<"Database Created!\n";
    	fclose(fp);
    }
    
    
    void Animal::addRecord()
    {
    	cout<<"add record called\n";
    	fp= fopen("inventory","a");
    	cout<<"/nEnter Data\n";
    	cout<<"age   type   health  sex\n";
    	
    	fscanf(stdin,"%d %s %s %c", &age,&type,&health,&sex);		//get input
    	fprintf(fp,"%d %s %s %c",age,type,health,sex);				//print to file
    	
    	fclose(fp);				//closing file
    }
    
    int Animal::menu()
    {
    	int choice;
    	cout<<".::--------------------Animal Menu---------------------::.\n";
    	cout<<"(1) Create a Database\n";
    	cout<<"(2) Add a new record\n";
    	cout<<"(3) Delete a record\n";
    	cout<<"(4) Search\n";
    	cout<<"(5) Return to Main Menu\n";
    	cout<<".::----------------------------------------------------::.\n";
    	cin >> choice;
    	switch(choice)
    	{
    	case 1: createDB();
    		break;
    	case 2: addRecord();
    		break;
    	case 3: delRecord();
    		break;
    	case 4: search();
    		break;
    	case 5: return 0;
    		break;
    	default: cout <<"Error.  Please enter a choice, 1-4\n";
    	}
    	menu();
    	return 0;
    }
    
    
    
    
    
    
    class Date
    {
    public:
    	int mm;
    	int dd;
    	int yy;
    };
    
    
    void intro()
    {
    	cout<<"**********************************************************\n";
    	cout<<"|                  Welcome To Project FluX               |       \n";
    	cout<<"**********************************************************\n";
    	cout<<"\n\n";
    }
    
    
    
    //*******Main Menu*****
    void mainMenu()
    {
    	Animal animal;
    	int choice;
    	cout<<".::---------------------Main Menu----------------------::.\n";
    	cout<<"(1) Create a Database\n";
    	cout<<"(2) Add a new record\n";
    	cout<<"(3) Delete a record\n";
    	cout<<"(4) Search\n";
    	cout<<"(5) Quit\n";
    	cout<<".::----------------------------------------------------::.\n";
    	cin >> choice;
    	switch(choice)
    	{
    	case 1: animal.menu();
    			mainMenu();
    		break;
    	case 2: animal.menu();
    			mainMenu();
    		break;
    	case 3: animal.menu();
    			mainMenu();
    		break;
    	case 4: animal.menu();
    			mainMenu();
    		break;
    	case 5: cout<<"thank you for using my program\n";
    		exit(1);
    		break;
    	default: cout <<"Error.  Please enter a choice, 1-4\n";
    	}
    	mainMenu();
    }

    here is the main.cpp
    Code:
    #include "animals.h"
    
    //prototypes
    void intro();
    void mainMenu();
    
    
    int main()
    {
    	intro();
    	mainMenu();
    
    return 0;
    }

    thanks for listneing

  2. #2

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Thumbs up

    Ahh i finally fixed it , i had to rewrite the whole functions, and I gave up using C and C++, so i just used C++'s <fstream> it seems alot more easier to read and unsterstand

    and i just used
    PHP Code:
    ofstream outfile("inventory.txt",ios::ate); 
    to append to the file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width