How to return the values in a function

I am trying to write code that will take the users input (book Title) & search for it. If the book is found, I would like to return (display) the values in the bookInfo function for that book. Suggestions on how to do that? Here is my .h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int searchList();
int binarySearch();
int cashier();
int invMenu();
int bookinfo();
int reports();
void addBook();
int lookUpBook();
int bookResult();
void editBook();
void deleteBook();
void repListing();
void repWholesale();
void repRetail();
void repQty();
void repCost();
void repAge();
//void DisplayTheArray ( int [][]); To be displayed later 


And here is the start of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//This program is to display the main menu screen
#include <iostream>
#include "mainmenu.h"
#include <iomanip>
#include <limits>

using namespace std;

const int MAX_ENTRIES = 20;
const int TITLE_LENGTH = 51;
const int ISBN_LENGTH = 14;
const int AUTHOR_LENGTH = 31;
const int PUB_LENGTH = 31;
const int DATE_LENGTH = 11;

char bookTitle[MAX_ENTRIES][TITLE_LENGTH]={0};
char ISBN[MAX_ENTRIES][ISBN_LENGTH]={0};
char author[MAX_ENTRIES][AUTHOR_LENGTH]={0};
char publisher[MAX_ENTRIES][PUB_LENGTH]={0};

char dateAdded[MAX_ENTRIES][DATE_LENGTH];
int qtyOnHand[MAX_ENTRIES];
double wholesale[MAX_ENTRIES];
double retail[MAX_ENTRIES];

char titleBook;
char titleResults;



int main()
{
        int choice;           //Menu choice

        while (true)
        {
        //Display the menu and get a choice
                cout << endl;
        cout << "Serendipity Booksellers" << endl;
        cout << "Main Menu" << endl;
        cout << "                       " << endl;
        cout << "1. Cashier Module" << endl;
        cout << "2. Inventory Database Module" << endl;
        cout << "3. Report Module" << endl;
        cout << "4. Exit" << endl;
        cout << "                        " << endl;
        cout << "Enter your choice: " << endl;
        cin >> choice;

        //Validate and process the menu choice.
        while (choice < 1 || choice > 4)
        {
                cout << "Please enter a number in the range 1-4." << endl;
                cin >> choice;
        }

        if (choice != 4)
        switch (choice)
        {
                case 1: cashier();
                          break;
                case 2: invMenu();
                          break;
                case 3: reports();
                          break;
                case 4: cout << "You selected item 4." << endl;
                          break;
        }

        } while (choice != 4);
        return 0;
}
//This function is to display the cashier screen

int cashier()
{
        char date[8];   //Defining variables to be requested from user
        int quantity;
        char ISBN;
        char title;
        float price;
        float subtotal;  //Defining variables to be used to show information to user
        float tax;
        float total;
        char again;     //To hold Y or N input

        do
        {
        //Getting variables defined above from user
        cin >> setw(8) >> date;
        cin >> quantity;
        cin >> ISBN;
        cin >> title;
        cin >> price;

        //Calculate the total amount
        subtotal = quantity * price;
        tax = subtotal * 0.06;
        total = subtotal + tax;

        cout << "Serendipity Booksellers" << endl;
        cout << "                       " << endl;
        cout << "Date: " << endl;
        cout << "                       " << endl;
        cout << "Qty   ISBN        Title         Price  Total " << endl;
        cout << "_____________________________________________" << endl;
        cout << "                                             " << endl;
        cout << setprecision(2) << fixed;
        cout << "Subtotal: " << setw(6) << subtotal << endl;
        cout << "Tax: " << setw(6) << tax << endl;
        cout << "Total: " << setw(6) << total << endl;
        cout << "                              " << endl;
        cout << "Thank You for Shopping at Serendipity! " << endl;

        //Does the user want to process another transaction?
        cout << "Do you want to process another transaction? (Y/N) " << endl;
        cin >> again;
        } while (again == 'Y' || again == 'y');
        return 0;
}
//This function is to display the inventory screen

int invMenu()
{
        int choice;           //Menu choice

        while (true)
        {
        //Display the menu and get a choice
                cout << endl;
        cout << "Serendipity Booksellers" << endl;
        cout << "Inventory Database " << endl;
        cout << "                   " << endl;
        cout << "1. Look Up a Book " << endl;
        cout << "2. Add a Book " << endl;
        cout << "3. Edit a Book's Record " << endl;
        cout << "4. Delete a Book " << endl;
        cout << "5. Return to the Main Menu " << endl;
        cout << "                           " << endl;
        cout << "Enter your choice: " << endl;
        cin >> choice;
        switch (choice)
        {
                case 1: void lookUpbook();
                          break;
                case 2: addBook();
                          break;
                case 3: editBook();
                          break;
                case 4: deleteBook();
                          break;
                case 5: cout << "You selected item 5." << endl;
                          break;
        }

        //Validate and process the menu choice.
        while (choice < 1 || choice > 5)
        {
                cout << "Please enter a number in the range 1-5." << endl;
                cin >> choice;
        }
        } while (choice != 5);
        return 0;
}

