Could somebody please help me with this program. This is my first time working with an EOF loop. This program simply adds up all the input numbers. My task is to have the last wanted input terminated by an EOF. However, when I run my program, my loop is infinite. Can someone please help me? I would really appreciate it.

/*  A program to compute the sum of numbers given on the input  */

#include <stdio.h>

int main()
{
  /* declarations */
 float x;
 float y;
 float sum;
 float number;

        /*  Initialize the sum             */


        /*  Get the first input            */
        printf("Enter the first input (EOF to end): \n");
        number = scanf("%f", &x);

        /*  While there is more input      */
        while (number != EOF)
        {
                 /*  Accumulate the sum     */
                 sum = x + sum;

                /*  Get the next input     */
                printf("Enter the next input (EOF to end): \n");

                number = scanf("%f", &x);

        }

        /*  Print the results    */
        printf("sum = %f\n", sum);
}

I got it to run fine as is on my machine:

Enter the first input (EOF to end):
10
Enter the next input (EOF to end):
10
Enter the next input (EOF to end):
10
Enter the next input (EOF to end):
30
Enter the next input (EOF to end):
^Z
sum = 60.000000

What are you using for EOF? On Windows it's CTRL-Z (or F6) on *nix it's CTRL-D.

A clarity suggestion:

You are using a variable names number to control the loop. But number is not a number. It is an EOF indicator. I recommend all variables be named something that indicates their real use -- in this case maybe indicateEOF or EOFentered. Something like that.

thank you jonsca, I realized I had been pressing the wrong buttons for EOF
However, why is your output different from mines?
I entered the same input and this is what I received:

Enter the first input (EOF to end): 
10
Enter the next input (EOF to end): 
10
Enter the next input (EOF to end): 
10
Enter the next input (EOF to end): 
30
Enter the next input (EOF to end): 
sum = 4.000000

Looks like a case of two different programs. Repost your code.

A clarity suggestion:

You are using a variable names number to control the loop. But number is not a number. It is an EOF indicator. I recommend all variables be named something that indicates their real use -- in this case maybe indicateEOF or EOFentered. Something like that.

Good suggestion regarding variable names. It's neither here nor there, but it seems like in all but the last iteration of the loop, number is a number. [TEX]\sout{"input" or something similar would be a good name.}[/TEX]

Good suggestion regarding variable names. It's neither here nor there, but it seems like in all but the last iteration of the loop, number is a number. "input" or something similar would be a good name.

No, number is an indicator. If you wish to be pedantic, all values in a computer are numbers -- even strings. But in this program number is used to indicate an error condition.

No, number is an indicator. If you wish to be pedantic, all values in a computer are numbers -- even strings. But in this program number is used to indicate an error condition.

I will admit that on my first hasty read though I had mixed up number and X as the variable holding the user's input number. However, number is the return value of scanf, which is "the number of fields successfully converted and assigned." In all but the last iteration of the loop, number contains the number of fields successfully converted and assigned, as per the scanf documentation. number is a number, and not in the "oh, everything in computers is all 1s and 0s so everything is a number" sense.

Regardless, my suggestion of input is wrong but number seems to be a perfectly suitable name for the variable.

Fine.... As you wish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.