Searching a two dimensional array

Pages: 12
bnbertha - I moved the char binarySearch(char[][], char, char); to the mainmenu.h file. As you can see, I was trying to match the char [] and the char [][], so I changed the char [] to char [][], but I don't think that's right. I get the E2453 Size of the type 'char[]' is unknown or zero. I was really just guessing on what your were trying to tell me, but I just don't get it.

guestgulkan - Thanks for all the help! I'm going to print your part and go over my code with it! I'll let you know how it goes!
guestgulkan - I'm going to go thru the code with your suggestions, but I have a question. Why would you not hard code the array size? Is there a scenario when you would want to hard code the array size?

I'm just trying to understand when you would want to do a hard code vs. not.

Oh, and what is the limits file? We haven't came across that in class yet.

And again, just curious, why change the loops from do/while to while loops? Just trying to understand everything. Thanks again for everything!
Last edited on
The day will come come when you are writing/taking part in code projects that are thousands of lines long and spread over multiple files - if you hard code values like that, then if that value needs to be changed it will be a pain to find all those places in all those lines in all those files to change them (and your fellow programmers will not be happy with you).


I only included the limits files for this part cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Last edited on
Ok, I understand the hardcoded part. Now, can you tell me what the cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); actually does or means? I haven't seen anything like that before.

I really do appreciate all the help and hope you don't mind me asking so many questions (cause I have a few more....lol)

Why change the case #'s from literals to integers? I believe our book showed them as literal's so that's why I wrote them that way.

Can you explain the cin.getline(&bookTitle[bookResult][0],TITLE_LENGTH); to me?

Why was the cout << endl; entered in a couple of different functions? Does it place a blank line there? Can you do that instead of just putting spaces between the quotations?

Again, thanks for the help.
guestgulkan - I really do appreciate the help. Do you have time to go over the display the array part that was commented out?
Soory I thought it was all done and dusted...
Do you mean the "Displaythe array" part ??
Well - we could do a deletebook function something like this:
(note I've not checked whether the book is already deleted or not set).
I deliberately ask the user for a number starting at 1 but change it to start from 0 when accessing the array.
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
void deleteBook()
{
    int  bookNum;
    char c;
    cout << "You selected Delete Book." << endl;
    cout << "Please enter a book number  (between 1 and " << MAX_ENTRIES << ")"<< endl; //start from 1 rather than 0 - might easier for the user
    cin >> bookNum;
    if (bookNum < 1 || bookNum > MAX_ENTRIES) 
    {
        cout << "Book number out of range" << endl;
        return;
    }

    cout << "Do you really want to delete book " << bookNum  << "? Y/N";
    cin >> c;
    c =toupper(c);
    switch (c)
    {
    case 'Y':
        bookTitle[bookNum-1][0]=0; //change range to start from 0. Signal delete by setting first char of book title to zero 
        cout << "Book " << bookNum << "successfully deleted" << endl;
        break;

    case 'N':
        cout << "Book deletion cancelled" << endl;
        break;
    default:
        cout << "Incorrect input - Book deletion cancelled" << endl;
        break;
    }

    return;
}


The lookupBook and displayBookInfo functions could look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void lookUpBook()
{
    int  bookNum;
    cout << "Serendipity Booksellers" << endl;
    cout << "Lookup Book." << endl;
    cout << "Please enter a book number(between 1 and " << MAX_ENTRIES << ")"<< endl;
    cin >> bookNum;
    if (bookNum < 1 || bookNum > MAX_ENTRIES)
    {
        cout << "**Book number out of range**" << endl;
        return;
    }

    displayBookInfo(bookNum-1); //change to zeros base 

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void  displayBookInfo(int bookNum)
{
        cout << endl;
		cout << "Serendipity Booksellers" << endl;
        cout << "Book Information" << endl;
        cout << endl;
        if(!bookTitle[bookNum][0])
        {
            cout << "**Book slot is empty**" << endl << endl;
            return;
        }
 
        cout << setw(18) << right << "TITLE: " << &bookTitle[bookNum][0] << endl;
        cout << setw(18) << right <<"ISBN: " << &isbn[bookNum][0] << endl;
        cout << setw(18) << right <<"AUTHOR: " << &author[bookNum][0] << endl;
        cout << setw(18) << right <<"PUBLISHER: " << &publisher[bookNum][0] << endl;
        cout << setw(18) << right <<"DATE ADDED: " << &dateAdded[bookNum][0] << endl;
        cout << setw(18) << right <<"QTY ON HAND: " << qtyOnHand[bookNum] << endl;
        cout << setw(18) << right <<"WHOLESALE PRICE: " << "$" << setprecision(2) << fixed << wholesale[bookNum] << endl;
        cout << setw(18) << right <<"RETAIL PRICE: " << "$" << retail[bookNum] << endl << endl;

}


Thanks! I just skimmed it right now, but looks good to me. I'm doing some re-reading on all the functions/arrays/pointers and some of the tutorial's on this site right now. Its helped me correct a few errors I had. There is a lot to this, and if you don't understand something, you can't move forward. I really respect all of you who know this stuff and appreciate all the help you guys have provided. I was really going crazy before I found this board!
Did you manage to sort out the passing char [][] and the prototypes?
Yes....that error is gone....thanks!
Guys!
How about a two-dimensional program where you must input the number of rows and columns and add it all... got any ideas?
Last edited on
Topic archived. No new replies allowed.
Pages: 12