//Add stub functions for inventory database

int lookUpBook()
{
	//Get the title of the book to search for
        cout << "Enter the title of the book you wish to search for. " << endl;
        cin >> titleBook;

	//Search for the title of the book
	int titleResults = binarySearch();

	//If titleResults contain -1 the book was not found.
        if (titleResults == -1)
                cout << "That book was not found." << endl;
        else
                // //Otherwise titleResults contains the searched book
                return int bookInfo;;
}
Last edited on
And the rest of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
void addBook()
{
        //Search addbook array for null terminator
        int bookResult = searchList();

        //If searchList returned -1, then array was full
        if (bookResult == -1)
                cout << "No more books may be added to the inventory." << endl;
        else
        {
                //Otherwise results contain an empty row

        cin.getline(std::numeric_limits<std::streamsize>::max(), '\n');

                cout << "Please enter the Book Title " << endl;
                cin.getline(&bookTitle[bookResult][0],TITLE_LENGTH);

                cout << "Please enter the ISBN number " << endl;
                cin.getline(&isbn[bookResult][0],ISBN_LENGTH);

                cout << "Please enter the Author's name " << endl;
                cin.getline(&author[bookResult][0],AUTHOR,LENGTH);

                cout << "Please enter the Publisher's name " << endl;
                cin.getline(&publisher[bookResult][0],PUB_LENGTH);

                cout << "Please enter the date the book is added to the ";
                cout << "inventory " << endl;
                cin.getline(&dateAdded[bookResult][0],DATE_LENGTH);

                cout << "Please enter the quanity of the book " << endl;
                cin >> qtyOnHand[bookResult];

                cout << "Please enter the wholesale price of the book " << endl;
                cin >> wholesale[bookResult];

                cout << "Please enter the retail price of the book " << endl;
                cin >> retail[bookResult];
        }
}

void editBook()
{
        char titleResults;

        //Function Prototype
        char binarySearch(char [], char, char);
        const char SIZE = 51;

        //Get the title of the book to search for
        cout << "Enter the title of the book you wish to search for. " << endl;
        cin >> titleBook;

        //Search for the title of the book
        titleResults = binarySearch(bookTitle, SIZE, titleBook);

        //If titleResults contain -1 the book was not found.
        if (titleResults == -1)
                cout << "That book was not found." << endl;
        else
                //Otherwise titleResults contains the searched book
                return int bookInfo;

}

void deleteBook()
{
        cout << "You selected Delete Book." << endl;
}



//This function is to display the book info screen

int bookInfo()
{
        cout << endl;

        cout << "Serendipity Booksellers" << endl;
        cout << "Book Information " << endl;
        cout << "                  " << endl
        cout << "ISBN: " << endl;
                //DisplayTheArray(isbn);
        cout << "Title: " << endl;
                //DisplayTheArray(bookTitle);
        cout << "Author: " << endl;
                //DisplayTheArray(author);
        cout << "Publisher: " << endl;
                //DisplayTheArray(publisher);
        cout << "Date Added: " << endl;
                //DisplayTheArray(dateAdded);
        cout << "Quantity-On-Hand: " << endl;
                //DisplayTheArray(qtyOnHand);
        cout << "Wholesale Cost: " << endl;
                //DisplayTheArray(wholesale);
        cout << "Retail Price: " << endl;
                //DisplayTheArray(retail);
        return 0;
}


//This function is to display the reports screen

int reports()
{
        int choice;           //Menu choice

        do
        {
        //Display the menu and get a choice
                cout << endl;
        cout << "Serendipity Booksellers" << endl;
        cout << "Reports " << endl;

        cout << "1. Inventory Listing " << endl;
        cout << "2. Inventory Wholesale Value " << endl;
        cout << "3. Inventory Retail Value " << endl;
        cout << "4. Listing by Quantity " << endl;
        cout << "5. Listing by Cost " << endl;
        cout << "6. Listing by Age " << endl;
        cout << "7. Return to the Main Menu " << endl;
        cout << "                              " << endl;
        cout << "Enter your choice: " << endl;
        cin >> choice;
        switch (choice)
        {
                case 1: void repListing();
                          break;
                case 2: void repWholesale();
                          break;
                case 3: void repRetail();
                          break;
                case 4: void repQty();
                          break;
                case 5: void repCost();
                          break;
                case 6: void repAge();
                          break;
                case 7: cout << "You selected item 7." << endl;
                          break;
        }


        //Validate and process the menu choice.
        while (choice < 1 || choice > 7)
        {
                cout << "Please enter a number in the range 1-7." << endl;
                cin >> choice;
        }
        } while (choice != 7);
        return 0;
}

