C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 16th, 2002, 09:15 AM
Neildadon Neildadon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 12 Neildadon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>

Reply With Quote
  #2  
Old December 16th, 2002, 10:22 AM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old December 16th, 2002, 01:41 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > getting my functions to return to main after they are used


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway