For Loop Skipping Statements.

This is a discussion on For Loop Skipping Statements. within the C Programming forums, part of the General Programming Boards category; hello. im a newbie C user and im having a little trouble in these for loop of mine im using. ...

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

    For Loop Skipping Statements.

    hello. im a newbie C user and im having a little trouble in these for loop of mine im using. the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped. im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.... somebody please help.. tnx


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    void main()
    {
    int i,j;
    struct date{
    	int month;
    	int day;
    	int year;
    	};
    struct info
    {
    char idno[10];
    char lastname[30];
    char firstname[30];
    char middlename[30];
    
    struct date birthdate;
    char gender[1];
    char address[50];
    int height_in_meters;
    int weight_in_kgs;
    };
    
    struct info student[50];
    struct info employee[50];
    struct date today;
    
    for(i=0;i<2;i++)
    {
    printf("Enter Information for Student %d: \n\n",i+1);
    printf("Enter ID Number: ");
    gets(student[i].idno);
    printf("Enter Student Last Name: ");
    gets(student[i].lastname);
    printf("Enter First Name: ");
    gets(student[i].firstname);
    printf("Enter Middlename: ");
    gets(student[i].middlename);
    printf("Enter Address: ");
    gets(student[i].address);
    printf("Enter Birth Month(MM): ");
    scanf("%d", &student[i].birthdate.month);
    printf("Enter Birth Day(DD): ");
    scanf("%d", &student[i].birthdate.day);
    printf("Enter Birth Year: ");
    scanf("%d", &student[i].birthdate.year);
    printf("Enter Gender(M/F): ");
    scanf("%s", &student[i].gender);
    printf("Enter Height(m): ");
    scanf("%d", &student[i].height_in_meters);
    printf("Enter Weight(kgs): ");
    scanf("%d", &student[i].weight_in_kgs);
    printf("\n\n");
     }
    getch();
    }
    here is a screenshot of the outputName:  Untitled.jpg
Views: 201
Size:  30.6 KB
    as you can see the gets part for the line "Enter ID Number is skipped on the second iteration..

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    First problem is you're using gets()
    Do NOT use gets() -> SourceForge.net: Gets - cpwiki

    The second problem is you're mixing scanf() with other input methods.
    To fix this, you need to clean up after scanf() if you want to use a line-oriented input method (ideally, fgets)
    FAQ > Flush the input buffer - Cprogramming.com

    > char gender[1];
    Beware of using %s to read into a single char.
    %s will always append a \0, so in most cases this is a buffer overrun.

    Use " %c" (note the leading space) to read a single char.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by rswest View Post
    the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped.
    im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.... somebody please help.. tnx


    Code:
    void main()
    {
    for(i=0;i<2;i++)
    {
    printf("Enter Information for Student %d: \n\n",i+1);
    printf("Enter ID Number: ");
    gets(student[i].idno);
     /*...*/
    scanf("%d", &student[i].weight_in_kgs);
    printf("\n\n");
     }
    The key part happens at the end. Say you type in 54 and press enter. Then here is what's in your input buffer:

    "54\n"

    So when scanf reads in %d, it gets the 54 and here is what's left in your input buffer:

    "\n"

    So your loop goes back to the beginning and you call gets, which reads the empty line. That is why it is "skipping" the first gets as you say.

    Suggestions:
    1. Don't use gets(). Use fgets instead.
    2. Don't combine scanf with fgets. Instead, use fgets to read an entire line, and then use sscanf on that.

    Example:

    Code:
     
    char buf[1000];
    fgets(buf, 1000, stdin);
    sscanf(buf, "%d", &x);
    If you use this approach consistently it'll be much easier to do line-based input. Each call to fgets is exactly one line of input.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    i see.. last night i was reading some tutorial and other posts here in the forums. and i saw someone having the same problem and somebody told him to use getchar(). so i used getchar() i added it after all the scanf and it now works. (i think). but now that im about to print the information. ex: print the details.info for all five members... i get to print all the data fine except for the address part. i mean it seems that scanf or gets dont work on getting anything i inputted for the address. anyways il try you're suggestions

    so basically i must not mix using scanf() and gets()? im not quite familiar yet with fgets and sscanf but ill give it a try. thanks salem and c99 tutorial

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop not skipping when condition isn't met
    By MNM1245 in forum C Programming
    Replies: 7
    Last Post: 11-04-2012, 04:32 PM
  2. How do I stop my loop from skipping getline
    By Darkroman in forum C++ Programming
    Replies: 2
    Last Post: 08-29-2011, 02:09 PM
  3. Skipping If Statements?
    By CBecker5000 in forum C Programming
    Replies: 1
    Last Post: 11-08-2009, 03:05 PM
  4. Program skipping while loop
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 03:30 AM
  5. Skipping a loop?
    By axon in forum C++ Programming
    Replies: 0
    Last Post: 02-16-2003, 06:52 PM

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