JA3N
JA3N

Reputation: 731

scanf infinite loop

This program takes the first number in a file and indicates how many numbers are going to be after it, then does various other things with the numbers that follow.

It seems like scanf is causing an infinite loop when trying to read from the file. WHen I run the program not even the check at 1 works

Here is the code:

#include <stdio.h>

int main(void) {
        int N, a, n;
        int x=0;
        int t=0;
        printf("1"); //Check
        scanf("%d", &N);
        printf("2"); //Check
        int nums[N];
        int i;
        printf("%d", &N);  //Check
        for (i=0; i<N; i++)
        {
                scanf("%d", &nums[i]);
                t+=nums[i];

                if (nums[i] > x) x=nums[i];

                if (i=0 || nums[i] < n) n = nums[i];
        }
        a = t/N;   
        printf("Number Processed: \t%d\n", &N);
        printf("Maximum: \t%d\n", &x);
        printf("Minimum: \t%d\n", &n);
        printf("Total: \t%d\n", &t);
        printf("Average: \t%d\n", &a);
}

The way i run the program is

gcc -lab16
./a.out <in1

where in1 is text and has the numbers

7
6
-30
90
3903
-934
443
445

Thanks for your time.

Upvotes: 0

Views: 420

Answers (3)

amit
amit

Reputation: 178521

if (i=0 || nums[i] < n) n = nums[i];

you are assigning i = 0, so the loop never realy advances! You probably wanted i == 0. This is causing the infinite loop.

Other issue: int nums[N]; - if you want an array of dynamic [determined in run-time] size, you will probably need to malloc() it.

Update: note that int nums[N] is valid in C99, so if your assigment is assuming C99 you should not worry about this issue. Otherwise - malloc() will be needed:

int* nums = (int*) malloc(sizeof(int) * N)

And don't forget to free(nums) before the program ends, or you will get memory leak.

Upvotes: 5

Adam Zalcman
Adam Zalcman

Reputation: 27233

There are numerous problems with this code.

You mistook assignment for comparison:

i=0

sets i to zero. You probably meant

i==0

which checks whether i is equal to zero.

You should check the value returned by scanf(). When data read by scanf() doesn't fit the format you specified it leaves the data in the buffer and the next call to scanf() sees the same data again.

The reason that the first printf() doesn't print anything is most likely that you are not printing any newline. Note that on some output devices like terminals output is line-buffered.

You are passing a pointer to a variable to printf(), but your format specifies %d. You probably meant the variable itself, not a pointer to it.

Also, if you need an array of length dependent on a value known only at runtime, you need to allocate it on the heap, e.g. using malloc().

Upvotes: 1

JMDeschenes
JMDeschenes

Reputation: 31

if (i=0 || nums[i] < n) n = nums[i]; 

This is the culprit. You are making an assignment i=0 when you should do a comparison : i==0

Your loop goes to infinity because everytime you are i to 0.

Moreover, the code you gave us, gave me an error, because you were creating new variables during runtime.

#include <stdio.h> 
#include "stdlib.h"
int main(void) { 
    int N, a, n; 
    int x=0; 
    int t=0; 
    int i; 
    int *nums;

    printf("1"); //Check 
    scanf("%d", &N); 
    printf("2"); //Check 

    nums = malloc(N*sizeof(int));

....

Upvotes: 1

Related Questions