0

I'm trying to create a function that asks the user for a value, which then will be stored as the max or min value entered, or if the user inputs a number < 0, it will exit the dataentry() function, else it will keep asking the user for input.

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

void dataentry();

int count = 0;
float max, min, mean, old, new;
float data;
char Old[10],Data[10],choice[25];

int main(void)
{
    dataentry();
}

void dataentry()
{
    printf("  |Enter Wind Speed Value:\n");
    printf("**|Enter -1 to exit data entry mode|**\n");

    fgets(Old, sizeof(Old), stdin);
    sscanf(Old, "%f",&old);

    max = old;
    min = old;
    data = 1;
    count = 1;

    printf("max=%f, min=%f, data=%f, count=%d.", max, min, data, count);

    for (count == 1;data >= 0; count++)
    {
        printf("\nEnter data value: ");

        //fgets(Data, sizeof(Data), stdin);  // I commented this out because I got a coredump error with it in
        sscanf(Data,"%f", &data);
        if (data >= max)
        {
            max = data;
        }
        else if (data <= min && data > 0)
        {
            min = data;
        }
    }
}

After the program prompts you the first time to enter data, before it reaches the for loop, it works and you enter your value. Then however it goes into an infinite loop printing "Enter data value: " over and over. I used the printf statement that prints out the max, min, data, and count values so I could check that they are being stored and they are, but when the function gets to the for loop it no longer does what I'm trying to do. Thank you in advance, this function is part of a larger program I'm writing but I cut all the irrelevant stuff out.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Mr. F
  • 7
  • 3
  • "I commented this out because I got a coredump error with it in" - and left yourself in a situation where you're trying to read from a `char` array you never initialized, that's not progress. – Crowman Oct 20 '14 at 02:19
  • see this one,you need to add "%n" in sscanf. http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops – oyss Oct 20 '14 at 02:21
  • You have an sscanf which is reading a value from a string. But You commented out the line which tried to read read a value into the string string from stdin. Fix that line rather than disabling it. Or obtain a non-negative value of data _somewhere_... – keshlam Oct 20 '14 at 02:22
  • Our professor just introduced us to sscanf() rather than using scanf() and I'm still new to exactly how it works so I was unaware that I was using an uninitialized string, thank you! – Mr. F Oct 20 '14 at 02:27
  • Your professor should be telling you the **first** thing you always do is *check the return result of all IO apis*. I can see taking a pass on the `printf` calls, but the `fgets` and `sscanf` calls should *all* be checked for validation rather than blindly assuming they worked. Assumption is the mother of all... – WhozCraig Oct 20 '14 at 09:38

1 Answers1

0

If you uncomment your fgets() line, it ought to work. Here's a working version, tidied up a little to check the return from the functions that you aren't monitoring, and to improve the logic of your loop:

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

void dataentry(void);

int count = 0;
float max, min, mean, old, new;
float data;
char Old[10], Data[10], choice[25];

int main(void)
{
    dataentry();
}

void dataentry(void)
{
    printf("  |Enter Wind Speed Value:\n");
    printf("**|Enter -1 to exit data entry mode|**\n");

    if ( !fgets(Old, sizeof(Old), stdin) ) {
        fprintf(stderr, "No input.\n");
        exit(EXIT_FAILURE);
    }

    if ( sscanf(Old, "%f", &old) != 1 ) {
        fprintf(stderr, "Badly formed input, enter a float next time.\n");
        exit(EXIT_FAILURE);
    }

    max = old;
    min = old;
    data = 1;
    count = 1;

    printf("max=%f, min=%f, data=%f, count=%d.\n", max, min, data, count);

    while (1) {
        printf("Enter data value: ");
        fflush(stdout);

        if ( !fgets(Data, sizeof(Data), stdin) ) {
            break;
        }

        if ( sscanf(Data, "%f", &data) != 1 ) {
            fprintf(stderr, "Badly formed input, enter a float next time.\n");
            exit(EXIT_FAILURE);
        }

        if ( data < 0 ) {
            break;
        }
        else {
            ++count;
        }

        if ( data >= max ) {
            max = data;
        } else if ( data <= min ) {
            min = data;
        }
    }

    printf("max=%f, min=%f, data=%f, count=%d.\n", max, min, data, count);
}

with sample output:

paul@thoth:~/src/sandbox$ ./ssf
  |Enter Wind Speed Value:
**|Enter -1 to exit data entry mode|**
10
max=10.000000, min=10.000000, data=1.000000, count=1.
Enter data value: 11
Enter data value: 12
Enter data value: 9
Enter data value: 8
Enter data value: -1
max=12.000000, min=8.000000, data=-1.000000, count=5.
paul@thoth:~/src/sandbox$ 

It's pretty bad form to use all those global variables, too. With your current program, at least, you could define every single one of them within dataentry().

Crowman
  • 25,242
  • 5
  • 48
  • 56