Thread: Struct and while

  1. #1
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20

    Struct and while

    Hi, I have a little problem with this code. When I try to run it, only asks my to write the name (nombre), and jumps to the address (direccion) of the person number 2. Why is happing that?

    Code:
    int main(int argc, char* argv[])
    {
    struct persona{
    char nombre [65];
    char direccion[65];
    int telefono;
    }gente[5];
    
    int a=0, b=0;
    
    do{
    a++;
    printf("\nPersona %i\n--------------------", a);
    
    printf("\nIntroduzca el nombre de la persona: ");
    scanf("%c", &gente[b].nombre);
    
    printf("\nIntroduzca la direccion de la persona: ");
    scanf("%c", &gente[b].direccion);
    
    printf("\nIntroduzca el telefono de la persona: ");
    scanf("%i", &gente[b].telefono);
    b++;
    
    }while (a < 5);
    
    getch();
    
            return 0;
    }
    Thanks,

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    '\n' is left in the buffer
    Flush the buffer

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    use this code to clear the input buffer
    Code:
    while((ch=getchar())!='\n' && ch!=EOF);
    Code:
    printf("\nIntroduzca el nombre de la persona: ");
    scanf("%s", &gente[b].nombre);
    while((ch=getchar())!='\n' && ch!=EOF);
    printf("\nIntroduzca la direccion de la persona: ");
    scanf("%s", &gente[b].direccion);
    while((ch=getchar())!='\n' && ch!=EOF);
    printf("\nIntroduzca el telefono de la persona: ");
    scanf("%i", &gente[b].telefono);
    b++;
    while((ch=getchar())!='\n' && ch!=EOF);
    -s.s.harish
    Last edited by ssharish; 06-23-2005 at 05:33 AM.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Actually, you are reading in a character (%c) into a string buffer. Change %c to %s and drop the & on the variables and take a few minutes to google 'man scanf'.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20
    I am having some problems qith this code.
    Code:
    while((ch=getchar())!='\n' && ch!=EOF);
    The compiler gives me an error that says "Undefined symbol ch". What kind of variable has to be ch? Has to have some initial value?. Also, the compiler says me that the "code has not effect", but, perhaps, this'll disappear when the variable ch is correct. Any idea?

    Thank you,

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Bitojis
    I am having some problems qith this code.
    Code:
    while((ch=getchar())!='\n' && ch!=EOF);
    The compiler gives me an error that says "Undefined symbol ch". What kind of variable has to be ch? Has to have some initial value?. Also, the compiler says me that the "code has not effect", but, perhaps, this'll disappear when the variable ch is correct. Any idea?

    Thank you,
    In this case you would declare the variable ch to be of type int:

    Code:
    int ch;  /* no need to intialise it here... */
    
    .....
    
    while((ch=getchar())!='\n' && ch!=EOF) /* ... as it is assigned a value here */
    As to the "code has no effect" - can't help you on that one without seeing what code you are using.

    ~/

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    read FAQ to find more about this code
    Code:
    int ch;
    . 
    .
    while((ch=getchar()!='\n' && ch!=EOF)
    .
    .


    - s.s.harish
    Last edited by ssharish; 06-24-2005 at 05:44 AM.

Popular pages Recent additions subscribe to a feed