Pregunta

I have the following program:

#include <stdio.h>      // printf, scanf definitions //
#include <math.h>       // sqrt function //


int main(void)
{
    double number;      // input- a number of a set //
    double avg;         // output - average //
    double sdev;        // output - standard deviation //
    double numSq;       // output - input squred //

    int count = 0;      // number of numbers counter //
    int status;         // status of scanf function //
    avg = 0;
    numSq = 0;

    do{
        status = scanf("%lf", &number);

        // Testing Purposes //
       // printf("%f\n", number);

        if (status == 1 && number !=EOF)
        {
            if ( number >=0)
            {

            avg += number;
            numSq += number * number;
            count +=1;
            }
            else if( number <0)
            {
                avg -= number;
                numSq += (number * number);
                count +=1;
            }
        }
        // Make an else for negative numbers and if scanf fails //
       }
       while (number !=EOF );

       avg = avg / count;
       numSq = numSq / count;  
       sdev = sqrt(numSq - (avg * avg));

       printf("The average is %.4f \n", avg);
       printf("The standard deviation is %.4f \n", sdev);

       return 0;
}

I also have a text file called data.txt that contains:

1.0
1.2 
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0

After program compilation, I run:
./standard < data.txt > results.txt
Yet nothing happens! A blank terminal with a flashing dot as this picture shows:

My guess is that the program is stuck in the loop, but why? And how would I fix this?

¿Fue útil?

Solución 2

Your comparisons between number and EOF don't make sense, scanf will not set number to any specific value if the end of file is reached. Instead, it will return EOF.

So what you should be testing to determine the end of file is status == EOF.

In fact, you should probably break out of the loop if status != 1 since that would mean either end of file, or invalid input was provided. Try and do proper error reporting in the latter case.

Otros consejos

1.We can find out the location end of file but can not retrieve the value of end of file. 2.We can use the return status of scanf family of function.In your case we should run the loop status!=-1. 3.we can read the input of redirected file using scanf. 4.following codes may help you.

#include <stdio.h>      // printf, scanf definitions //
#include <math.h>       // sqrt function //


int main(void)
{
    double number=0;      // input- a number of a set //
    double avg;         // output - average //
    double sdev;        // output - standard deviation //
    double numSq;       // output - input squred //

    int count = 0;      // number of numbers counter //
    int status;         // status of scanf function //
    avg = 0;
    numSq = 0;
        FILE *fp=NULL;
        fp=fopen("data.txt","r");
    do{
        //status = scanf("%lf", &number);
        status=fscanf(fp,"%lf", &number);
        printf("status:%d number:%lf\n",status,number);
        sleep(1);

        // Testing Purposes //
       // printf("%f\n", number);

        if (status == 1 && number !=EOF)
        {
            if ( number >=0)
            {

 avg += number;
            numSq += number * number;
            count +=1;
            }
            else if( number <0)
            {
                avg -= number;
                numSq += (number * number);
                count +=1;
            }
        }
        // Make an else for negative numbers and if scanf fails //
       }
       while (status !=-1 );
        avg = avg / count;
       numSq = numSq / count;
       sdev = sqrt(numSq - (avg * avg));

       printf("The average is %.4f \n", avg);
       printf("The standard deviation is %.4f \n", sdev);

       return 0;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top