PDA

View Full Version : Mot cau hoi hoi bi ngo ngan!!



woodyvn
03-09-2002, 12:13
Mình viết một đoạn code dùng để bắt phím như sau:

...
printf("- Would you like to input this name again? - Y/N ");
if (getch()=='y' || getch()=='Y')
{ printf("\n- Now you can give me its name: "); gets(scr_name); }
else
{ printf("\n- You should copy this file into this folder,\n"); printf(" and run me again!"); exit(1); }
...

Nhưng khi chạy chương trình sẽ như sau:

- Would you like to input this name again? - Y/N
- Now you can give me its name: y

Làm thế nào để không có chữ y (khi mình ấn phím y) ở dòng thứ hai như trên, tức là như sau:

- Would you like to input this name again? - Y/N y
- Now you can give me its name:

Các bạn giúp mình với nhé.

BlackArt
04-09-2002, 14:14
Chỉ việc xoá bộ đệm bàn phím trước khi hiển thị :"Now you can give me its name" là được thôi.

patfinder
05-09-2002, 12:08
Doan ma nay khong on.
Neu nguoi dung nhan ky tu dau khong phai la 'y' thi ho se se phai nhan tiep mot ky tu nua (cau lenh getch()=='Y').

Poseidon
08-09-2002, 09:14
Minh cung nghi nhu ban Patfinder vay!

Nhu Quynh Tan
09-09-2002, 11:57
char ch;

...
printf("- Would you like to input this name again? - Y/N ");

while (1)
{
ch = getch();

if (ch == 'y' || ch == 'Y' || ch== 'n' || ch == 'N')
break;
}

switch (ch)
{
case 'y':
case 'Y':
printf("\n- Now you can give me its name: ");
gets(scr_name);
break;

default:
printf("\n- You should copy this file into this folder,\n");
printf(" and run me again!");
exit(1);
}

woodyvn
10-09-2002, 12:16
A thuc ra doan ma tren hoan toan khong co loi gi ca. Toi da bien dich chuong trinh co doan ma tren voi Turbo C 2.0 va khong bi loi chu 'y' ngo ngan day nua. Loi nay chi xuat hien khi toi bien dich chuong trinh nay voi Borland C++ 5.5 (free command line tool). Cac ban co biet cach nao khac phuc voi BC++ 5.5 khong

Nhu Quynh Tan
11-09-2002, 11:31
No er ????
...
printf("- Would you like to input this name again? - Y/N ");
if (getch()=='y' || getch()=='Y')
{ printf("\n- Now you can give me its name: "); gets(scr_name); }
else
{ printf("\n- You should copy this file into this folder,\n"); printf(" and run me again!"); exit(1); }
...

....and what will happen when user press the key 'A' or 'Z' ? Take your code solid and strong !

Nhu Quynh Tan
11-09-2002, 11:34
I think both BC 5.5 or TC3.0 are not the problem. The problem is inside your code :)

woodyvn
12-09-2002, 14:42
You don't believe? Then you should compile this piece of code with TC 2.0, and you will see it runs smoothly. The user doesn't need to press 'N' or 'n' to cancel his work. He can do it by press any key except 'y' or 'Y'. And if you like to see the button you've pressed appearing then you should replace getch() with getche(). Everything will work well. I think the BC++5.5 has some bugs with getch() or getche().

madcat
04-10-2002, 13:44
goi ham flushall() truoc khi goi gets , day la kinh nghiem cua toi, van de la gets lay du lieu tu keyboard buffer , ban phai clear no truoc khi dat mot chuoi moi vao

tranducmille
01-08-2003, 21:13
Bạn có thể dùng hàm fflush(stdin) trước hàm gets() đó . rồi tương tự như madcat đó .

kickme!
10-08-2003, 14:43
Bạn có thể sử dụng đoạn mã sau trước hàm gets:
while(getch()!='\0');
sau đó gọi hàm getch() bình thường.Ý nghĩa của vòng while là xóa bộ đệm nhưng hàm getch() sau nó sẽ không xuất kí tự từ bàn phím.

louisking
14-08-2003, 11:16
Trước khi bạn đánh câu lệnh "Would ..." bạn nên xóa vùng buffer trong bàn phím rồi sau đó bạn lại tiếp tục xóa tiếp một lần nữa thử xem .