Thread: this is troubling me please help

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    6

    Angry this is troubling me please help

    Hi everyone in this program down below in the while loop it works the first time, but when it goes back to start of the loop again, it skips the first question of the loop all the time, and it wont let me enter it again, it just skips it
    can you please help me why this wont let me do it or fix it up for me =) thanx heaps
    Rob


    #include <stdio.h>
    #include <string.h>
    #define MAX 100
    /* size of structure array */

    typedef struct {
    char name[ 20 ];
    char phone [ 15 ];
    char studentid [ 15 ];


    } customerData;

    void bubblesort(customerData[]);

    int main()
    {
    customerData customer[MAX];
    customerData hold;
    int i, pass, element;
    char targetName[ 20 ];
    int s;

    printf("Enter students records (names and phone no.).\n");
    printf("\nThey will then be sorted by names.\n\n");



    while ( s != 0)
    {

    printf( "\nEnter customer lastname Then First name (E.g. Jones John): ");
    gets ( customer[i].name );
    printf( "\nEnter phone number: " );
    gets( customer[i].phone );
    printf( "\nEnter Student ID: ");
    gets( customer[i].studentid);
    printf( "With to enter anymore?(1 for YES, 0 for NO)\n ");
    scanf("%d", &s);
    }

    }

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Try using a do/while loop here:

    Code:
    do
    
    {
    
    
    
    
    
    }while (s != 0);
    Mr. C.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    6
    do
    {

    printf( "\nEnter customer lastname Then First name (E.g. Jones John): ");
    gets ( customer[i].name );
    printf( "\nEnter phone number: " );
    gets( customer[i].phone );
    printf( "\nEnter Student ID: ");
    gets( customer[i].studentid);
    printf( "With to enter anymore?(1 for YES, 0 for NO)\n ");
    scanf("%d", &s);
    } while (s != 0);

    }

    If Written this but still i have the same problem?
    any other suggestions?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Add "fflush(stdin);" after scanf(). Then the [Enter] should not be processed twice any more.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Don't use either of the following:

    - gets()
    This has no buffer overrun protection.

    - fflush(stdin)
    Is undefined, because fflush() is for output streams. Do a board search to find a discussion on this subject.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. errors troubling me
    By paushali in forum C Programming
    Replies: 2
    Last Post: 08-29-2007, 11:28 AM
  2. Troubling compiling with pthread
    By Yasir_Malik in forum Linux Programming
    Replies: 2
    Last Post: 09-30-2003, 07:08 AM
  3. This Has Been Troubling Me For Some Time.
    By DeanDemon in forum C++ Programming
    Replies: 8
    Last Post: 12-12-2002, 06:22 AM