getting a function to return to main??

This is a discussion on getting a function to return to main?? within the C++ Programming forums, part of the General Programming Boards category; how do i get my fuctions to return back to main. when it runs my function ends e.g. the member_list ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    15

    getting a function to return to main??

    how do i get my fuctions to return back to main. when it runs my function ends e.g. the member_list function after operation i want it to go back to main because thats where my menu is the program is supposed to finsih when the user presses 5.

    can anyone tell me then how i get my function to return to main.

    ive tried putting the menu outside main but i get errors

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    #include <fstream.h>
    
    const int maxmembers = 15;
    const int maxbooks = 30;
    
    struct cust
    {
      string surname;
      string house_no;
      string road;
      string ID;
      int books_borrowed;
    };
    
    struct books
    {
      string author;
      string title;
      string category;
      string ISBN;
      int loaned;
    }; 
    
    int member_list(cust c[])
    {
     for(int i=0; i< maxmembers;i++)
        {
      cout<< "Surname\t\tHouse No\tRoad\t\tID" << endl;
      cout << c[i].surname;
      cout << c[i].house_no;
      cout << c[i].road;
      cout << c[i].ID << endl;
        }
    }
    
    int book_list(books b[])
    {
     for(int i=0; i< maxbooks;i++)
        {
      cout<< "Author\t\t\tTitle\t\t\tCategory\tISBN" << endl;
      cout << b[i].author ;
      cout << b[i].title ;
      cout << b[i].category;
      cout << b[i].ISBN << endl;
        }
    }
    
    void issue_book(books b [], cust c[])
    {
      string  member = 0;
      string ISBN = 0;
      cout << "Please enter ISBN number of book: " << endl;
      cin >> ISBN ;
      cout << "Please enter member ID: " << endl;
      cin >> member;
      for(int i=0; i < maxbooks; i++)
        {
          if (b[i].ISBN == ISBN)
    	{
    	  b[i].loaned ++;
    	}
        }
    
      for(int i=0; i < maxmembers; i++)
        {
          if(c[i].ID == member)
    	{
    	  c[i].books_borrowed ++;
    	}
        }
      //save_book(b);
      //save_user(c);
    }
      
    void return_book(books b[], cust c[])
    {
      string member = 0;
      string ISBN = 0;
      cout << "Please enter ISBN number of book: " << endl;
      cin >> ISBN ;
      cout << "Please enter member ID: " << endl;
      cin >> member;
      for(int i=0; i < maxbooks; i++)
        {
          if (b[i].ISBN == ISBN)
    	{
    	  b[i].loaned --;
    	}
        }
      for(int i=0; i < maxmembers; i++)
        {
          if(c[i].ID == member)
    	{
    	  c[i].books_borrowed --;
    	}
        }
      //save_book();
      //save_user();
      
    }
    
    /*void save_book(books b[])
    {
    ofstream book_data;
    
     if (book_data.is_open())
       {
    for(int i=0; i < maxbooks;i++)
      {
      book_data << b[i].author;
      book_data << ";";
      book_data << b[i].title;
      book_data << ";";
      book_data << b[i].category;
      book_data << ";";
      book_data << b[i].ISBN;
      book_data << ";";
      book_data << b[i].loaned;
      book_data << ";";
      }
     book_data.close();
       }
    }*/
    
    
    /*void save_user(cust c[])
    {
    ofstream user_data;
    
    if (user_data.is_open())
      {
        for(int i=0; i < maxmembers;i++)
          {
     user_data << c[i].surname;
     user_data << ";";
     user_data << c[i].house_no;
     user_data << ";";
     user_data << c[i].road;
     user_data << ";";
     user_data << c[i].ID;
     user_data << ";";
     user_data << c[i].books_borrowed;
     user_data << ";";
      }
    	user_data.close();
    	{
    }*/
    
    int main()
    {
    
    //variables for type people and books
      books book[maxbooks];
      cust user[maxmembers];
    
      char uTest[1000];
      int cRead = 0;
      int nTest = 100;
    
      ifstream user_data;
      user_data.open("userdata.txt",ios::in);
    
    do
      {
        user_data.getline(uTest,nTest,';');
        user[cRead].surname = uTest;
        user_data.getline(uTest,nTest,';');
        user[cRead].house_no = uTest;
        user_data.getline(uTest,nTest,';');
        user[cRead].road = uTest;
        user_data.getline(uTest,nTest,'\n');
        user[cRead].ID = uTest;
    
        if (user_data)
          {  int nTest[100];
    	cRead++;
          }
    
      }while (user_data && cRead < maxmembers);
    
      user_data.close();
    
      ifstream book_data;
      book_data.open("bookdata.txt",ios::in);
    
    do
      {
        book_data.getline(uTest,nTest,';');
        book[cRead].author = uTest;
        book_data.getline(uTest,nTest,';');
        book[cRead].title = uTest;
        book_data.getline(uTest,nTest,';');
        book[cRead].category = uTest;
        book_data.getline(uTest,nTest,'\n');
        book[cRead].ISBN = uTest;
    
        if (book_data)
          {
    	cRead++;
          }
    
      }while (book_data && cRead < maxbooks);
    
      book_data.close();
    
      int choice;
      cout <<"Library Menu" << endl << endl ;
      cout <<"[1] List Library Members and Member Information" << endl;
      cout <<"[2] List Library Books and Book Information" << endl ;
      cout <<"[3] Issue Library Book" << endl ;
      cout <<"[4] Return Library Book" << endl ;
      cout <<"[5] Exit Library System" << endl ;
      cout <<"Enter Your Required Choice" << endl;
      cin >> choice;
      
      switch(choice)
        {
        case 1:
          member_list(user);
          break;
        case 2:
          book_list(book);
          break;
        case 3:
          issue_book(book,user);
          break;
        case 4:
          return_book(book,user);
          break;
        case 5:
          exit(1);
          break;
        default:
          cout << "This is not an option" << endl;
          break;
        
        }
    }

  2. #2
    Shadow12345
    Guest
    ...the program always 'returns' to main after it returns its value

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    15
    not in this program it ends. when i run either member_list or book_list it lists the books then ends.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    ... the program always returns to it's caller....

    Code:
    #include <iostream>
    
    void menu()
    {
       int choice;
    
       do
       {
          cout <<"Library Menu" << endl << endl ;
          cout <<"[1] List Library Members and Member Information" << endl;
          cout <<"[2] List Library Books and Book Information" << endl ;
          cout <<"[3] Issue Library Book" << endl ;
          cout <<"[4] Return Library Book" << endl ;
          cout <<"[5] Exit Library System" << endl ;
          cout <<"Enter Your Required Choice" << endl;
          cin >> choice;
    
          switch(choice)
            {
            case 1:
              member_list(user);
              break;
            case 2:
              book_list(book);
              break;
            case 3:
              issue_book(book,user);
              break;
            case 4:
              return_book(book,user);
              break;
            case 5:   // just break; at the end of this function 
              break;  // the program will return to main (the caller)
            default:
              cout << "This is not an option" << endl;
              break;
            }
       } while(choice != 5);
    }
    
    int main(void)
    {
       menu();
       return 0;
    }
    Last edited by Monster; 12-16-2002 at 09:35 AM.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    15
    thanks for your help mate just another question really

    ive done what you say and the program doesnt end but it doesnt return to my menu any suggestions ??

    ive tried to call menu in the functions like this but i get 1 error mesage

    void member_list(cust c[])
    {
    for(int i=0; i< maxmembers;i++)
    {
    cout<< "Surname\t\tHouse No\tRoad\t\tID" << endl;
    cout << c[i].surname;
    cout << c[i].house_no;
    cout << c[i].road;
    cout << c[i].ID << endl;
    }
    menu();
    }

    158 meerkat.cscs.wmin.ac.uk% library.cc: In function `void member_list(cust *)':
    library.cc:37: implicit declaration of function `int menu(...)'

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Don't call menu in the functions. Function menu calls member_list, function member_list calls menu, etc, etc. This is wrong.

    Just put a while loop in main:
    Code:
    int menu()
    {
       int choice;
       ...
       return choice;
    }
    
    int main(void)
    {
       int choice;
    
       do {
          choice = menu();
       while(choice != 5) ;
    
       return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    15
    thanks dude it works

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    158 meerkat.cscs.wmin.ac.uk% library.cc: In function `void member_list(cust *)':
    library.cc:37: implicit declaration of function `int menu(...)'


    This is because your menu() function is after the member_list() function. It's best to make use of function prototypes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21