Unexplained program crashing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    Unexplained program crashing

    Ok, so I've finished the "library" problem. The program takes in input and sorts the information. However the program crashes if I enter an authors name beginning with the letter z. Also if the book starts with the letter Z the program crashes. Everything else works perfectly.....e xcept for using the letter Z.

    Any ideas?

    Code:
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    struct book
    {
    	char title[25],author[25];
    	int ISBN,pages,year;
    };
    class shelf
    {
    private:
    	book books[50];
    	int num_books;
    public:
    	shelf();
    	~shelf(){};
    	void set_range();
    	void input(char title[]);
    	void output();
    	void lib_output();
    	void single_output(char title[]);
    	char beg_alpha[2];
    	char end_alpha[2];
    	bool full();
    };
    shelf::shelf()
    {
    	int i=0;
    	for(i=0;i<49;++i)
    	{
    		books[i].title[i]='-';
    		books[i].author[i]='-';
    		books[i].ISBN=0;
    		books[i].pages=0;
    		books[i].year=0000;
    	}
    	
    	for (i=0;i<2;++i)
    	{
    		beg_alpha[i]='-';
    		end_alpha[i]='-';
    	}
    	num_books=0;
    };
    bool shelf::full()
    {
    	return (num_books==49);
    }
    void shelf::input(char title[])
    {
    	strcpy (books[num_books].title,title);
    	if (!full())
    	{
    			cout<<"Enter the authors name."<<endl;
    			cin>>books[num_books].author;
    			cout<<"The title of the book is:"<<endl;
    			cout<<"Enter the ISBN of the book."<<endl;
    			cin>>books[num_books].ISBN;
    			cout<<"Enter the number of pages."<<endl;
    			cin>>books[num_books].pages;
    			cout<<"Enter the year the book was published."<<endl;
    			cin>>books[num_books].year;
    			++num_books;
    	}
    	else if (full())
    	cout<<"Shelf is full!"<<endl<<endl;
    }
    void shelf::output()
    {
    	for (int i=0;i<num_books;++i)
    	{
    			cout<<"Author:     "<<books[i].author<<endl;
    			cout<<"Title:      "<<books[i].title<<endl;
    			cout<<"ISBN:       "<<books[i].ISBN<<endl;
    			cout<<"Pages:	    "<<books[i].pages<<endl;
    			cout<<"Published   "<<books[i].year<<endl<<endl;
    	}	
    
    }
    void shelf::single_output(char title[])
    {
    	int i=0;
    	for (i=0;i<num_books;++i)
    	{
    		if(!strcmp(books[i].title,title))
    		{
    			cout<<"Book has been found!"<<endl<<endl;
    			cout<<"Author:     "<<books[i].author<<endl;
    			cout<<"Title:      "<<books[i].title<<endl;
    			cout<<"ISBN:       "<<books[i].ISBN<<endl;
    			cout<<"Pages:	    "<<books[i].pages<<endl;
    			cout<<"Published   "<<books[i].year<<endl<<endl;
    			i=num_books+1;
    		}
    		else
    			cout<<endl<<"No book found!!!."<<endl<<endl;
    	}
    }
    
    
    void main()
    {
    	char ans, title[25]={'-'}, let;
    	int i=0, sel=0;
    	shelf library[25];
    	cout<<"Shelf Setup:"<<endl;
    	for (i=0;i<24;++i)
    	{
    		library[i].beg_alpha[0]=65+i;
    		library[i].beg_alpha[1]='a';
    		library[i].end_alpha[0]=65+i;
    		library[i].end_alpha[1]='z';
    		cout<<library[i].beg_alpha[0]<<library[i].beg_alpha[1]<<"-"<<library[i].end_alpha[0]<<library[i].end_alpha[1]<<endl;
    	}
    	library[24].beg_alpha[24]='Y';
    	library[24].end_alpha[24]='Z';
    	cout<<library[24].beg_alpha[24]<<"-"<<library[24].end_alpha[24]<<endl;
    	do
    	{
    		cout<<"What would you like to do?"<<endl<<"Enter 1 to enter a book."<<endl<<"Enter 2 to display a SINGLE book."<<endl;
    		cout<<"Enter 3 to display an entire shelf."<<endl<<"Enter 4 to display the entire library."<<endl<<"Enter 5 to quit"<<endl;
    		cin>>sel;
    		do
    		{
    			switch(sel)
    			{
    			case 1: 
    				cout<<"Enter the title of your book."<<endl;
    				cin>>title;
    				library[title[0]-65].input(title);
    				break;
    	
    			case 2:
    				cout<<"Enter the name of the book you would like to display."<<endl;
    				cin>>title;
    				library[title[0]-65].single_output(title);
    				break;
    
    			case 3:
    				cout<<"Enter the first letter of the shelf you would like to dislplay."<<endl;
    				cin>>let;
    				let=toupper(let);
    				library[let-65].output();
    				break;
    
    			case 4:
    				cout<<"Displaying the entire library....."<<endl;
    				for (i=0;i<25;++i)
    				{
    				library[i].output();
    				}
    				break;
    
    			case 5:
    				cout<<"Exiting program.."<<endl;
    				break;
    
    			default:
    				cout<<"Make a selection from the menu."<<endl;
    				break;
    			}
    	
    		}
    		while (sel<1||sel>5);
    	}
    	while (sel!=5);
    }
    Thanks,
    J
  • iWillLiveforever
    New Member
    • Feb 2007
    • 136

    #2
    When you run the program have you entered something starting with z and something else not starting with z? If so does the program still crash or does it ignore the field with the z. Also define crash does the program give you an error or just finish running?

    Comment

    • Hypnotik
      New Member
      • Jun 2007
      • 87

      #3
      I have attempted a Z for the title, and something other than Z for the name and it crashes. By crash I mean a window pops up and asks if I want to send the error, don't send the error, or debug. The name in the window is Library 6.exe.

      I also just noticed it crashes if you enter a word that starts with a lower case letter.

      Thanks,
      J

      Comment

      • iWillLiveforever
        New Member
        • Feb 2007
        • 136

        #4
        Have you tried running it on multiple computers?

        Comment

        • Hypnotik
          New Member
          • Jun 2007
          • 87

          #5
          Originally posted by iWillLiveforeve r
          Have you tried running it on multiple computers?

          Yes someone else ran it on their computer, they are the one who told me it crashes if you enter a title with a lowercase letter.


          J

          Comment

          • scruggsy
            New Member
            • Mar 2007
            • 147

            #6
            You allocated space for 25 elements in your library array.
            There are 26 letters in the alphabet.
            Lines like this:
            Code:
            library[title[0]-65].input(title);
            ....will generate an index of 25 for a string beginning with "Z". But your array's indices only go from 0 to 24.

            Whenever you come across an issue like this which only happens in "edge cases" (at the beginning or end of an expected range for input), check the size of your arrays.
            And whenever you test your code, make sure you specifically test the edge cases, since those are the most common sources of error.

            Hope this helps.

            Comment

            • iWillLiveforever
              New Member
              • Feb 2007
              • 136

              #7
              Yes but that still does not account fot the lower case letters.

              Comment

              • scruggsy
                New Member
                • Mar 2007
                • 147

                #8
                Originally posted by iWillLiveforeve r
                Yes but that still does not account fot the lower case letters.
                Doesn't it?
                Look at the code and think about what happens if you enter a title beginning with a lower-case letter:
                Code:
                cout<<"Enter the title of your book."<<endl;
                cin>>title;
                library[title[0]-65].input(title);
                The lower-case letter 'a' has an ASCII code of 97.
                So if you enter a title beginning with a, the program tries to store it in library[97-65], aka library[32], which is out of bounds.

                Comment

                • Hypnotik
                  New Member
                  • Jun 2007
                  • 87

                  #9
                  Originally posted by scruggsy
                  You allocated space for 25 elements in your library array.
                  There are 26 letters in the alphabet.
                  Lines like this:
                  Code:
                  library[title[0]-65].input(title);
                  ....will generate an index of 25 for a string beginning with "Z". But your array's indices only go from 0 to 24.

                  Whenever you come across an issue like this which only happens in "edge cases" (at the beginning or end of an expected range for input), check the size of your arrays.
                  And whenever you test your code, make sure you specifically test the edge cases, since those are the most common sources of error.

                  Hope this helps.
                  That' helps alot, with the understanding of why that is happening. However I only want 25 "shelves" in the library. Letters 'Y' and 'Z' are to by stored on the same shelf. Would setting up an if statement, and subtracting 1 get the program to store titles with the letter 'Z' on shelf 24?

                  J

                  Comment

                  • Hypnotik
                    New Member
                    • Jun 2007
                    • 87

                    #10
                    Thanks a ton scruggsy. Your explanation was right on point. The program, after making some toupper() changes and a few other changes, is working great.

                    Once again, your explanation was great.

                    Thanks a lot, I appreciate it.

                    J

                    Comment

                    Working...