raymondtheboss
raymondtheboss

Reputation: 31

Check to see if scanf is not a float

#define maximum 100 
#include <math.h>
#include <stdio.h>

int main () {

    float sum, mean, variance, difference;
    float sumforvariance, standarddev;
    sumforvariance=0;
    sum=0;
    mean=0;
    variance=0;
    difference=0;
    standarddev=0;

    int a, count, b, c;
    float insertnum[maximum]

    for (a=0; a<maximum; a++) {
            scanf("%f",&insertnum[a]);
            count ++;

            if (insertnum[a]==35.00) {

                    if (count==1) {
                            printf ("no data\n");
                            return 0;
                    }
                    break; 

                }

    }

    for (b=0; b<count; b++) {
            sum+=insertnum[b];
    }
    mean=sum/count;

    for (c=0; c<count; c++) {
            difference=insertnum[c]-mean;
            sumforvariance=sumforvariance+pow(difference,2);
    }
    variance=variance/count;
    standarddev=sqrt(variance);

    printf("mean: %f",mean);
    printf("standdev: %f",standarddev);

Hi so I have a simple question. I am trying to calculate a standard deviation and mean for a set of numbers like this ./a.out 12 20 30 etc #
The # is to terminate inputing more numbers. As you can see in the first for loop, I am trying to input the numbers from standard output into an array of floats. The problem is when I enter 35, I do not want to terminate inputting more numbers because its not equal to #. How am I able to enter 35 and continue to enter more numbers until I enter # since they both contain the same numerical value. #=35 and 35=35.

Upvotes: 0

Views: 76

Answers (2)

selbie
selbie

Reputation: 104514

Read you user input in as a string. Sniff for the terminating condition, then convert from string to float. Using the helper function, strtof which is available from #include <stdlib.h>

#define maximum 100 
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

int main() {

    float sum, mean, variance, difference;
    float sumforvariance, standarddev;
    sumforvariance = 0;
    sum = 0;
    mean = 0;
    variance = 0;
    difference = 0;
    standarddev = 0;

    int a, count=0, b, c;

    float insertnum[maximum];

    for (a = 0; a < maximum; a++) {

        char userinput[101] = {0};
        userinput[0] = '\0';

        scanf("%100s", userinput);

        if (userinput[0] == '#')
        {
            break;
        }
        insertnum[count] = strtof(userinput, nullptr);
        count++;
    }
    return 0;
}

Also, you forgot to initialize count. And your code was inserting the # read into the array as well. So I fixed that for you.

Aside - I'll never forget the day my computer science professor passionately screamed to the class about the dangers of "reading numbers" from input. Users type characters with the keyboard not numbers. Hence, "validating input" became engrained with me to this day. You might want to consider just letting your loop break whenever the user types anything not a number. A modified version of the loop as follows:

for (a = 0; a < maximum; a++) {
    char userinput[101] = {0};
    userinput[0] = '\0';
    scanf("%100s", userinput);
    char* endptr = NULL;

    float f = strtof(userinput, &endptr);
    if (userinput == endptr)
    {
        // whatever was typed was not a float
        break;
    }
    insertnum[count] = f;
    count++;
}

Upvotes: 3

Chris Dodd
Chris Dodd

Reputation: 126193

Check the return value of scanf -- when it successfully converts a float it will return 1 (or more generally, however many conversions in the format string succeeded). So when the input is #, it will return 0 (nothing converted) and leave the # on in the input stream. You can then check the next character to make sure its a #. So you end up with a loop like:

for (a=0; a<maximum && scanf("%f",&insertnum[a]) == 1; a++) {
    ++count;
}

or even

for (count=0; count < maximum && scanf("%f",&insertnum[count]) == 1; ++count);
if (count == maximum) {
    // read the limit -- may be more data
} else {
    if (getchar() == '#') {
        // got the expected terminator
    } else {
        // something else caused a problem

Upvotes: 1

Related Questions