//Add stub functions for reports menu

        void repListing()
        {
                cout << "You selected Inventory Listing." << endl;
        }

        void repWholesale()
        {
                cout << "You selected Inventory Wholesale Value." << endl;
        }

        void repRetail()
        {
                cout << "You selected Inventory Retail Value." << endl;
        }

        void repQty()
        {
                cout << "You selected Listing By Quantity." << endl;
        }

        void repCost()
        {
                cout << "You selected Listing By Cost." << endl;
        }

        void repAge()
        {
                cout << "You selected Listing By Age." << endl;
        }

//*********************************************************
//This function is to search the bookTitle for an empty row
//*********************************************************

int searchList()
{
        int index = 0;
        int position = -1;

        while (index < MAX_ENTRIES)
        {
                if (bookTitle[index][0] == [0])
                {
                        position = index;
                }
                index++;
        }
        return position;
}

//**********************************************************
//This function is to search the bookTitle array for a value
//**********************************************************

int binarySearch()
{
        char first = A,
        char last = numelems - 1,
        char middle,
        char position = -1;
        book found = false;

        while (!found && first <= last)
        {
                middle = (first + last) / 2;
                if (array[middle] == value)
                {
                        found = true;
                        position = middle;
                }
                else if (array[middle] > value)
                        last = middle -1;
                else
                        first = middle +1;
        }
        return position;
}
This may or may not make sense/help. I tried. I'm positive there are things wrong with it but it's up to you to make it work.

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

void SearchnDisplay(char Searchfor[])
{
       bool doesexist = false;
       for(int i = 0; i < MAXENTRIES; i++)
       {
            for(int j = 0; j < TITLELENGTH; j++)
           {
                  if(Searchfor[j] != bookTitle[i][j])
                  {
                         doesexist = false;
                         break;
                  }else{
                        doesexist = true;
                        continue;
                  }
            }
            if(doesexist)
            {
                   Print all your stuff here using i as the index of your arrays.
                   break;
            } 
      }
}


May I suggest that you shouldn't post you're entire code. I was tempted to not even try to answer you're question because I wasn't sure how hard it was going to be to find what was relevant. Try to figure out what is relevant to your question and only post that. I think seeing over 400 lines of code may scare away some people that may have been able to answer your question.

EDIT: Well now that I reread your title and post I'm not exactly sure what you're asking to be honest. If I'm totally off , sorry, maybe you could be more specific about what you want to do.
Last edited on
I almost just posted what I thought was relevant, but the first time I did that, some asked for all of it....so I thought it was better to post all of it...

As for what I'm trying to do, is to use the lookup function to find a book (that's stored in an array), and if the book is found, display the information for that book (which is stored in the bookInfo function)...I need help with the search part and how to return the information....

Here is the lookUp function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int lookUpBook()
{
	//Get the title of the book to search for
        cout << "Enter the title of the book you wish to search for. " << endl;
        cin >> titleBook;

	//Search for the title of the book
	int titleResults = binarySearch();

	//If titleResults contain -1 the book was not found.
        if (titleResults == -1)
                cout << "That book was not found." << endl;
        else
                // //Otherwise titleResults contains the searched book
                return int bookInfo;;
}


Here is the bookInfo code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int bookInfo()
{
        cout << endl;

        cout << "Serendipity Booksellers" << endl;
        cout << "Book Information " << endl;
        cout << "                  " << endl
        cout << "ISBN: " << endl;
                //DisplayTheArray(isbn);
        cout << "Title: " << endl;
                //DisplayTheArray(bookTitle);
        cout << "Author: " << endl;
                //DisplayTheArray(author);
        cout << "Publisher: " << endl;
                //DisplayTheArray(publisher);
        cout << "Date Added: " << endl;
                //DisplayTheArray(dateAdded);
        cout << "Quantity-On-Hand: " << endl;
                //DisplayTheArray(qtyOnHand);
        cout << "Wholesale Cost: " << endl;
                //DisplayTheArray(wholesale);
        cout << "Retail Price: " << endl;
                //DisplayTheArray(retail);
        return 0;
}


And here is the search code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int binarySearch()
{
        char first = A,
        char last = numelems - 1,
        char middle,
        char position = -1;
        book found = false;

        while (!found && first <= last)
        {
                middle = (first + last) / 2;
                if (array[middle] == value)
                {
                        found = true;
                        position = middle;
                }
                else if (array[middle] > value)
                        last = middle -1;
                else
                        first = middle +1;
        }
        return position;
}


I do appreciate the help and may ask a lot of dumb questions...I'm just trying to get an understanding of this....
All you need to do is read up on how to use function arguments.

In your case, your functions will need to use either pointer or reference arguments so data obtained by the functions will be accessible by the funcition that called it.
Thanks for the info....I will do that....We haven't hit pointers yet, so I guess I'll need to concentrate on the reference arguments....
I would keep display and search functions separate.
For searching, just return the index into the array of the found book. If no book was found, return -1.

int lookUpBook( ... )

Hope this helps. (If that was what you are asking about.)
Last edited on
Topic archived. No new replies allowed.