Return to Snippet

Revision: 14982
at June 19, 2009 03:11 by Zufolek


Initial Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


char waitkey(){
 while(!kbhit())_sleep(0);
 {
  char c=getch();
  if(c==27)exit(-1);
  return c;
 }
}


char pause(){
 puts("Press any key...");
 return waitkey();
}


int confirm(char*s){
 int ok=0;
 puts(s);
 do{
  char c=waitkey();
  switch(c){
   default:
   continue;
   case 'y':
   case 'Y':
   ok=1;
   case 'n':
   case 'N':
   goto Done;
  }
 }while(1);
 Done:
 return ok;
}


void inputs(char*m,unsigned ml,char*s){
 unsigned l=0;
 --ml;
 puts(s);
 do{
  char c=waitkey();
  switch(c){
   default:
   if(l<ml){
    ++l;
    *m++=c;
    putch(c);
   }
   break;
   case 8:
   if(l){
    --l;
    *--m=0;
    putch(c);
    putch(' ');
    putch(c);
   }
   break;
   case 13:
   *m=0;
   putch('\n');
   return;
  }
 }while(1);
}


int main(){
 int a;
 char m[10];
 inputs(m,10,"Your name?");
 printf("Hello, %s!\n",m);
 a=confirm("Does it work?");
 printf("Answer=%d\n",a);
 pause();
 return 0;
}

Initial URL


Initial Description
windows

Initial Title
console string input, confirm, pause

Initial Tags


Initial Language
C