Thread: problem with function calling

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    16

    problem with function calling

    here is my code

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define MAX 10
    char choice;
    char type;
    int i;
    //-----------------------------------
    //base and derived classes
    class books {
     public:
    struct database {
        char title[256];
        char author[256];
    	char isbn[11];
    	char publishername[256];
    	char cdrom[4];
    	char yearofpub[5];
    	char price[5];
      }np[MAX];   // end of struct
    //..........................
      void find(void);
      void enter(int);
      void remove(void);
      void first(void);
      void list(void);
      
    };
    //------------------------------------
    
    
    
    class store: public books{
    	public:
    	void save(void);
    	void load(void);
    };
    
        char name[12]="peter minns";
    	
    //------------------------------------
    int main(int) {
    int x=1;
      books bdata; 
      store storage;
      do{
      storage.first();
      
      return(0);
      }while(x=1);
      };
      
    //------------------------------------
    
    void books::first(void) {
    //do {
    system("cls");
    
    cout<<"***********************************************************\n";
    cout<<"***********************************************************\n";
    cout<<"*****************Welcome to the Book***********************\n";
    cout<<"***********************Database****************************\n";
    cout<<"***********************************************************\n";
    cout<<"***********************************************************\n\n\n\n";
    
    cout<<" What would you like to do? \n";
    cout<<" Select:\n\n";
    cout<<" A			To add a book to the database\n";
    cout<<" F			To search for a book\n";
    cout<<" R			To remove a book\n";
    cout<<" C			Clear book database\n";
    cout<<" S			Show all books\n";
    cout<<" Q			Quit program\n";
    choice=getchar();
    choice=toupper(choice);
    
    switch(choice) {
    
    	case 'A': {
    		cout<<"you selected 'Add'\n";
    	
    		enter(i);
    		
    		break;}
    		
    	case 'F': {
    		cout<<"you selected 'Search'\n";
    		break;}
    		
    	case 'R': {
    		cout<<"you selected 'Remove'\n";
    		break;}	   
    		
    	
    	case 'C': {
    		cout<<"you selected 'Clear'\n";
    		break;}
    		
    	case 'S': {
    		cout<<"you selected 'show all'\n";
    		list();
    		break;}
    		
    
    
    	case 'Q': {
    		cout<<"you selected 'Quit'\n";
    		break;}
    
    	
    };
    //}while(choice!='Q','A');
    }; 
      
    //---------------------------------------
    //member functions
    		void books::enter(int i) {
    		
    			cout<<"Enter the book title: ";
    			cin>>(np[i].title);
    			cout<<"Enter the author: ";
    			cin>>(np[i].author);
    			cout<<"Enter the ISBN number: ";
    			cin>>(np[i].isbn);
    			cout<<"Enter the publisher name: ";
    			cin>>(np[i].publishername);
    			cout<<"Is there a CDROM included (yes or no): ";
    			cin>>(np[i].cdrom);
    			cout<<"Year published: ";
    			cin>>(np[i].yearofpub);
    			cout<<"Enter price - exclude  sign: ";
    			cin>>(np[i].price);
    			
    			cout<<"\n\n\n information added to database\n\n\n";
    			system ("cls"); 
    			main(0);
    };
    
    void books::list(void) {
    int i;
    int ch;
    for (i=0; i<2; i++) {
    	cout<<"title "<<np[i].title<<endl;}
    	
    
      
    	main(0);
    	//cout<<"name "<<np[i].name<<	  
    }
    the problem is in the enter function. After the information has been entered the program calls main(int) again but then breaks from the program. Anyone know how i can stop this?

    Code:
    //---------------------------------------
    //member functions
    		void books::enter(int i) {
    		
    			cout<<"Enter the book title: ";
    			cin>>(np[i].title);
    			cout<<"Enter the author: ";
    			cin>>(np[i].author);
    			cout<<"Enter the ISBN number: ";
    			cin>>(np[i].isbn);
    			cout<<"Enter the publisher name: ";
    			cin>>(np[i].publishername);
    			cout<<"Is there a CDROM included (yes or no): ";
    			cin>>(np[i].cdrom);
    			cout<<"Year published: ";
    			cin>>(np[i].yearofpub);
    			cout<<"Enter price - exclude  sign: ";
    			cin>>(np[i].price);
    			
    			cout<<"\n\n\n information added to database\n\n\n";
    			system ("cls"); 
    			main(0);
    };
    I want to be able to access the menu system again after every function.

    Cheers

    Andy

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You don't call main(). When a subprogram finishes, it returns control to its caller up to main() and eventually main() returns to OS.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    Oh i see. I didnt call it earlier but i still had the same problem so i will change that.

    But i need main to call first() every time a sub program finishes. Can i do that with my loop?

    Cheers for the help

    Andy

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You got some design issue, but for now...
    Code:
    int main() 
    {
        char choice;
        books bdata; 
        store storage;
      
        do {
            storage.first(choice);
        } while(toupper(choice) != 'Q');
    };
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM