1

I made a hangman game; it runs fine with no errors. I used a while loop, with a nested if statement, to give the player ten guesses. I want the loop to break if they completely spell the word. I am using a dash counter to keep track of the numbers of letters left over; when it hits zero, then the word is completely spelled.

The thing is though, my teacher is not letting me use break statements. I don't see any other ways to break the while loop. Thoughts?

int main()
{
    printf("Your Word Is: %s", &dash);
    //printf("\nWord: %c", &dash);

    loopcount = 10;
    count = 1;
    while ((count <= loopcount))
    {
        printf("\n\nPick a letter to guess: ");
        scanf(" %c", &guess);

        printf("\n\nYour Guess Was %c. ", guess);
        count = count + 1;

        if (guess == 'c')
        {
            dash[0] = 'c';
            printf("\n\n %s", dash);
        } else if (guess == 'o')
        {
            dash[1] = 'o';
            printf("\n\n %s", dash);
        } else if (guess == 'd')
        {
            dash[2] = 'd';
            printf("\n\n %s", dash);
        } else if (guess == 'i')
        {
            dash[3] = 'i';
            printf("\n\n %s", dash);
        } else if (guess == 'n')
        {
            dash[4] = 'n';
            printf("\n\n %s", dash);
        } else if (guess == 'g')
        {
            dash[5] = 'g';
            printf("\n\n %s", dash);
        } else
        {
            printf("That Letter Is Not Apart of this Word");
        }
    
        printf("\n\nYou are at Guess Number %d, Your word is at %s",
              count, dash);
    
        dashcount = 0;
        for (i = 0; dash[i]; i++)
        {
            if (dash[i] == '-')
            {
                dashcount++;
            }
        }
    }

    if (dashcount > 0)
    {
        printf("\n");

    } else
    {
        printf("\n\n\nI'm Sorry, the word was %s.", word);
    }

    printf("\n\n");
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    The only alternative to the condition statement, or `break;` is `goto label;` – David C. Rankin Oct 01 '20 at 05:40
  • 1
    You could put the loop in a function, and return from the function. – Barmar Oct 01 '20 at 05:43
  • You can set a variable that you check in the `while()` condition, and use `continue` instead of `break`. – Barmar Oct 01 '20 at 05:44
  • `while (count <= loopcount && dashcount > 0)` – user3386109 Oct 01 '20 at 05:58
  • You ask about getting rid of a `break`, but I do not see one in your code. Please show a [mre] of the code which does what you want and has the only problem of containing a `break`. – Yunnosch Oct 01 '20 at 06:02
  • I am curious: why did you choose to avoid `switch(guess)` in favor of `if-else-if`? – virolino Oct 01 '20 at 06:23
  • @virolino: I am not OP thought that way, but for a point I'd advocate using `if-else-if` instead of `switch-cases` because the `switch-case` typically produce a larger code than `if-else-if` statements. It may not matter generally, but it does matter on a small bare-metal embedded system, where flash memory is in orders of as low as 8/16/32 KB. – WedaPashi Oct 01 '20 at 06:31
  • @WedaPashi: I found on the net statements regarding `switch` being faster, but nothing about size. Please help? :) – virolino Oct 01 '20 at 09:32
  • @virolino: Not an official reference as such but, please see https://softwareengineering.stackexchange.com/questions/15820/should-i-use-switch-statements-or-long-if-else-chains#:~:text=So%20the%20switch%20case%20had,line%20as%20the%20case%20statement. and/or https://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c. I have observed the same countless times with 8-bit or 16-bit controllers I work on, its just that I can't share the snippet and generated assembly code and the binary here in a comment. – WedaPashi Oct 01 '20 at 11:28
  • @WedaPashi: thanks for the reference. I already went through it (kind of diagonally, true) and I did not find any reference to the size, resulting as smaller or bigger. They mostly speak about speed optimization and code clarity. Do not get me wrong, I do not contradict you (as I do not have any information, pro or against). I just find your statement about the size a bit counter-intuitive. Maybe the compiler is older and does not optimize properly? Or it is configured to maximize speed at the cost of size? – virolino Oct 01 '20 at 11:45

1 Answers1

1

try to change the if statement to:

 while ((count <= loopcount) && (dashcount > 0))

This if statement will make the while loop to run only when both of the sides will be True.

Jord_Shan
  • 111
  • 4