fgets issue

This is a discussion on fgets issue within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<string.h> void avg_min (double *, int *, FILE *); int main (void) { double avg, preavg, postavg, quizavg; ...

  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
    Katy, Texas
    Posts
    2,309
    This
    Code:
    scanf("&#37;d",&choice);
    is leaving the newline character in the input buffer.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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, 03:20 PM
  2. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 10:26 AM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21