fflush didn't work

This is a discussion on fflush didn't work within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I try to validate the kode inputs by using getche to validate each characters input,after that i ask the name ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    fflush didn't work

    I try to validate the kode inputs by using getche to validate each characters input,after that i ask the name {gets(nama);} instead of receiving input the program passes it.
    my codes:


    char kode[4];
    char nama[20];
    void menu()

    {
    char input;
    int i=0;
    printf("\n");
    gotoxy(1,7);
    printf("KODE : ");
    do
    { gotoxy(12+i,7); printf(" ");
    gotoxy(12+i,7);
    input=getche();
    if(input==8&&i>0)
    i--;
    else
    if(i<=2&&((input>=48&&input<=57)||(input>=97&&inpu t<=122))) /*validasi per karakter*/
    { kode[i]=input;
    i++;
    }
    } while(input!=13||i!=3);
    gotoxy(1,8);
    printf("NAMA : ");
    do{gotoxy(12,8);clreol();
    gotoxy(12,8);fflush(stdin);gets(nama);
    }while(strlen(nama)>20);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    First the trivial errors
    > if(input==8&&i>0)
    Try writing this as
    if ( input == '\b' && i > 0 )

    > if(i<=2&&((input>=48&&input<=57)||(input>=97&&inpu t<=122)))
    Ditto
    if ( i <= 2 && ((input>='0' &&input<='9' )||(input>='a' &&input<='z' )))

    Both improve readability and portability

    > while(input!=13||i!=3);
    Since you seem to be using DOS specific input routines, I guess this should be
    while ( input != '\r; || i!=3 );

    > fflush(stdin);
    Flushing input streams is an undefined operation - so you need to use something else.

    Like this perhaps
    while ( getchar() != '\n' ) continue;

    Or perhaps (since this is non-ANSI)
    while ( getch() != '\r' ) continue;

    > gets(nama);
    > }while(strlen(nama)>20);
    Now here's the real problem - if strlen(nama) is > 20, you've already trashed memory you don't own (and you might not even get a chance to strlen it). Even if you do, your program is already wrong.
    Use fgets to read input - at least it will not exceed the limits of the buffer you supply.

  3. #3
    Registered User Generator's Avatar
    Join Date
    Aug 2001
    Posts
    238
    How long have you been programming salem, you know everything. Have you written any tuts?
    What's a matter you no like peppi?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

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