-2

I need to cycle through a loop till the end of it, but the compiler continues to loop even when i>=k and I can't go out the endless cycle. What's wrong with this snippet?

int i=0;
User signed_up[k]
char input_user[15];
bool stop_cycle=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
        }
        stop_cycle=false;
    }
} while (!stop_cycle);

Edit: k is a counter that increases in the subroutine registration everytime it runs. At this point of the code it could be 0, 1, etc. What I'm trying to achieve is to ask every time I insert an already existing username to insert it again, till the search in the array ends.

  • 1
    Do you have a debugging tool? It's the best way to get your error. Anyway, the problem is that you have programmed to loop endlessly. If the statement if (strcmp(input_user, signed_up[i].username)==0) { should be the stop condition, then why do you add stop_cycle=false;? – Krassi Em Sep 26 '17 at 17:35
  • 2
    I don't see where variable `k` is set. The snippet must be incomplete. – Evgeny Tanhilevich Sep 26 '17 at 17:35
  • 3
    is `k` ever initialized? Also `fflush(stdin)` is undefined behavior: https://stackoverflow.com/questions/18170410/what-is-the-use-of-fflushstdin-in-c-programming – yano Sep 26 '17 at 17:35
  • The question specifies the `do-while-loop` is the endless cycle, but the description mentions variable `k` which is a condition related to a `for-loop` embedded in aforementioned `do-while-loop`. Which are you having the problem with? If it is the `do-while-loop`, you always set `stop_cycle` to `false` in the `for-loop` before you reach the end of the `do-while-loop`. – Christian Gibbons Sep 26 '17 at 17:39
  • This `scanf("%s",`...` should better be `scanf("%14s", ...` else you risk buffer overflow on user input. – alk Sep 26 '17 at 17:49
  • You unconditionally set `stop_cycle` to `false` in your `for` loop - what did you *expect* to happen? Voting to close as a typo. – EJoshuaS - Stand with Ukraine Sep 26 '17 at 17:53

2 Answers2

1

What exactly are you trying to do here?

The steps your code takes both resets the i variable to 0 in each do iteration and also sets stop_cycle to both true and false each iteration.

The for loop on each do iteration runs from i=0 to i < k.

You end the do by setting stop cycle to false each time, so the while never triggers.

Try running this as:

printf("\n\n%*sUsername: ", 26, "");
fflush(stdin);
scanf("%s", input_user);

for (i=0;i<k;i++) {
    if (strcmp(input_user, signed_up[i].username)==0) {
        printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
    }
}

or

int i=0;
User signed_up[k]
char input_user[15];
bool stop_cycle=true;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            stop_cycle=false;
        }
    }
} while (stop_cycle);

Note the change in location of the stop cycle variable and the values of it in the second version.

Rashid 'Lee' Ibrahim
  • 1,357
  • 1
  • 9
  • 21
0

tl;dr

int i=0;
User signed_up[k];
char input_user[15];
bool stop_cycle=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            stop_cycle=false;
        }
    }
} while (!stop_cycle);

Longer Explanation

If this is your situation...

So you want to ask them to enter a username.

If the username exists, you want to ask them again (to enter a different username), and also print "Username inserito già presente nel database!"

You want to continue doing this until the user has entered a username that is not in the "database".

The purpose of the for loop is to test whether the username (input_user) is already in the "database" (i.e. the array, signed_up).

The variable k is a count of the number of User objects in the signed_up array.

Thus, the for loop is looping through all the values in the signed_up array to test whether input_user matches a username member of a User object in the array.

If input_user matches an existing username of a User, you want to continue looping in the do while loop.

Setting stop_cycle to true will stop the loop.

Then...

Your problem is that you only want to set stop_cycle=false; when input_user matches a username. That is the only time where you don't stop the cycle (i.e., stop_cycle=false;)

The mistake is you put stop_cycle=false; outside of the if statement. It should be inside the if statement. That will continue the "cycle" as long as the username already exists.

So this code works:

int i=0;
User signed_up[k];
char input_user[15];
bool stop_cycle=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    stop_cycle=true;

    for (i=0;i<k;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            stop_cycle=false;
        }
    }
} while (!stop_cycle);

Other Advice

In addition to other people's suggestions, my advice is:

Rename stop_cycle to username_exists, and k to num_users.

So the code looks like this:

int i=0;
User signed_up[num_users];
char input_user[15];
bool username_exists=false;

do {
    printf("\n\n%*sUsername: ", 26, "");
    fflush(stdin);
    scanf("%s", input_user);
    username_exists=false;

    for (i=0;i<num_users;i++) {
        if (strcmp(input_user, signed_up[i].username)==0) {
            printf("\n%*sUsername inserito gi%c presente nel database!", 26, "", 133);
            username_exists=true;
        }
    }
} while (username_exists); 

This makes it clear that your for loop is testing to see if the username exists, and it makes it clear that your do loop will continue as long as the username exists. It also makes it clear that k represents the number of users.