|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
getting my functions to return to main after they are used
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; } } </code> |
|
#2
|
||||
|
||||
|
You'll need to create a loop to achieve this. Do something like this:
Code:
...
int choice = 0;
int done = 0;
while (done == 0) {
cout <<"Library Menu" << endl << endl ;
...
case 5: done = 1;
...
}
BTW, main should return an int, so be sure to return a "0" for success or a "1" for error, or use stdlib.h and return EXIT_SUCCESS or EXIT_FAILURE. |
|
#3
|
|||
|
|||
|
Neildadon,
vpopper's recommendation to put the function call into a loop is correct. You should be aware though that functions always return to exactly the point they were called from. There's no (good) way to make them return to somewhere else. The goal is always to make sure that your program will do what you want it to after that function returns.
__________________
Clay Dowling Lazarus Notes Articles and commentary on web development http://www.lazarusid.com/notes/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > getting my functions to return to main after they are used |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|