Thread: Skips inputs

  1. #1
    Registered User boneyflesh's Avatar
    Join Date
    Sep 2013
    Posts
    21

    Unhappy Skips inputs

    can anyone help me with this,,, it skips the input phase,, instead of inputing the lastname it goes directly to the firstname then middle initail and street name are working fine but it skips the streetname then goes to city and so on....

    Code:
    char l[50], f[50], m;		char strtname[50], cty[50], cntry[50];
    	int strtnum, zp, i;
    	 
    	system("cls");
    	printf("Enter Last name:");
    	gets(l);
    	printf("\nEnter First name:");
    	gets(f);
    	printf("\nEnter Middle initial:");
    	scanf("%c",&m);
    	printf("\nEnter Street number:");
    	scanf("%d",&strtnum);
    	printf("\nEnter Street name:");
    	gets(strtname);
    	printf("\nEnter City:");
    	gets(cty);
    	printf("\nEnter Country:");
    	gets(cntry);
    	printf("\nEnter ZIP code:");
    	scanf("%d",&zp);
    	
    	strcpy(p[recno].fullname.lastname,l);
    	strcpy(p[recno].fullname.firstname,f);
    	p[recno].fullname.mi =m;
    	p[recno].homeaddress.zip =zp;
    	strcpy(p[recno].homeaddress.city,cty);
    	strcpy(p[recno].homeaddress.country,cntry);
    	p[recno].homeaddress.zip =zp;



    output:
    Enter last name: //skips input
    Enter First name:

    Enter middle initial:

    Enter street number:

    Enter Street name://skips input
    Enter City:

    Enter country:

    Enter zip code:

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Use this instead:

    Code:
    fgets(string, sizeof string, stdin);
    sscanf(string, whateveryouwouldputforscanf, &var, ... );
    You might have to declare at least one extra string for random stuff, but... It's safer, and suddenly no issues.

  3. #3
    Registered User boneyflesh's Avatar
    Join Date
    Sep 2013
    Posts
    21
    hmmm,,, i dont know how to apply that but thanks anyway.......

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    Generally speaking, you should use fgets() to read ALL input.
    Then use sscanf, or some of the str... functions to parse that data how you want.

    Mixing string input (gets or fgets) with formatted input (scanf) is almost always a train-wreck waiting to happen.
    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

Similar Threads

  1. Replies: 28
    Last Post: 12-01-2013, 12:05 PM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. why the compiler skips this line..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 06-04-2009, 09:46 AM
  4. program skips over code
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 11-16-2006, 06:38 PM
  5. compiler skips one statement
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 08:40 AM