#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Exclamation No compiling Error but while runing program give fatal error


    error is in "void search()" function but compiler didn't get it and compile successfully .... and program is also running but when call "search()" function it give error plz help

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    struct proj
    {
    	string tittle;
    	string author;
    	string publisher;
    	string loc;
    	int price;
    };
    class project
    {
    private:
    	struct proj lib[5];
    	char test[100];
    	int s_i;
    	char opt;
    public:
    	void data()
    	{
    		struct proj lib[5];
    		lib[1].tittle=("a");
    		lib[1].author=("a");
    		lib[1].publisher=("a");
    		lib[1].loc=("Row # 3, Rack # 2, Shelf # 5");
    		lib[1].price=400;
    		lib[2].tittle=("b");
    		lib[2].author=("b");
    		lib[2].publisher=("b");
    		lib[2].loc=("Row # 9, Rack # 1, Shelf # 1");
    		lib[2].price=300;
    		lib[3].tittle=("c");
    		lib[3].author=("c");
    		lib[3].publisher=("c");
    		lib[3].loc=("Row # 5, Rack # 8, Shelf # 2");
    		lib[3].price=200;
    		lib[4].tittle=("d");
    		lib[4].author=("d");
    		lib[4].publisher=("d");
    		lib[4].loc=("Row # 1, Rack # 7, Shelf # 4");
    		lib[4].price=350;
    		lib[0].tittle=("e");
    		lib[0].author=("e");
    		lib[0].publisher=("e");
    		lib[1].loc=("Row # 2, Rack # 6, Shelf # 1");
    		lib[0].price=150;
    	}
    	void search() /* i have problem in this function */
    	{
    		int i;
    			for(i=0;i<5;i++)
    			{
    				if(lib[i].tittle[0] == test[0] && lib[i].tittle[1] == test[1] && lib[i].tittle[2] == test[2])
    				{
    					s_i=i;
    				} /* strcomp() also not working*/
    			}
    	}
    	void input_tittle()
    	{
    		cout<<"Enter Book Tittle = ";
    		cin.getline(test,99);
    	}
    	void display()
    	{
    		cout<<endl<<" Tittle    "<<lib[s_i].tittle<<endl<<" Author    "<<lib[s_i].author<<endl<<" Publisher "<<lib[s_i].publisher<<" Location  "<<lib[s_i].loc<<endl<<"Price     "<<lib[s_i].price<<" Rs/- (should pay if book lost by reader)"<<endl<<endl;
    	}
    };
    
    int main()
    {
    	project obj;
    	obj.data();
    	cout<<"My library"<<endl;
    	obj.input_tittle();
    	obj.search();
    	obj.display();
    	system("pause");
    	return 0;
    }
    Last edited by muas; November 10th, 2013 at 01:49 PM. Reason: forget to add some information
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    What error does it give? And what input data did you give it?

    C-style strings (which is what test is) are char arrays. That means that the first character in the string will be at test[0]. You skip that character.

    The titles you load your database with are all just one character long. And yet you compare character for character after that one character. Shouldn't you only compare titles for the length of the title and not for some arbitrary length that exceeds the lengths of all the titles?

    If you don't find a match, shouldn't you display a message to that effect? And shouldn't s_i be initialized to a known value and not left to be some random garbage value? -- assuming that the constructor won't automatically initialize it to zero or the like. And if you don't find a title match, shouldn't you set s_i to a known invalid value that you can then test for later before you try to use s_i? Kind of like setting an invalid pointer to NULL and then testing for NULL before trying to use the pointer.

    And how did you try to use strcomp? What is strcomp? I've never heard of it and you don't define it anywhere.

    Now, if you had instead tried to use strcmp (in programming, spelling is of the utmost importance!), how did you try to use it? Did you just try something like:
    strcmp(lib[i].tittle,test);
    ? Or did you remember string's .c_str() method?:
    strcmp(lib[i].tittle.c_str(),test);
    And did you interpret the return value correctly? In that a zero indicates a match? And did you realize that strcmp is case-sensitive?

    Too much missing information for us to be able to figure out your problem.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    PS

    Code:
        void data()
        {
            struct proj lib[5];
            lib[1].tittle=("a");
            lib[1].author=("a");
            lib[1].publisher=("a");
            lib[1].loc=("Row # 3, Rack # 2, Shelf # 5");
            lib[1].price=400;
            lib[2].tittle=("b");
            lib[2].author=("b");
            lib[2].publisher=("b");
            lib[2].loc=("Row # 9, Rack # 1, Shelf # 1");
            lib[2].price=300;
            lib[3].tittle=("c");
            lib[3].author=("c");
            lib[3].publisher=("c");
            lib[3].loc=("Row # 5, Rack # 8, Shelf # 2");
            lib[3].price=200;
            lib[4].tittle=("d");
            lib[4].author=("d");
            lib[4].publisher=("d");
            lib[4].loc=("Row # 1, Rack # 7, Shelf # 4");
            lib[4].price=350;
            lib[0].tittle=("e");
            lib[0].author=("e");
            lib[0].publisher=("e");
            lib[1].loc=("Row # 2, Rack # 6, Shelf # 1");
            lib[0].price=150;
        }
    In this method, you create a local array which you then initialize and when then when you exit that method that local array ceases to exist, as is the case with all local variables.

    Please remember the concept of variable hiding. When you declare a variable within a narrower scope with the same name as a variable in a wider scope, then when you reference that name it will use that narrower-scope variable, thus hiding the wider-scope variable. Thus, your local array, lib, hides the class' property of the same name. You end up initializing the local array and the class property, lib, never gets initialized.

    Immediate solution: lose the declaration in the data method (highlighted in red).

    Please also direct your attention to the other line in red. You are initializing lib[1].loc twice with different data. I think your intention for the line in red was for it to initialize lib[0].loc.

    Also, you don't need to use the keyword struct when you declare a proj object. This is after all C++, you know.

    Don't forget to look into the other issues I noted.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    strcmp() also not working it gives error "Debug assertion failed" after i input tittle
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0
    every thing else is working except search function i checked rest of the code and its giving output and i mistakenly wrote strcmp as strcomp
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    You've made changes. That means your code is different. We cannot read your mind.

    If you don't understand what I am requesting, then simply read my mind.

    PS
    I trust that you did take care of that variable-hiding bug I had pointed out.
    Last edited by dwise1_aol; November 10th, 2013 at 02:20 PM.

IMN logo majestic logo threadwatch logo seochat tools logo