Thread: program quit automatically

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    program quit automatically

    Code:
    #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;
       }
    the program quit automatically after entering isbn. any idea ?

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are trying to write strings to pointers. Try defining your books like:
    Code:
    typedef struct {
       char author[100];
       char title[100];
       char isbn[100];
       int edition;
    } Book;
    Now each of those variables can hold 99 characters plus a null terminator character. The other problem is when you read in the edition. You need to pass a variable pointer to scanf, not the variable's value. Also, you are reading in 1 more record than the user told you to. Look at your for loop declaration for the reason why.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    uhh the program arent suppose to quit at that stage, it still have another line to execute to enter

    Quote Originally Posted by bithub View Post
    You are trying to write strings to pointers. Try defining your books like:
    Code:
    typedef struct {
       char author[100];
       char title[100];
       char isbn[100];
       int edition;
    } Book;
    Now each of those variables can hold 99 characters plus a null terminator character. The other problem is when you read in the edition. You need to pass a variable pointer to scanf, not the variable's value. Also, you are reading in 1 more record than the user told you to. Look at your for loop declaration for the reason why.
    well Book *pbook[howmanyrecords]; <- this already declare the pbook as pointer, if I put scanf with &, it wont work

    well using author[100] and author* produce same result.

    about the forloop issue, I Will look onto it but for now I really need to know why it stop executing the rest of the program after entering isbn

    ohh nvm the reason why it quit is I havent allocate memory for the pointer variable inside the struct, that why array of 100 work as well but didnt quit
    Last edited by guest18; 08-06-2009 at 05:57 PM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    60
    Code:
    #include <stdio.h>
    
    typedef struct _Book {
       char* author; /* better set statically like char author[30] */
       char* title; /* better set statically */
       char* isbn; /* better set statically */
       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++)  /* max record: count from 0 to MAX - 1 */
       {
             pbook[counter] = (Book*)malloc(sizeof(Book));
                       
             printf("counter number is %d \n",counter + 1); /* counter = index + 1 */
             
             printf("enter author\n");
    	/* if dynamically, then allocate memory first */
             scanf("%s",pbook[counter]->author);
    	/* if dynamically, then allocate memory first */   
             printf("enter title\n");
              scanf("%s",pbook[counter]->title);
    	/* if dynamically, then allocate memory first */         
             printf("enter isbn\n");
              scanf("%s",pbook[counter]->isbn);
           	/* if dynamically, then allocate memory first */
             printf("enter edition\n");
             scanf("%d",pbook[counter]->edition);
            
            
            } 
            
            
       for (counter=0;counter < howmanyrecords;counter++) /* not 0 */
       {
                  
            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;
    }

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    well the reason why it is pointer in the struct is because the question Im doing required me to do so

    yeah, thanks alot, I shall go find way of allocating the pointer inside the struct

  7. #7
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    >yeah, thanks alot, I shall go find way of allocating the pointer inside the struct

    *facepalm*

    dynamic array in STRUCT
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by guest18 View Post
    well the reason why it is pointer in the struct is because the question Im doing required me to do so

    yeah, thanks alot, I shall go find way of allocating the pointer inside the struct
    malloc(), calloc()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. completed a program; desire input
    By spirited in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 08:50 AM
  4. How to quit a program???????
    By UnclePunker in forum C++ Programming
    Replies: 12
    Last Post: 05-30-2002, 12:57 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread