Like Tree2Likes
  • 2 Post By laserlight

Need Help with 2D array (Library System)

This is a discussion on Need Help with 2D array (Library System) within the C++ Programming forums, part of the General Programming Boards category; Hey Guys, I need assistance with my code because everytime it skips the part to input. Maybe someone could help ...

  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
    21,311
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,311
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,311
    Keep a count of how many rows are in use, then loop until that point instead of always looping to 20.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,311
    You're welcome. Good to know I've been knighted
    Salem and Matticus like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,169
    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%.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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, 03: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, 07: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


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