# what am i doing wrong?

This is a discussion on what am i doing wrong? within the C Programming forums, part of the General Programming Boards category; So here's my problem: Write a program that reads a list of integers from thekeyboard and creates the following information: ...

1. ## what am i doing wrong?

So here's my problem:
Write a program that reads a list of integers from thekeyboard and creates the following information:
a. Finds and prints the sum and average of the integers
b. Finds and prints the largest and the smallest integer
c. Prints a Boolean (true or false) if some of them are less than 20
d. Prints a Boolean (true or false) if all of them are between 10 and 90
The input data consist of a list of integers with a sentinel. The program must prompt the user to enter the integers, one by one, and enter the sentinel when the end of the list has been reached (represented by 99999).

Here's my code:

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
/*Local Definitions*/
int num;
int sum = 0;
int larg = 0;
int smal = 99998;
int less = 0;
int tween = 0;
int count = 0;
long avg;

/*Statements*/
printf("Enter numbers with <return> (99999 to stop):");
while (scanf("%d", &num)!= 99999);
count++;
sum += num;
avg = sum / count;
{
if (num < smal);
{
smal = num;
}

if (num > larg);
{
larg = num;
}

if (num < 20);
{
less = 1;
}

if (10 < num < 90);
{
tween = 1;

}
printf("The number of integers is: \t%d", count);
printf("The sum of the integers is: \t%d", sum);
printf("The average of the integers is: \t%d", avg);
printf("The smallest integer is: \t%d", smal);
printf("The largest integer is: \t%d", larg);
if (less = 1)
{
printf("At least one number was < 20:   True");
}
else printf("At least one number was < 20:   False");
if (tween = 1)
{
printf("All numbers were (10 <= n <= 90): True");
}
else printf("All numbers were (10 <= n <= 90):  False");

system("PAUSE");
return 0;
}```
I am getting a "parse error at end of input" message, and I can't figure out why. Any help would be appreciated. Thanks
melee

2. Code:
`while (scanf("%d", &num)!= 99999);`
Beware of the semi-colon!

Edit: Also your opening { after that line should be moved up higher (like inplace of the aforementioned semi-colon) and you seem to be missing a closing }

3. Originally Posted by melee
Code:
`while (scanf("%d", &num)!= 99999);`
Also, scanf returns the number of arguments that it successfully converted/processed so the above will not do what you want it to. You probably want to do something like this:

Code:
```scanf("%d",&num);
while (num != 99999)
{
// Put your code that you want to loop through here
scanf("%d",&num);
}```
Originally Posted by melee
Code:
`if (10 < num < 90);`
Most of your if statements (except for the last couple) suffer from an improperly trailing semicolon. Get rid of them! Also you can't test the value of num like you are doing above. You should probably set this value to true initially and then in the loop set it to false if you encounter any value outside this range:

Code:
```int tween = 1;
...
if( num <= 10 || num >= 90 )
tween = 0;```

4. Yes - placing a colon right after a loop like that will cause the loop to do absolutely nothing - it will cycle again one it hits either a semicolon or a { } set.

5. Ok, here's my (slightly) inproved code:

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
/*Local Definitions*/
int num;
int sum = 0;
int larg = 0;
int smal = 99998;
int less = 0;
int tween = 1;
int count = 0;
long avg;

/*Statements*/
printf("Enter numbers with <return> (99999 to stop):");
scanf("%d", &num);
while  (num != 99999)
{
count++;
sum += num;
avg = sum / count;
if (num < smal)
{
smal = num;
}

if (num > larg)
{
larg = num;
}

if (num < 20)
{
less = 1;
}
if( num <= 10 || num >= 90 )
{
tween = 0;
}
}
printf("The number of integers is: \t%d", count);
printf("The sum of the integers is: \t%d", sum);
printf("The average of the integers is: \t%d", avg);
printf("The smallest integer is: \t%d", smal);
printf("The largest integer is: \t%d", larg);
if (less = 1)
{
printf("At least one number was < 20:   True");
}
else printf("At least one number was < 20:   False");
if (tween = 1)
{
printf("All numbers were (10 <= n <= 90): True");
}
else printf("All numbers were (10 <= n <= 90):  False");

system("PAUSE");
return 0;
}```

I got rid of the trailing semicolon's after the if statements, I changed the if (10 < num < 90); statement to if( num <= 10 || num >= 90 ) and changed the initialization of tween to true as opposed to false.
I also moved the leading { up a few lines to include the first couple statements. I now compile fine, but the program stops after the first number is input. No output, and I can't input another number. There is something about this problem that just won't work itself out in my head.
melee

6. Ok you need to have a scanf() at the end of the loop to get the next number.

Also
Code:
`avg = sum / count;`
Needs to go outside the loop

7. Move your initial printf and scanf inside the while loop.

After you input the first number, the while loop is endless since there is no opportunity for num == 9999.

8. ok, almost got it.

Code:
```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
/*Local Definitions*/
int num;
int sum = 0;
int larg = 0;
int smal = 99998;
int less = 0;
int tween = 1;
int count = 0;
long avg;

/*Statements*/
printf("Enter numbers with <return> (99999 to stop):");
scanf("%d", &num);
while  (num != 99999)
{
count++;
sum += num;

if (num < smal)
{
smal = num;
}

if (num > larg)
{
larg = num;
}

if (num < 20)
{
less = 1;
}
if( num <= 10 || num >= 90 )
{
tween = 0;
}
scanf("%d", &num);
}
avg = sum / count;
printf("The number of integers is: \t\t%d\n", count);
printf("The sum of the integers is: \t\t%d\n", sum);
printf("The average of the integers is: \t%d\n", avg);
printf("The smallest integer is: \t\t%d\n", smal);
printf("The largest integer is: \t\t%d\n", larg);
if (less = 1)

printf("At least one number was < 20:   \tTrue\n");

else printf("At least one number was < 20:   \tFalse\n");
if (tween = 1)

printf("All numbers were (10 <= n <= 90): \tTrue\n");

else printf("All numbers were (10 <= n <= 90):  \tFalse\n");

system("PAUSE");
return 0;
}```
The program works fine except it always prints "At least one number was < 20: True" and "All numbers were (10 <= n <= 90): True" even if the numbers were not true in those cases. Something wrong with my
Code:
```                      if (less = 1)

printf("At least one number was < 20:   \tTrue\n");

else printf("At least one number was < 20:   \tFalse\n");
if (tween = 1)

printf("All numbers were (10 <= n <= 90): \tTrue\n");

else printf("All numbers were (10 <= n <= 90):  \tFalse\n");```
segment?
melee

9. Look closely at
Code:
`if (less = 1)`
and
Code:
`if (tween = 1)`

10. ahh. <-punches self in head
Thank you Scribbler, Thantos, sean_mackrory, and hk_mp5kpdw!!!