Thread: Need Help with 2D array (Library System)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15

    Question Need Help with 2D array (Library System)

    Hey Guys, I need assistance with my code because everytime it skips the part to input. Maybe someone could help me with my code. here it is.

    Code:
    int main()
    {
        int index,choice,choiceloop,row=0,col=0,main=0,stop;
        char bookstore[20][50],loopchoice,booktitle[50];
        do
        {
        system("cls");
        cout<<"Welcome to MMSU's Library System"<<endl;
        cout<<"Press 1 to Add Books"<<endl;
        cout<<"Press 2 to Display Books"<<endl;
        cout<<"Press 3 to Search Books"<<endl;
        cout<<"Press 4 to Edit Books"<<endl;
        cout<<"Press 0 to exit the System"<<endl;
        cout<<endl<<"Choice: ";
        cin>>choice;
        if(choice==1)
        {
            system("cls");
            cout<<"ADDING BOOK - MMSU Library System"<<endl;
            row=0;
            do
            {
                cout<<endl<<"Title: ";
                fgets(bookstore[row],50,stdin);
                //for(row=0;row<20;row++)
                //    for(col=0;col<50;col++)
                //        booktitle[col]=bookstore[row][col];
                cout<<endl<<"Do you want to enter again(Y/N): ";
                cin>>loopchoice;
                if(loopchoice=='y'||loopchoice=='Y')
                {
                    choiceloop=1;
                    row++;
                }    
                else if(loopchoice=='n'||loopchoice=='N')
                {
                    choiceloop=0;
                    main=1;
                }    
            }
            while(choiceloop==1);
    I know its a long read but I'd be grateful if you could help. Thanks in Advance Guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't mix C-style input with C++-style I/O. This:
    Code:
    fgets(bookstore[row],50,stdin);
    should be:
    Code:
    cin.getline(bookstore[row], 50);
    Anyway, the problem is that this line:
    Code:
    cin>>choice;
    leaves the newline from entering the input in the input buffer. You could use cin.ignore() to remove that newline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15

    Problem 2

    Thanks sir Laserlight, I have already made that work using cin.getline. Now i have a new problem.
    Code:
    else if(choice==3)
        {
            system("cls");
            cout<<"SEARCH BOOK - MMSU Library System"<<endl;
            do
            {
                cin.ignore();
                cout<<endl<<"Find Book Title: ";
                cin.getline(booktitle,50);
                for(row=0;row<20;row++)
                {
                    for(col=0;col<50;col++)
                    {
                        if(booktitle[col]==bookstore[row][col])
                            continue;
                            
                        else if(booktitle[col]!=bookstore[row][col])
                        {
                            stop=1;
                            cout<<"No Book Found";
                            break;
                        }
                    }
                    if(stop=1)
                        break;
                }    
                cout<<endl<<"Do you want to borrow book/s again(Y/N): "<<endl;
                cin>>loopchoice;
                if(loopchoice=='y'||loopchoice=='Y')
                    choiceloop=1;
                else if(loopchoice=='n'||loopchoice=='N')
                {
                    choiceloop=0;
                    main=1;
                }
            }
            while(choiceloop==1);
    In this code the program has to search for the book in the array.

    For Example:
    I chose 1 to add book Title: How I Met Your Mother/how i met your mother (How do I configure it to check for both lower case and upper case?)

    then I chose 3 to search Book Title.
    I enter title: How I Met Your Mother
    but it returns No Book Found.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, you are just asking how to check for both upper and lower case? One solution is to convert the strings to be lowercase only for searching.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15

    Problem 3

    I have already gone past the 2nd problem, Just used toupper now I have a third problem and hopefully the last one.
    Code:
    else if(choice==2)
        {
            system("cls");
            cout<<"DISPLAY BOOK - MMSU Library System"<<endl;
            cout<<"\tBook Index\t\tTitle"<<endl;
            for(row=0;row<20;row++)
                cout<<"\t"<<row<<"\t\t"<<bookstore[row]<<endl;
            main=1;
            system("pause");
        }
    The problem is that the program only has to display the row with values in it. my program displays 0-19 with trash values. how do I do it?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Keep a count of how many rows are in use, then loop until that point instead of always looping to 20.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Philippines
    Posts
    15
    Done! That was the last problem. Thank you sir laserlight! you are a very helpful person. Thank you so much!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're welcome. Good to know I've been knighted
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    You're welcome. Good to know I've been knighted
    Shouldn't your title be "Dame" though?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please use std::string instead of char arrays. Then, use std::getline instead of std::cin.getline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with library system
    By hexarthrius in forum C Programming
    Replies: 20
    Last Post: 03-12-2011, 04:23 AM
  2. Can i copy the output of system() to an array?
    By xenanovich in forum Linux Programming
    Replies: 9
    Last Post: 05-26-2010, 12:25 AM
  3. Replies: 1
    Last Post: 08-16-2009, 05:49 AM
  4. Matrix / 2D Array Templated Library
    By CaeZaR in forum C++ Programming
    Replies: 1
    Last Post: 02-08-2006, 08:02 PM
  5. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM

Tags for this Thread