Question

I am not sure why I am getting an infinite loop when I run this code. I can get it to run just fine without the function, but that would defeat the purpose of everything I will be trying to do. Can anyone spare a second to see what's going on with this function that is causing my program to crash?

#include <stdio.h>
#define ROWS 5

int checkStatus(int manStatus[]) {

    int i;
        for(i = 0; i < ROWS; i++)
        {
            if(manStatus[i] == -1)
                printf("Sorry, but %d is still single!\n", i);
                return 1; //True
        }
        return 0; //False
    }

void main() {

    int manStatus[ROWS];
    int i=0;

    //Initialize each man to -1, as in single:
    for(i=0; i < ROWS; i++) {
        manStatus[i] = -1;
    }

    i=0;
    while ( (checkStatus(manStatus) ) ) {

        printf("Still Single People!");
        manStatus[i] = 0;
        i++;
    }
}
Was it helpful?

Solution

You need a curly brace in your condition, here:

    if(manStatus[i] == -1)
       {
            printf("Sorry, but %d is still single!\n", i);
            return 1; //True
       }

Without which will always return true (return 1) and therefore would create an infinite loop in your main().

OTHER TIPS

You have a pair of braces ({}) missing:

        for(i = 0; i < ROWS; i++)
        {
            if(manStatus[i] == -1)
                printf("Sorry, but %d is still single!\n", i);
                return 1; //True
        }

Change that to this:

        for(i = 0; i < ROWS; i++)
        {
            if(manStatus[i] == -1)
            {
                printf("Sorry, but %d is still single!\n", i);
                return 1; //True
            }
        }

Despite the appearance of this code, the two statements are not grouped together:

if(manStatus[i] == -1)
    printf("Sorry, but %d is still single!\n", i);
    return 1; //True

This effectively means that your function returns 1 every single time.... not dependent on an if statement.

What you really want is:

 if(manStatus[i] == -1)
 {
     printf("Sorry, but %d is still single!\n", i);
     return 1; //True   ==> INSIDE the If-statement <==
 }

This:

if(manStatus[i] == -1)
    printf("Sorry, but %d is still single!\n", i);
    return 1; //True

Should be:

if(manStatus[i] == -1) {
    printf("Sorry, but %d is still single!\n", i);
    return 1; //True
}

If its more than one line, needs to have braces. Rule of thumb, always put them in.

    int checkStatus(int manStatus[]) {

    int i;
    for(i = 0; i < ROWS; i++)
    {
        if(manStatus[i] == -1)
            printf("Sorry, but %d is still single!\n", i);
        return 1; //True
    }
    return 0; //False
    }

the problem here in the if condition .. the compiler reads it as : if manStatus[i] == -1 printf("string") else return 1;

so you are messing a curly bracket, try this :

 int checkStatus(int manStatus[]) {

int i;
for(i = 0; i < ROWS; i++)
{
    if(manStatus[i] == -1)
    {
        printf("Sorry, but %d is still single!\n", i);
        return 1; //True
    }
}
return 0; //False
 }

and also when you are sending the manStatus in the while loop you are only sending the first element not the whole array so "checkStatus" function is always checking the first element over and over.. so it returns the same result over and over .. making the infinite loop you had before.

hope that helped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top