Myio_c Library


myio_c.h
#ifndef _MYIO_C_H
#define _MYIO_C_H

#include <ctype.h>
#include <time.h>
#include <stdlib.h>


   /*****************************************************
   *                                                    *
   * File Name: myio_c.h                                *
   * Author: Brenda Sonderegger (all functions)         *
   * Contents:                                          *
   *    get_int()                                       *
   *    get_int_low()                                   *
   *    get_int_bound()                                 *
   *    get_float()                                     *
   *    get_float_bound()                               *
   *    get_float_low()                                 *
   *    get_random()                                    *
   *    my_gets()                                       *
   *                                                    *
   *****************************************************/  

   /*****************************************************
   * void get_int(int *value)                           *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Sept. 22, 1998                      *
   * Parameters: single address                         * 
   * Return value: none                                 *
   * Side Effect: puts an integer value into the memory *
   *              location specified by the address and *
   *              if an error occurs, the input stream  *
   *              is emptied                            *
   * Description: guarantees that the input value is an *
   *              integer.  Inputs of a, 23c, 2.4 are   *
   *              all considered to be illegal          *
   *****************************************************/
void get_int(int *value);



   /*****************************************************
   * void get_int_low(int *value, int low)              *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Sept. 16, 1998                      *
   * Parameters: single address, and single integer     * 
   * Return value: none                                 *
   * Side Effect: puts an integer value into the memory *
   *              location specified by the address that*
   *              if guaranteed to be greater than or   *
   *              equal to the low value                *
   * Description: guarantees that the input value is an *
   *              integer greater than or equal to low  *
   * Functions called: get_int()                        *
   *****************************************************/
void get_int_low(int *value, int low);



   /*****************************************************
   * void get_int_bound(int *value, int low, int high)  *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Sept. 16, 1998                      *
   * Parameters: single address, and two integers       * 
   * Return value: none                                 *
   * Side Effect: puts an integer value into the memory *
   *              location specified by the address that*
   *              if guaranteed to be greater than or   *
   *              equal to the low value and less than  *
   *              or equal to the high value            *
   * Description: guarantees that the input value is an *
   *              integer greater than or equal to low  *
   *              and less than or equal to high        *
   * Functions called: get_int()                        *
   *****************************************************/
void get_int_bound(int *value, int low, int high);
 

   /*****************************************************
   * void get_float(float *value)                       *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Sept. 22, 1998                      *
   * Parameters: single address                         * 
   * Return value: none                                 *
   * Side Effect: puts a float value into the memory    *
   *              location specified by the address and *
   *              if an error occurs, the input stream  *
   *              is emptied                            *
   * Description: guarantees that the input value is a  *
   *              float. Inputs of a, 23c, 2.4a are     *
   *              all considered to be illegal          *
   *****************************************************/
void get_float(float *value);



   /*************************************************************
   * void get_float_bound(float *value, float low, float high)  *
   * Author: Brenda Sonderegger                                 *
   * Last Modified: Sept. 16, 1998                              *
   * Parameters: single address, and two floats                 * 
   * Return value: none                                         *
   * Side Effect: puts a float value into the memory location   *
   *              specified by the address that is guaranteed to*
   *              be greater than or equal to the low value and *
   *              less than or equal to the high value          *
   * Description: guarantees that the input value is a float    *
   *              greater than or equal to low  and less than or*
   *              to high                                       *
   * Functions called: get_int()                                *
   *************************************************************/
void get_float_bound(float *value, float low, float high);
 

   /*****************************************************
   * void get_float_low(float *value, float low)        *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Sept. 22, 1998                      *
   * Parameters: single address and single float        * 
   * Return value: none                                 *
   * Side Effect: puts a float value into the memory    *
   *              location specified by the address and *
   *              if an error occurs, the input stream  *
   *              is emptied. The float value is greater*
   *              than or equal to low.                 *
   * Description: guarantees that the input value is a  *
   *              float. Inputs of a, 23c, 2.4a are     *
   *              all considered to be illegal. The     *
   *              value is greater than or equal to low *
   *****************************************************/
void get_float_low(float *value, float low);




   /*****************************************************
   * int get_random(int low, int high)                  *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Sept. 23, 1998                      *
   * Parameters: two integers                           * 
   * Return value: random integer between low and high  *
   * Side Effect: none                                  * 
   * Description: guarantees that the returned value is *
   *              between low and high inclusively      *
   * Functions called: from stdlib.h                    *
   *                       srand()                      *
   *                       rand()                       *
   *                   from time.h                      *
   *                       time()                       *
   *                                                    * 
   *****************************************************/
int get_random(int low, int high);



   /*****************************************************
   * void my_gets(char *strng, int max_char)            *
   * Author: Brenda Sonderegger                         *
   * Last Modified: Oct. 5, 1998                        * 
   * Parameters: single array, one integer              *
   * Return value: none                                 *
   * Side Effect: puts a string of a maximum of max_char*
   *              into the array and adds the null      *
   *              terminator to the string.  The array  *
   *              must be defined to be at least        *
   *              max_char + 1 in length                *
   * Description: guarantees that the input string is no*
   *              more than max_char in length excluding*
   *              the null terminator.  If the input is *
   *              more than max_char in length, an error*
   *              message is entered, the string is     *
   *              truncated, and the input stream is    *
   *              cleared.                              *
   *                                                    *
   *****************************************************/
void my_gets(char strng[], int max_char);

#endif


