jt1019
jt1019

Reputation: 1

C program freezes after scanf input

I'm working on a homework assignment to calculate the nth prime number as chosen by the user. I had this working just fine, and fairly quickly, but I decided to add in an error message if the user put in anything greater than 50000. For some reason, that decided not to work, so I took it out. After that, my program freezes once the user inputs which prime number they want.

#include <stdio.h>

int main(void) 
{   
    long int pFactor,test,nthCount,target,result,notPrime;
    test=2;
    nthCount=0;
    printf("Which prime number do you want to know?");
    scanf("%li",&target);
    while (nthCount<target)
    {   
        for(pFactor=test/2;pFactor>1;pFactor--)
        {
            notPrime=0;
            result=(test%pFactor);
            if(result==0)
            {
                notPrime=1;
                break;
            }
            if(notPrime!=1)
            {   
                nthCount++;
                notPrime=0;
                test++;
            }
        }
    }
    test--;
    printf("The %li prime number is %li.\n",target,test);
    return 0;
}

I think it's something scanf related, as anything I try to print after that doesn't come out.

Upvotes: 0

Views: 1744

Answers (3)

moooeeeep
moooeeeep

Reputation: 32542

In such cases (program freezes) you can normally quickly get to the root of the problem by making some observations.

First, find out if it's an infinite loop or some function blocking the program flow. 100% CPU usage is a good indicator for an out-of-control loop and 0% CPU usage is a good indicator that your program is blocking, i.e. is waiting for some event to happen. In your case you could have noted that it's the first: an infinite loop.

Then try to find out where and why this happens. You can for example add debug output at strategic positions in suspected loops. You could have adapted your loop body to this:

while (nthCount<target)
{   
    /* added debug output: */
    printf("%ld, %ld, %ld\n", target, nthCount, pFactor);
    for(pFactor=test/2;pFactor>1;pFactor--)
    {
        /* omitted for the sake of brevity */
    }
}

Running this, you would have seen something like this:

$ gcc test.cc && echo 1 | ./a.out | head
Which prime number do you want to know?1
1, 0, 0
1, 0, 1
1, 0, 1
1, 0, 1
1, 0, 1
1, 0, 1
1, 0, 1
1, 0, 1
1, 0, 1

Here you can see that the outer loop is spinning infinitely because the inner loop never executes (the last number is always less or equal to one) and thus the loop condition of the outer loop is never changed (and thus never false). And this is where you can start fixing it.

Upvotes: 0

Surajeet Bharati
Surajeet Bharati

Reputation: 1433

Your program is stuck within an infinite loop. Body of the for loop is unreachable for all cases and that causing the while-loop to run for infinite time. Modify your program and debug your program yourself next time.

Upvotes: 0

Abhineet
Abhineet

Reputation: 5409

for(pFactor=test/2;pFactor>1;pFactor--) // where test = 2

deduces to, pFactor>1 is always false (1>1)

So, the flow never enters the for loop and thus, nthCount always remains 0.

while (nthCount<target) // becomes an infinite loop

Upvotes: 6

Related Questions