home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / mt / inputf.c < prev    next >
C/C++ Source or Header  |  1989-01-12  |  5KB  |  156 lines

  1. /* inputf.c   functions for prompting user input */
  2. /* `MIDI Sequencing In C', Jim Conger, M&T Books, 1989 */
  3.  
  4. #include <conio.h>      /* compiler library module headers */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. #include "standard.h"           /* header files */
  10. #include "screenf.h"
  11.  
  12.  
  13. /* Get floating point no. from user.  Return enters value, ESC quits. */
  14. /* Prompt string is printed on line number lineno with norm_attrib. */
  15. /* If input is not >= min or <= max, error message is printed on */
  16. /* lineno with emph_attrib, and user is given another try. */
  17. /* Returns 1 if number was input, 0 if process was escapted. */
  18. int
  19. getfloat(int lineno, char *prompt, float *value, float min, float max, 
  20.          int norm_attrib, int emph_attrib)
  21. {
  22.     char input[20], minbuf[20], maxbuf[20], message[SCRNWIDE];
  23.     int n;
  24.     float tempval;
  25.  
  26.     while(1){
  27.         while(kbhit()) 
  28.             getch();        /* clear any stray keypress */
  29.         clearline(lineno, norm_attrib);
  30.         writeword(prompt, 1, lineno, norm_attrib);
  31.         n = get_string(input, 19);
  32.         if (n == 0)
  33.             return(0);
  34.         tempval = atof(input);
  35.  
  36.         if(tempval > max || tempval < min){
  37.             gcvt(min, 3, minbuf);
  38.             gcvt(max, 3, maxbuf);
  39.             strcpy(message, "Value must be between ");
  40.             strcat(message, minbuf);
  41.             strcat(message, " and ");
  42.             strcat(message, maxbuf);
  43.             writerr(message, lineno, norm_attrib, emph_attrib);
  44.         }
  45.         else{
  46.             *value = tempval;
  47.             return(1);
  48.         }
  49.     }
  50.     return(0);
  51. }
  52.  
  53.  
  54. /* Get integer value from user.  Return enters value, ESC quits. */
  55. /* Prompt string is printed on line number lineno with norm_attrib. */
  56. /* If input is not >= min or <= max, error message is printed on */
  57. /* lineno with emph_attrib, and user is given another try. */
  58. /* Returns 1 if number was input, 0 if process was escapted. */
  59. int
  60. getint(int lineno, char *prompt, int *value, int min, int max, 
  61.     int norm_attrib, int emph_attrib)    
  62. {
  63.     char input[20], minbuf[20], maxbuf[20], message[SCRNWIDE];
  64.     int tempval, n;
  65.  
  66.     while(1){
  67.         while(kbhit()) 
  68.             getch();        /* clear any stray keypress */
  69.         clearline(lineno, norm_attrib);
  70.         writeword(prompt, 1, lineno, norm_attrib);
  71.         n = get_string(input, 19);
  72.         if (n == 0)
  73.             return(0);
  74.         tempval = atoi(input);
  75.  
  76.         if(tempval > max || tempval < min){
  77.             itoa(min, minbuf, 10);
  78.             itoa(max, maxbuf, 10);
  79.             strcpy(message, "Value must be between ");
  80.             strcat(message, minbuf);
  81.             strcat(message, " and ");
  82.             strcat(message, maxbuf);
  83.             writerr(message, lineno, norm_attrib, emph_attrib);
  84.         }
  85.         else{
  86.             *value = tempval;
  87.             return(1);
  88.         }
  89.     }
  90.     return(0);
  91. }
  92.  
  93.  
  94. /* Get input string of no more than max chars from user */
  95. /* Prompt string is printed on line number lineno with norm_attrib. */
  96. /* Returns number of characters input. */
  97. int
  98. getstr(int lineno, char *prompt, char *string, int max, int attrib)
  99. {
  100.     int n;
  101.     
  102.     clearline(lineno, attrib);
  103.     while(kbhit()) 
  104.         getch();
  105.     writeword(prompt, 1, lineno, attrib);
  106.     n = get_string(string, max);
  107.     clearline(lineno, attrib);
  108.     return(n);
  109. }
  110.  
  111.  
  112. /* Clean version of fgets(),  Returns no. of chars read, up to max.*/ 
  113. /* Returns 0 for just ENTER or if ESC is hit at any time. */
  114. int
  115. get_string(char *str, int max)
  116. {
  117.     int i, c;
  118.     
  119.     i = 0;
  120.     while (1) {
  121.         c = getch();
  122.         if (c == ESC || (c == '\n' && i == 0)){
  123.             *str = '\0';
  124.             return(0);
  125.         }
  126.         else if (c == 0)        /* ignore arrow or function keys */
  127.             getch();
  128.         else if (c == '\r')     /* do not put a CR in string */
  129.             break;
  130.         else if (c == BACKSP){  /* rub out on back space */
  131.             if (i > 0){
  132.                 *(--str) = ' ';
  133.                 i--;
  134.                 putch(BACKSP);
  135.                 putch(' ');
  136.                 putch(BACKSP);
  137.             }
  138.         }
  139.         else if (iscntrl(c))
  140.             ;                   /* do not process other control chars */
  141.         else{
  142.             if (i < max){       /* do not advance past max chars */
  143.                 i++;
  144.             }
  145.             else{
  146.                 str--;          /* just overwrite last char position */
  147.                 putch(BACKSP);
  148.             }
  149.             putch(c);
  150.             *str++ = c;         
  151.         }
  152.     }
  153.     *str = '\0';
  154.     return(i);
  155. }
  156.