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.

4

1 回答 1

0

如果你取消注释你的fgets()行,它应该可以工作。这是一个工作版本,进行了一些整理以检查您未监控的函数的返回,并改进循环的逻辑:

#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);
}

带有样本输出:

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$ 

使用所有这些全局变量也是一种非常糟糕的形式。至少,使用您当前的程序,您可以在dataentry().

于 2014-10-20T02:34:24.363 回答