# Thread: Help with basic program

1. ## Help with basic program

Hello,
I am trying to write a program where you enter an undefined amount of numbers and when you type in -1 it will output the average of the numbers you have entered. I am having trouble figuring out how to keep storing a new number. I figure I need a variable for the amount of numbers entered, and one for each of the numbers entered but it has to be unlimited.
Here is what i have so far and I don't know if I have the right idea. it doesnt work and when I run it I get runtime errors.
Code:
```#include <stdio.h>
#include <math.h>

int main(void)
{
int n,newnumber,average,count=0,total;
printf("enter numbers: \n");
while (n != -1, newnumber != -1)
{
scanf_s("%lf",&n);
scanf_s("%lf",&newnumber);
total=n+newnumber;
count++;
}
if (n==-1,newnumber==-1)
average=total/count;
printf("average is: %i",average);
getchar();
return 0;
}```

2. Instead of keeping track of each number entered, just keep track of a running sum:
Code:
```sum = 0
numbers_entered = 0
loop until input number is -1
{
input number from user
if number is not -1
{
increment numbers_entered
}
}
print sum divided by numbers_entered```

3. ah okay I new it would be something simple!

thank you

4. Hi,
well I am stuck on one more problem. I have to read a text file numbers.txt and list how many even numbers there are in the file. I think I might be over complicating things again everything comes out fine except the for (even_test=0) says i need ; before ) i tried a lot of things but i think if I can get that line working my code would be fine.
I would really appreciate any help,
Thank you
(the numbers.txt is just a plain list of random numbers no order)
Code:
```#include <stdio.h>
#include <math.h>
#define FILENAME "numbers.txt"
int main (void)
{
/* Declare variables */
int file_numbers,number_even,num_data,even_test;
number_even = 0;
FILE *numbers;
numbers = fopen(FILENAME,"r");
while ((fscanf(numbers,"%lf",&file_numbers)) == 2)
{
num_data++;
if (num_data == 1)
{
even_test = file_numbers%2;
}
for (even_test; even_test=0; number_even++)

printf("the amount of even numbers are: %i",number_even);
}
getchar();
getchar();
return 0;
}```
Here is an update of what ive changed, still having problems hopefully im getting closer!

Code:
```#include <stdio.h>
#include <math.h>
#define FILENAME "numbers.txt"
int main (void)
{
/* Declare variables */
int file_numbers,number_even,even_test;
number_even = 0;
FILE *numbers;
numbers = fopen(FILENAME,"r");
while ((fscanf(numbers,"%lf",&file_numbers)) == 2)
{
even_test = file_numbers%2;

if (even_test = 0)

number_even++;

}
printf("the amount of even numbers are: %i",number_even);
getchar();
getchar();
return 0;

}```

5. I'm not sure what your goal is with num_data. You're on the right track with the % operator, but for loops are designed for... looping. I believe you want something more like:
Code:
```if(even_test)
{
// Increment the even number counter
}```
No looping necessary. Also, your printf statement should be after the while loop if you want to display the total after reading the entire file.

6. [QUOTE=brettski900;1007478]
Code:
`if (even_test == 0)`
Add another = sign in that if check. A single = is for assigning, a double == is for testing equality. The way you had it, the if check always sets even_test to zero and never enters the body of the if statement.

7. Also, your fscanf is incorrect. First, you have to use %d for integers. Second, it returns the number of successful conversions. You are asking it to convert one thing, so the most it can return is 1. Change the while loop to something like:
Code:
`while ((fscanf(numbers, "%d", &file_numbers)) == 1) {`

8. Code:
```while ((fscanf(numbers,"%lf",&file_numbers)) == 2)
{
num_data++;
if (num_data == 1)
{
even_test = file_numbers%2;
if (even_test = 0)
number_even++;
printf("the amount of even numbers are: %i",number_even);
}
}```
Couple of things ... you defind numbers as an int, but you are scanning for doubles...
You should probably use ...
Code:
`while (fscanf("%d%", &file_numbers))`
The loop wil exit the first time fscanf() fails to convert a value.

Second your odd/even test can be simplified like this...
Code:
`number_even +=  ((file_numbers & 1) == 0) ? 1 : 0;`
Lastly your results line should probably be outside the loop unless you want to see it counting each number in turn...

9. Thank you guys so much i took your advice and i made the program work! here it is for future reference.
Code:
```#include <stdio.h>
#include <math.h>
/* define the file */
#define FILENAME "numbers.txt"
int main (void)
{
/* Declare variables */
int file_numbers,number_even,even_test;
/* set number_even to 0 */
number_even = 0;
/* FILE is set to numbers */
FILE *numbers;
numbers = fopen(FILENAME,"r");
/* Loop read data if the function is read it will have a value of 1
if it does not have a value of 1, end loop*/
while ((fscanf(numbers,"%d",&file_numbers)) == 1)
{
/* divide the file_numbers by 2 */
even_test = file_numbers%2;
/* if the remainder is 0 then the number is even! */
if (even_test == 0)
/* add 1 to number_even */
number_even++;

}
/* print the number_even which will display the amount of even numbers */
printf("the amount of even numbers are: %i",number_even);
getchar();
getchar();
return 0;

}```
Thanks again!
Brett