Posted By

Zufolek on 06/19/09


Tagged


Versions (?)

console string input, confirm, pause


 / Published in: C
 

windows

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5.  
  6. char waitkey(){
  7. while(!kbhit())_sleep(0);
  8. {
  9. char c=getch();
  10. if(c==27)exit(-1);
  11. return c;
  12. }
  13. }
  14.  
  15.  
  16. char pause(){
  17. puts("Press any key...");
  18. return waitkey();
  19. }
  20.  
  21.  
  22. int confirm(char*s){
  23. int ok=0;
  24. puts(s);
  25. do{
  26. char c=waitkey();
  27. switch(c){
  28. default:
  29. continue;
  30. case 'y':
  31. case 'Y':
  32. ok=1;
  33. case 'n':
  34. case 'N':
  35. goto Done;
  36. }
  37. }while(1);
  38. Done:
  39. return ok;
  40. }
  41.  
  42.  
  43. void inputs(char*m,unsigned ml,char*s){
  44. unsigned l=0;
  45. --ml;
  46. puts(s);
  47. do{
  48. char c=waitkey();
  49. switch(c){
  50. default:
  51. if(l<ml){
  52. ++l;
  53. *m++=c;
  54. putch(c);
  55. }
  56. break;
  57. case 8:
  58. if(l){
  59. --l;
  60. *--m=0;
  61. putch(c);
  62. putch(' ');
  63. putch(c);
  64. }
  65. break;
  66. case 13:
  67. *m=0;
  68. putch('\n');
  69. return;
  70. }
  71. }while(1);
  72. }
  73.  
  74.  
  75. int main(){
  76. int a;
  77. char m[10];
  78. inputs(m,10,"Your name?");
  79. printf("Hello, %s!\n",m);
  80. a=confirm("Does it work?");
  81. printf("Answer=%d\n",a);
  82. pause();
  83. return 0;
  84. }

Report this snippet  

You need to login to post a comment.