Program quit automatically



  • #include <stdio.h>
    
    typedef struct {
       char* author;
       char* title;
       char* isbn;
       int edition;
    } Book;
    
    int main()
    {
       int howmanyrecords;
       int counter=0;
           
       printf("How Many Record you want ?");
       scanf("%d",&howmanyrecords);
       
          Book *pbook[howmanyrecords];
       
     
             for(counter=0;counter<=howmanyrecords;counter++) 
       {
             pbook[counter] = (Book*)malloc(sizeof(Book));
                       
             printf("counter number is %d \n",counter);
             
             printf("enter author\n");
             scanf("%s",pbook[counter]->author);
       
             printf("enter title\n");
              scanf("%s",pbook[counter]->title);
             
             printf("enter isbn\n");
              scanf("%s",pbook[counter]->isbn);
             
             printf("enter edition\n");
             scanf("%d",pbook[counter]->edition);
            
            
            } 
            
            
       for (counter=0;counter <= 0;counter++)
       {
                  
            printf ("record %d author is %s and the title is %s and the isbn %s is and the edition is%d\n"
                   ,counter,pbook[counter]->author,pbook[counter]->title,pbook[counter]->isbn,pbook[counter]->edition);
             
            
            }   
             
    
       
       return 0;
       }
    

    after entering isbn it quit automatically . anyone can help ?

    ehh but first thing first , how do I format this nicely, I copy and paste the code from dev-c++ and the forum software just throw away all my space and new line


  • It's seg faulting because you are trying to access unallocated memory.  check the return code after it quits and you will see that it is not 0.   You need to allocate memory for the fields on Book.



  • #include

     you don't need this line if you're not pulling in another header either. I'm with tster that you didn't allocate memory for book.



  • Not only have you not allocated any memory for the books, but you haven't allocated any for the elements of book.

    Seeing as you filed this under C++ I suggest you look up constructors and the STL string classes.  Better still, don't write this sort of application in C/C++.

    It has its place, but this is exactly why nobody uses this shit for applications any more.  You spend more time working out WTF you have forgotten to do with the language than actually solving the domain problem.



  • @LoztInSpace said:

    Not only have you not allocated any memory for the books, but you haven't allocated any for the elements of book.

    Seeing as you filed this under C++ I suggest you look up constructors and the STL string classes.  Better still, don't write this sort of application in C/C++.

    It has its place, but this is exactly why nobody uses this shit for applications any more.  You spend more time working out WTF you have forgotten to do with the language than actually solving the domain problem.

     

    He obviously didn't choose this language.  This is homework and he is learning the language, not developing an application.



  • @LoztInSpace said:

    Not only have you not allocated any memory for the books, but you haven't allocated any for the elements of book.

    Seeing as you filed this under C++ I suggest you look up constructors and the STL string classes.  Better still, don't write this sort of application in C/C++.

    It has its place, but this is exactly why nobody uses this shit for applications any more.  You spend more time working out WTF you have forgotten to do with the language than actually solving the domain problem.

     

    I totally disagree with you. I think C++ still has its place. He obviously hasn't learn to use pointers yet. Knowing the concepts of proper memory managment, pointers, etc, is a good skill, even in newer languages. People always think that newer languages like .Net never leak memory, but thats entirely not so. I agree C++ is probably one of the toughest out there, but I don't view it as an obstacle in writing an application.



  • I agree that knowing about pointers etc can be useful and also that C++ does have it's place, which is what I said. That place is not in 'normal' business applications though. Your time needs to be spent working on things that make/save money in the business you are in, not stupid, avoidable technical problems.

    Let's look at a typical progression:

    C - stuff everywhere, memory bombs, nightmare crashes and minute inspection of every line
    C++ v1 - ditto, but with a glimer of hope
    C++ with STL: smart pointers and strings. Yay!. Oh, hold on, we've ended up with half arsed managed code anyway, it's just not as good as .NET/Java or whatever.
    .NET - thanks Christ for that - How did I ever get anything done beforehand? (now you have to fill your time writing elaborate 'enterprisey' sledgehammers to crack your nuts, but that's another rant).



  • Sigh - the obvious analogy here is having to master pseudo HTML to make a simple point readable:

    I agree that knowing about pointers etc can be useful and also that C++ does have it's place, which is what I said. That place is not in 'normal' business applications though. Your time needs to be spent working on things that make/save money in the business you are in, not stupid, avoidable technical problems.

    Let's look at a typical progression:

    • C - stuff everywhere, memory bombs, nightmare crashes and minute inspection of every line
    • C++ v1 - ditto, but with a glimer of hope
    • C++ with STL: smart pointers and strings. Yay!. Oh, hold on, we've ended up with half arsed managed code anyway, it's just not as good as .NET/Java or whatever.
    • .NET/Java - thank Christ for that - How did I ever get anything done beforehand? (now you have to fill your time writing elaborate 'enterprisey' sledgehammers to crack your nuts, but that's another rant)

     


Log in to reply