/*
 *             getval.c
 *
 *
 *       Returns a real number 
 */

#include "defn.h"
#include <stdio.h> 

#define MAXBUF 20
#define ERROR "Two or more decimal places in a number"

float
getval()
{
    char    valbuf[MAXBUF];
    char    *ptval;
    double  value;
    int     DECIMAL = 0;
    int     i;

    /* Null the value buffer "valbuf" */
    for (i = 0; i < MAXBUF; i++)
	valbuf[i] = '\0';

    i = 0;

    ch = getc(stream);

    while (((ch <= ' ') || (ch == ',')) && ch != EOF)/* Get rid of seperators */
	ch = getc(stream);

    while ((ch >= '0' && ch <= '9') || ch == '.' || ch == '-' || ch == '+')
    {
	if (ch == '.')
	{
	    if (DECIMAL)
	    {
		fprintf(stderr,"Error: %s\n", ERROR);
		exit(1);
	    }
	    DECIMAL = 1;
	}
	valbuf[i++] = ch;
	ch = getc(stream);

    }
    while (((ch <= ' ') || (ch == ',')) && ch != EOF)/* Get rid of seperators */
	ch = getc(stream);
    ungetc(ch, stream);			/* Put non numeric char back */
    ptval = &valbuf[0];
    value = atof (ptval);
    return (float)(value);
}
