Problem with odd loop when passing float to function 
Author Message
 Problem with odd loop when passing float to function

Quote:

> First of all, I know this code is long and probably inefficient as
> hell, but I was just practicing and so didn't take the time to sit
> down and plan it out. Anyway, I stumbled on a run time error that I
> can't for the life of me figure out. Everything runs smoothly until
> the final call to getInput(), which asks for input of type float. When
> I input any number, rather than store the value in the variable and
> return to main() which should print it, it seems to loop back to the
> beginning of main() instead. As in it reprompts for a single character
> and then an integer and so on, until it gets to the float, at which
> point it loops back again.

Here is a slightly cleaner and working version.  Note the restriction
necessary on the "string" for your approach to work:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

enum {INT_NUM, LONG_NUM, FLOAT_NUM, SINGLE_CHAR, ALPHA_STRING,
ALPHA_NUMERIC};
char buff[128];

void getInput(const char *prompt, short in_type, void *ptr)
{

    int err_flag = 0;
    size_t i;
    int (*test_type) (int) = isdigit;
    char *nl;

    do {
        if (err_flag)
            printf("Invalid entry.\n");
        err_flag = 0;
        printf("%s ", prompt);
        fflush(stdout);
        fgets(buff, 128, stdin);
        if ((nl = strchr(buff, '\n')))
            *nl = 0;
        switch (in_type) {
        case INT_NUM:
        case LONG_NUM:
            test_type = isdigit;
            break;
        case FLOAT_NUM:
            for (i = 0; i < strlen(buff); i++) {
                if ((!test_type(buff[i])) &&
                    (buff[i] != '.'))
                    err_flag = 1;
            }
            break;
        case SINGLE_CHAR:
            if (strlen(buff) > 1) {
                err_flag = 1;
                break;
            }
            else
                test_type = isalpha;
            break;
        case ALPHA_STRING:  
            test_type = isalpha;
            break;
        case ALPHA_NUMERIC:
            test_type = isalnum;
            break;
        }

        if (in_type != FLOAT_NUM) {
            for (i = 0; i < strlen(buff); i++) {
                if (!test_type(buff[i]))
                    err_flag = 1;
            }
        }
    }
    while (err_flag);

    switch (in_type) {
    case INT_NUM:
        *(int *)ptr = atoi(buff);
        break;
    case LONG_NUM:
        *(long *)ptr = atol(buff);
        break;
    case FLOAT_NUM:
        *(float *)ptr = atof(buff);
        break;
    case SINGLE_CHAR:
        *(char *)ptr = buff[0];
        break;
    case ALPHA_STRING:
    case ALPHA_NUMERIC:
        strcpy((char *)ptr, buff);
        break;
    }

Quote:
}

int main(void)
{
    char love, hate[10];
    int stoopid;
    float crazy;

    getInput("Enter a single letter: ", SINGLE_CHAR, &love);
    printf("%c\n", love);
    memset(buff, 0, sizeof buff);
    getInput("Enter a whole number: ", INT_NUM, &stoopid);
    printf("%d\n", stoopid);
    memset(buff, 0, sizeof buff);
    getInput("Enter a string WITHOUT whitespace: ", ALPHA_STRING, hate);
    printf("%s\n", hate);
    memset(buff, 0, sizeof buff);
    getInput("Enter a floating point number: ", FLOAT_NUM,
             &crazy);
    printf("%f\n", crazy);

    return 0;

Quote:
}

--




Fri, 17 Aug 2001 03:00:00 GMT  
 Problem with odd loop when passing float to function
[...]

Quote:
> /* I'm assuming that the const in the *prompt declaration indicates that I'm
> passing a literal string to it, instead of a variable that contains a string.
> Is that correct? */

> > void getInput(const char *prompt, short in_type, void *ptr)

No.  It just represents a promise that getInput() will not modify
*prompt.  A decent compiler will issue diagnostics if your code does try
to modify it.

[...]

Quote:
> /* fflush() doesn't work on the system at my school. Lord only knows why */
> >         fflush(stdout);

That's a bummer.  fflush() is defined by the standard for output
streams.  It looks like your school should get a new compiler.

Quote:
> >         fgets(buff, 128, stdin);

> /* Let's see if I understand this part of the code. This finds the ending
> newline character and replaces it with the string terminator. If there is no
> newline char, then it leaves it like it is, assuming that in the absence of
> the newline, it must instead already end with the string terminator. Correct?
> */

This call of fgets() will fill buff with at most 127 characters followed
'\0'.  It terminates with the '\n' unless there is not enough room for
it.  So we remove the '\n' if it's there.

[...]

Quote:

> /* I haven't seen the memset() function before now, but I would guess it
> works like this: arg1 is string in memory that you want to work with. arg2 is
> the symbol you wish to replace each element with. arg3 is the size of each
> element in the string. Is this correct? Either way, I have been told that
> resetting the elements in buff is unecessary as long as I test fgets() return
> value when getting the input. */

> >     memset(buff, 0, sizeof buff);

Yes, you have it right.  To be crystal-clear:
memset(void *s, int c, size_t n)
copies the value of c, converted to an unsigned char, into each of the
first n characters of the object to which s points.

To be honest, I didn't bother to look at why you were zeroing buff.  It
is something I bother with.

--




Sat, 18 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem with odd loop when passing float to function

2. Passing float to dll problem

3. Homework Problem --- While Loop Need in a Function

4. FAQ 13.16 (was: Homework Problem --- While Loop Need in a Function)

5. problem passing unmanaged pointer from one managed member function to another

6. problem: conditional argument passing to a function

7. Problems passing arrays of Strings to function.

8. problems about passing struct to functions

9. Anwser: Problems passing a dynamic array of structures to a function

10. Problems passing a dynamic array of structures to a function

11. FAQ: Problems passing results from a function back, gcc and SunOS 4.1.4

12. pointer problem - passing contents back to calling function.

 

 
Powered by phpBB® Forum Software