Thread: Problem with fgets and getting the string

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Problem with fgets and getting the string

    Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /* struct student -- the basis for the student data */
    struct student {
    	char name[50];	/* holds the name of the student */
    	struct grades *grade_list;	/* the student's grade list */
    	struct list *next_student;	/* pointer to next student */
    };
    /* struct grades -- the list of grades per student */
    struct grades {
    	int grade;	/* the actual grade for this struct */
    	struct grades *next_grade;
    };
    /* enums -- the menu item chosen */
    enum menu_opts {SRCHSTUD, ADDSTUD, EXIT, ERROR};
    
    int printmnu()
    {
    	char line[2];
    	/* display menu items for user input */
    	printf("***********************--MENU--************************\n");
    	printf("-Search Student(S)\t-Add Student(A)\t\t-Exit(E)\n");
    	/* user input */
    	printf("Choice: ");
    	fgets(line, sizeof(line), stdin);
    	strcpy(line, strupr(line));
    	/* figure out what user chose and return enum-ed value */
    	if (line[0] == 'S')
    	{return SRCHSTUD;}
    	else if (line[0] == 'E')
    	{return EXIT;}
    	else if (line[0] == 'A')
    	{return ADDSTUD;}
    	else
    	{return ERROR;}
    }
    
    int main()
    {
    	int test;
    	char strline[10];
    	test = printmnu();
    	/* Check user input and take appropriate actions */
    	if (test == EXIT)
    	{
    		printf("Exit (Yes/No): ");
    		fgets(strline, sizeof(strline), stdin);
    	}
    	return 0;
    }
    My problem is this. This code compiles fine (it is an unfinished program). Right now, it only gets input from the user. But, the first fgets works fine and gets a string from the user. When the function returns back to main, and if "test" == EXIT, I can't get a string from the user. I know it reached that code because it did print "Exit (Yes/No): ", but it just ended the program there. Why didn't it get the string the second time with fgets? If it at all matters, I'm using VC++6. If you have time, try to compile it and tell me the results. Thanks...

    --Garfield
    1978 Silver Anniversary Corvette

  2. #2
    Unregistered
    Guest
    Code:
    /* enums -- the menu item chosen */
    enum menu_opts {SRCHSTUD, ADDSTUD, EXIT, ERROR};
    Your problem is here. When you use enum but don't define the values, C will set the values starting at 0, an integer, and count up. 'S', 'E', and 'A' don't coincide with 0, 1, and 2. Try changing your enum declaration to

    enum menu_opts {SRCHSTUD = 'S', ADDSTUD = 'A', EXIT = 'E', ERROR = -1};

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Change char line[2]; in printmnu() to something like
    &nbsp; char line[20];

    Remember that fgets stores n-1 characters and a \0, so [2] just allows you a single character.

    This is the problem, because this leaves the "\n" in the input buffer, ready to mess up your next fgets call.

    > strcpy(line, strupr(line));
    Try
    &nbsp; strupr(line);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed