Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm getting an incredible error in the for loop in c language. For loop works in 1,2 and 3 but stops suddenly

I know it’s a cliché title but it really is.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int chances = 5;         //You can change your chances in there
char sentence[20];      //You can change your sentence's lenght. BUT İF U CHANGE THAT u have change indicator'S lenght
char indicator[20];     //İndicator
char character;         //Variable for your guess 

int woncheck = 0;

void status();          //function
void check();           //function

void main() {

    printf("Wellcome to my hangman game");
    printf("Player 1 please enter your sentence [Max 20 character] ==> ");  
    gets(sentence);         //Get sentence
    
    while (strlen(sentence)>=20)    //Checking for less than 20 char
    {
        printf("Please enter max 20 character ==> ");
        gets(sentence);
    }

    for (int i = 0; i < strlen(sentence); i++) {  // The indicator takes _ as long as the sentence
        if (sentence[i] != ' ') {
            indicator[i] = '_';
        } else {
            indicator[i] = ' ';
        }
    }

    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");   //Screen was skipped 

    status(); //Shows sentence's lenght before all of all

    for (int j = 0; j < chances ; j++) //We gave [changes] rights, so we create loops for a [changes] times
    {
        woncheck = 1;   //We need to check the game

        for (int k = 0; k < strlen(sentence); k++) {
            if (indicator[k] == '_') { // if any indicator letter's is not equal to '_' , it is mean, we not won the game yet
                woncheck = 0;
                break;
            }
        }

        if (woncheck == 1) {
            printf("Player 1 won the game\n");
            break;
        }
    
        printf("Player 2 please enter 1 character ==> ");   //Let's gets letter
        scanf(" %c",&character);
        printf("\n");
        chances--;      // -1 Chance
        check();    //Checking
        status();   //Now where we are in status
        
        if (chances == 0) {
            printf("Player 1 lost the game\n");
            break;
        }
    }
}

void status() {
    for (int  i = 0; i < strlen(sentence); i++)     //As long as the sentence...
    {
/*         if (sentence[i]==' ')       //İf yours sentences have space, we give space
           {
           printf(" ");
           }
*/        printf("%c ",indicator[i]);     //We print the indicator equal to the length of the sentence
    }

    printf("You have %d chances left. \n \n",chances);
}

void check() {
    for (int  i = 0; i < strlen(sentence); i++)
    {
        if (toupper(sentence[i]) == toupper(character))     //if any sentece's letter equal to yours letter indicator gets your letter 
        {
            indicator[i] = character;
        }
        
    }
}

It’s basic hangman game.

But if i try enter 3. letter, program is closing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tryed these code in 2 ide + online compiler but all the time i guess i got logic error.

I request you to try it on your own computer and see if it works.

If it is not working or you found an error, I would appreciate it if you could add it to the comment.

Thanks in advance…

>Solution :

Ignoring the other problems with your code, and addressing only the claimed issue of the game loop ending prematurely, I can see you are counting the "chances" in two conflicting ways.

for (int j = 0; j < chances ; j++)
{
    // (omitted)

    chances--;      // -1 Chance
    check();    //Checking
    status();   //Now where we are in status
    
    if (chances == 0) {
        printf("Player 1 lost the game\n");
        break;
    }
}

You start with a variable chances that is initialized to the value 5, and you construct a for loop using j to loop chances times.

However, in that loop you are also reducing chances by 1 every time before continuing the loop. That means your variable states when testing

  • after the first iteration, chances is 4
  • after the second iteration, chances is 3
  • after the third iteration, chances is 2 and this now fails the continuation condition.

You should choose only one approach. Either track the game progress with j only and do not modify chances, or only use chances as follows:

while (chances > 0)
{
    // (omitted)

    chances--;      // -1 Chance
    check();    //Checking
    status();   //Now where we are in status
}

if (woncheck) {
    printf("Player 1 won the game\n");
} else {
    printf("Player 1 lost the game\n");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading