Thread: fgets issue

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    fgets issue

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void avg_min (double *, int *, FILE *);
    
    int main (void) 
    {
    
    	double avg, preavg, postavg, quizavg;
    	int min, premin, postmin, quizmin, check;
    	double finalgrade;
    	int choice = 1; 
    	int counter= 0;
    	FILE *out;
    	char student_name[30]={""};
    	char *inputptr;
    
    	out = fopen("results.txt","a");
    
      while (choice == 1)
      {
        printf("1. Next student\n");
        printf("2. Done\n");
        printf("Enter choice:");
        scanf("%d",&choice);
    
        switch (choice)
        {
    
          case 1:
    
          printf("Enter the student's name:");
          inputptr = fgets (student_name, 30, stdin);
          printf( inputptr );
          if( inputptr != NULL)
          {
            printf( inputptr );
            //fputs (student_name, out);
          }
    in case 1, when fgets() is called it does not wait for user input and continues on to next lines of code. I'm not sure why it is doing this.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This
    Code:
    scanf("&#37;d",&choice);
    is leaving the newline character in the input buffer.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    ah ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange behavior if I use fgets() after fscanf()
    By avi2886 in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 04:20 PM
  2. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 11:26 AM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  4. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  5. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM