Thread: fprintf

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    1

    Question fprintf

    I wrote a little program to maintain a household inventory and it exits before it get the input from the keyboard.


    here is the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void select_mode();
    void add_item(FILE *out);
    /*void delete_item(FILE *out);
    void search_item(FILE *out);
    void edit_item(FILE *out);*/
    
    typedef struct
    { 
       char make[30],
            model[30],
            serialnum[30],
            item_desc[4000];
    }INV_ITEM;
    
    INV_ITEM item;
    
    
    int main()
    {
       select_mode();
    }
    
    
    void select_mode()
    { 
       char mode;
       FILE *out;
       out=fopen("inv_file","a");
     
       printf(" add an item (1)\n");
       printf(" delete an item (2)\n");
       printf(" search for an item (3)\n");
       printf(" edit an item (4)\n");
       printf(" What would you like to do: ");
       mode=getchar();
       
       switch (mode)
       {
          case '1':   add_item(out);
                      break;
          case '2':   printf("delete_item(out);");
                      break;
          case '3':   printf("search_item(out);");
                      break;
          case '4':   printf("edit_item(out);");
                      break;
          default :   printf("You must choose one of the select menu items");
                      select_mode();
                      break;
       }
       fclose(out);
    }
    
           
       
    void add_item(FILE *out)
    {
       char *ptr;
       fprintf(stdout,"Enter the name of the maker : ");
       fgets(item.make, sizeof(item.make), stdin);
       fprintf(stdout,"%s|",item.make);
    }
    it prints out the "Enter the name of the maker " and then goes straight to a unix prompt.
    Last edited by carlcromer; 02-13-2003 at 10:11 AM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Please read this thread, then edit your post accordingly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    All you need to do is throw in something to remove the newline character from the input stream. When you put a char into mode, there were actually two keypresses, one for the char itself and one for enter. Since you only read the char for mode, enter is still in the stream and fgets conveniently stops reading at that character :-)
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void select_mode();
    void add_item(FILE *out);
    
    typedef struct
    {
      char make[30],
      model[30],
      serialnum[30],
      item_desc[4000];
    }INV_ITEM;
    
    INV_ITEM item;
    
    int main()
    {
      select_mode();
    }
    
    void select_mode()
    {
      char mode;
      FILE *out;
    
      out = stdout;
      
      printf(" add an item (1)\n");
      printf(" delete an item (2)\n");
      printf(" search for an item (3)\n");
      printf(" edit an item (4)\n");
      printf(" What would you like to do: ");
      mode=(char)getchar();
      getchar(); /* Remove newline */
      
      switch (mode)
      {
      case '1':   add_item(out);
        break;
      case '2':   printf("delete_item(out);");
        break;
      case '3':   printf("search_item(out);");
        break;
      case '4':   printf("edit_item(out);");
        break;
      default :   printf("You must choose one of the select menu items");
        select_mode();
        break;
      }
      fclose(out);
    }
    
    void add_item(FILE *out)
    {
      fprintf(out,"Enter the name of the maker : ");
      fgets(item.make, sizeof(item.make), stdin);
      fprintf(out,"%s|",item.make);
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  5. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 06:58 PM