myio_c.c
#include "myio_c.h" /***************************************************** * * * File Name: myio_c.c * * Author: Brenda Sonderegger (all functions) * * Contents: * * get_int() * * get_int_low() * * get_int_bound() * * get_float() * * get_float_low() * * get_float_bound() * * get_random() * * my_gets() * * * *****************************************************/ /***************************************************** * void get_int(int *value) * * Author: Brenda Sonderegger * * Last Modified: Sept. 22, 1998 * * Parameters: single address * * Return value: none * * Side Effect: puts an integer value into the memory * * location specified by the address and * * if an error occurs, the input stream * * is emptied * * Description: guarantees that the input value is an * * integer. Inputs of a, 23c, 2.4 are * * all considered to be illegal * *****************************************************/ void get_int(int *value) { int iochar, error_check; do { error_check = scanf("%d", value); if (error_check != 1) { printf("\nError - not an integer. Try again. "); while ((iochar = getchar()) != '\n'); } else { iochar = getchar(); if (! isspace(iochar)) { printf("\nError - not an integer. Try again. "); while ((iochar = getchar()) != '\n'); error_check = 0; } } } while (error_check != 1); } /***************************************************** * void get_int_low(int *value, int low) * * Author: Brenda Sonderegger * * Last Modified: Sept. 16, 1998 * * Parameters: single address, and single integer * * Return value: none * * Side Effect: puts an integer value into the memory * * location specified by the address that* * if guaranteed to be greater than or * * equal to the low value * * Description: guarantees that the input value is an * * integer greater than or equal to low * * Functions called: get_int() * *****************************************************/ void get_int_low(int *value, int low) { do { get_int(value); if (*value < low) printf("\nValue must be >= %d. Try again.\n", low); } while (*value < low); } /***************************************************** * void get_int_bound(int *value, int low, int high) * * Author: Brenda Sonderegger * * Last Modified: Sept. 16, 1998 * * Parameters: single address, and two integers * * Return value: none * * Side Effect: puts an integer value into the memory * * location specified by the address that* * if guaranteed to be greater than or * * equal to the low value and less than * * or equal to the high value * * Description: guarantees that the input value is an * * integer greater than or equal to low * * and less than or equal to high * * Functions called: get_int() * *****************************************************/ void get_int_bound(int *value, int low, int high) { do { get_int(value); if ((*value < low) || (*value > high)) printf("\nValue must be between %d and %d, inclusive. Try again.\n", low, high); } while ((*value < low) || (*value > high)); } /***************************************************** * void get_float(float *value) * * Author: Brenda Sonderegger * * Last Modified: Sept. 22, 1998 * * Parameters: single address * * Return value: none * * Side Effect: puts a float value into the memory * * location specified by the address and * * if an error occurs, the input stream * * is emptied * * Description: guarantees that the input value is a * * float. Inputs of a, 23c, 2.4a are * * all considered to be illegal * *****************************************************/ void get_float(float *value) { int iochar, error_check; do { error_check = scanf("%f", value); if (error_check != 1) { printf("\nError - not a float. Try again. "); while ((iochar = getchar()) != '\n'); } else { iochar = getchar(); if (! isspace(iochar)) { printf("\nError - not a float. Try again. "); while ((iochar = getchar()) != '\n'); error_check = 0; } } } while (error_check != 1); } /***************************************************** * void get_float_low(float *value, float low) * * Author: Brenda Sonderegger * * Last Modified: Sept. 16, 1998 * * Parameters: single address, and single float * * Return value: none * * Side Effect: puts a float value into the memory * * location specified by the address that* * is guaranteed to be greater than or * * equal to the low value * * Description: guarantees that the input value is a * * float greater than or equal to low * * Functions called: get_float() * *****************************************************/ void get_float_low(float *value, float low) { do { get_float(value); if (*value < low) printf("\nValue must be >= %f. Try again.\n", low); } while (*value < low); } /************************************************************* * void get_float_bound(float *value, float low, float high) * * Author: Brenda Sonderegger * * Last Modified: Sept. 16, 1998 * * Parameters: single address, and two integers * * Return value: none * * Side Effect: puts a float value into the memory location * * specified by the address that is guaranteed to* * be greater than or equal to the low value and * * less than or equal to the high value * * Description: guarantees that the input value is a float * * greater than or equal to low and less than or * * equal to high * * Functions called: get_float() * *************************************************************/ void get_float_bound(float *value, float low, float high) { do { get_float(value); if ((*value < low) || (*value > high)) printf("\nValue must be between %f and %f, inclusive. Try again.\n", low, high); } while ((*value < low) || (*value > high)); } /***************************************************** * int get_random(int low, int high) * * Author: Brenda Sonderegger * * Last Modified: Sept. 23, 1998 * * Parameters: two integers * * Return value: random integer between low and high * * Side Effect: none * * Description: guarantees that the returned value is * * between low and high inclusively * * Functions called: from stdlib.h * * srand() * * rand() * * from time.h * * time() * * * *****************************************************/ int get_random(int low, int high) { static int seeded = 0; int random; if (seeded == 0) { srand((unsigned)time((time_t *) NULL)); seeded = 1; } random = rand() % (high - low + 1) + low; return (random); } /***************************************************** * void my_gets(char *strng, int max_char) * * Author: Brenda Sonderegger * * Last Modified: Oct. 5, 1998 * * Parameters: single array, one integer * * Return value: none * * Side Effect: puts a string of a maximum of max_char* * into the array and adds the null * * terminator to the string. The array * * must be defined to be at least * * max_char + 1 in length * * Description: guarantees that the input string is no* * more than max_char in length excluding* * the null terminator. If the input is * * more than max_char in length, an error* * message is entered, the string is * * truncated, and the input stream is * * cleared. * * * *****************************************************/ void my_gets(char strng[], int max_char) { int iochar, check, index = 0; /*code not included for this one*/ }