# Thread: i have infinite loops. why?

1. ## i have infinite loops. why?

#include <stdio.h>
#include <conio.h>

int main()
{

int answer, num, count = 0, total = 0, max = 0;
float average;

do
{
clrscr();
printf("Enter an integer: ");
scanf("%i", &num);

while ( count < 3 && num > 0 )
{
count=count + 1;
total += num;
if ( num > max )
{
max = num;

}

printf("Enter next integer: ");
scanf("%i", &num);
}

if ( count > 0 )
{
printf("\n");
printf("The maximum number is %i\n", max);
printf("the count is %i\n", count);
printf("the total is %i\n", total);
average= (float)total / count;
printf("the average in two decimal point is %.2f\n", average);
}

else
printf("No valid number were entered\n");
printf("Another run? Y/N ");
} while ( answer != 'N');
return(0);
}

i have problems doing the while loop! help me out if possible . .

thanks mate

just need a click in the head

3. Aha, I've seen this before. This is one little disadvantage of using scanf the wrong way. There are a lot of discussions about what's the best way to read from input (gets, fgets, scanf, etc). Try replacing:
Code:
`scanf("%i", &answer);`
by:
Code:
`answer = getchar();`

4. i dont think is scanf , tried, but still faulty . .

im suppose to prompt the user for Y/N?
if yes[y], re-run the whole program,
if no [ N ] exit;

5. I've tried your program and it works fine if I replace the scanf with getchar (for reading the answer).

b.t.w. you can also consider using the toupper function so the program also accept 'n' (lowercase).

Code:
`while ( toupper(answer) != 'N');`

7. I wasn't going to get involved, as I normally skip over code postings that lack [code] tags, but here are some hints:[list=1][*]If the user indicates s/he wants another run, reinitialize your count variable (it still holds the value from the last run).[*]scanf() is leaving a carriage return in the input buffer when you ask the user if they want another turn. This carriage return causes the next scanf() ("Enter an integer") to return early.[/list=1]If that helps in any way, you can thank me by using [code] tags in the future ;)

8. >scanf("%i", &num);

Shouldn't this be

scanf("%d", &num);

Put some extra printf's in your code to check the loops.

9. Oh what a mish-mash of half truths we have here....

10. Originally posted by Salem
Oh what a mish-mash of half truths we have here....
That is my biggest problem with this board. There is alot of people answearing things they don't really know. As a rule of thumb "Always check the replies you get in here with a book or some reliable source" There is only a hand full of people I really trust to know what they are talking about in here.

11. >There is alot of people answearing things they don't really know.
That I can tolerate, that is why these are called discussion forums. Anyone can answer and if they are wrong they will be corrected in short order.

>Oh what a mish-mash of half truths we have here....
Quite.

>>scanf("%i", &num);
>Shouldn't this be
>scanf("%d", &num);
Why? In actual practice %i and %d can be used the same way, though %i is a bit more generic as it can be used to scan in non-decimal values whereas %d doesn't have that feature.

-Prelude

12. ## i have modified, but i still cant read the char

#include <stdio.h>
#include <conio.h>

int main()
{
int num, count, total, max;
float average;

clrscr();
do
{
count=0, total=0, num=0;
printf("\n");
printf("Enter an integer: ");
scanf("%d", &num);

while ( count < 3 && num > 0 )
{
count=count + 1;
total += num;
if ( num > max )
{
max = num;

}

printf("Enter next integer: ");
scanf("%d", &num);
}

if ( count > 0 )
{
printf("\n");
printf("The maximum number is %i\n", max);
printf("the count is %i\n", count);
printf("the total is %i\n", total);
average= (float)total / count;
printf("the average in two decimal point is %.2f\n", average);
printf("\n");

}

else
{
printf("No valid number were entered\n");
}

printf("End of run. Another run? Y/N: ");

return(0);
}

Problems: the loops is soft of working, but can read char,
once i read char, i have an infinite loops . .

13. thanks deckard u got me thinking a little, i set count =0, max=0, and total =0

14. ## Got it working!

Here is a working code:

#include <stdio.h>
#include <conio.h>

int main()
{
int num, count, total, max;
float average;

clrscr();
do
{
count=0, total=0, num=0;
printf("\n");
printf("Enter an integer: ");
scanf("%d", &num);

while ( count < 9 && num > 0 )
{
count += 1;
total += num;
if ( num > max )
{
max = num;

}

printf("Enter next integer: ");
scanf("%i", &num);

}

if ( count > 0 )
{
printf("\n");
printf("The maximum number is %i\n", max);
printf("the count is %i\n", count);
printf("the total is %i\n", total);
average= (float)total / count;
printf("the average in two decimal point is %.2f\n", average);
printf("\n");

}

else
{
printf("No valid number were entered\n");
}

printf("End of run. Another run? Y/N: ");

return(0);
}

Thanks to those that helped me out, getchar() do work
why scanf dont work in getting a character?

15. Here is how to get scanf working to read a single character, in a single line of code:

scanf("%c%*c", &mychar );

Or:

scanf("%c%*[^\n]", &mychar );

I think the second one will work also. That one should be "buffer safe".

Quzah.