ignored gets..

This is a discussion on ignored gets.. within the C Programming forums, part of the General Programming Boards category; hi, i am a newbie in C programming. lately, i am trying to create a program for travel agency simulation ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    ignored gets..

    hi, i am a newbie in C programming.
    lately, i am trying to create a program for travel agency simulation
    it will asks for user inputs for the information.

    i have edited the code to use fgets() and prevent using scanf() as adviced. i used fscanf() instead. the problem still the same, the program skip one of the fgets(). i don't know where the problem is. the program works normally if i use char data type for all structure member. but it only works with one of the input function. i am really confused. is it that i can't use different data type or i can't use different input function?

    tip and help is really appreaciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_NAME 20
    #define MAX_ADD 35
    #define MAX_PHONE 20
    #define BOOKING_TYPE 20
    #define MAX_CODE 5
    #define MAX_CUSTCODE 10
    
    typedef struct customer
    {
    	char name[MAX_NAME];
    	char address[MAX_ADD];
    	int custCode[MAX_CUSTCODE];
    	int phone[MAX_PHONE];
    	struct customer *next;	
    }Customer;
    
    Customer *addCustomer(void);
    
    int main()
    {
    	addCustomer();
    
    	return 0;
    }
    
    Customer *addCustomer(void)
    {
    	Customer *current, *first;
    	char choice[2];
    	current = first = malloc(sizeof(Customer));
    	
    	printf("\nplease enter customer code:\n");
    	fscanf(stdin, "%d", current->custCode);
    	printf("\nplease enter customer name:\n");
    	fgets(current -> name, sizeof(current->name), stdin);
    	printf("\nplease enter customer address:\n");
    	fgets(current -> address, sizeof(current->address), stdin);
    	printf("\nplease enter customer's phone number:\n");
    	fscanf(stdin, "%d", current->phone);
    
    	printf("\n\nAdd another customer?(y/n)");
    	fgets(choice, sizeof(choice), stdin);
    	
    	while (*choice != 'N' || *choice != 'n')
    	{
    		if ((current -> next = malloc(sizeof(Customer))) == NULL)
    		{
    			printf("Out of memory\nCan't add any customers\n");
    			return first;
    		}
    		current = current->next;
    
    		printf("\nplease enter customer code:\n");
    		fscanf(stdin, "%d", current->custCode);
    		printf("\nplease enter customer name:\n");
    		fgets(current -> name, sizeof(current->name), stdin);
    		printf("\nplease enter customer address:\n");
    		fgets(current -> address, sizeof(current->address), stdin);
    		printf("\nplease enter customer's phone number:\n");
    		fscanf(stdin, "%d", current->phone);
    		
    		printf("\n\nAdd another customer?(y/n)\t");
    		fgets(choice, sizeof(choice), stdin);
    		}
    	current -> next = NULL;
    	return first;
    }
    Last edited by bakia; 05-01-2005 at 02:57 AM. Reason: another problem here..

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    1. NEVER use gets()
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    There is no protection for stopping the user typing in more data than you're prepared to accept.

    2. NEVER use scanf()
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    scanf() is also poor in this respect, and it's use of standard in is different to what most other functions expect. The result is weirdness when you try and use other input functions alongside scanf.

    Basically, stick to fgets() for all primary input, and then validate the input and copy to it's final storage.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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