Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:32:21+00:00 2026-05-31T08:32:21+00:00

in the for loop, and in the if statements, for some reason it goes

  • 0

in the for loop, and in the if statements, for some reason it goes through both if statements even though they are the opposite of each other and i dont know why it is doing it. I have tried different ways and it still comes out the same way.

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



int main ()
{
char secretword[20] = {};
char alphabet[28] = {"abcdefghijklmnopqrstuvwxyz "};
char guess[2] = {};
int i = 0, k = 0;
int seclength = 0, alphlength = 0;
int GuessCtr = 6;

printf("You Get six chances to guess all of the letters in a phrase\n");
printf("Enter the secret word/phrase: ");
scanf("%s", &secretword);
seclength = strlen(secretword);
alphlength = strlen(alphabet);
while(GuessCtr != 0)
{
    printf("Past guesses: ");
    for(i = 0; i < alphlength; i++)
    {
        printf("%c", alphabet[i]);
    }
    printf("\n");
    printf("Guess a character: ");
    scanf("%s", &guess);
    printf("\n");
    for(i = 0; i < seclength; i++)
    {
            if(secretword[i] == guess[0])
            {
                secretword[i] = '*';
            }
            /*else
            {
                GuessCtr--;
                printf("You missed - you have %d wrong guesses left!", GuessCtr);
            }*/
            if(secretword[i] != guess[0])
            {
                GuessCtr--;
                printf("You missed - you have %d wrong guesses left!", GuessCtr);
            }
    }

    for(i = 0; i < seclength; i++)
    {
        printf("%c", secretword[i]);
    }
    printf("\n");
    for(i = 0; i < alphlength; i++)
    {
        if(alphabet[i] == guess[0])
        {
            alphabet[i] = '*';
        }
    }
}
printf("You suck!");


return 0;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:32:23+00:00Added an answer on May 31, 2026 at 8:32 am

    Because the body of the first if statement changes the variable that it tested, and the second one will test again. When the second if is evaluated, the variable has a different value than it did the first time.

    To fix this, you need to use else, so the condition is “remembered” from the first time:

            if(secretword[i] == guess[0])
            {
                secretword[i] = '*';
            }
            else
            {
                GuessCtr--;
                printf("You missed - you have %d wrong guesses left!", GuessCtr);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using C#. I have a list of items. I loop through each
If I have something like a loop or a set of if/else statements, and
I want to know how to increase the value in a FOR-loop statement. This
How to loop through a select statement results to have a formatted text? for
The situation is like this: there is a continuous loop, that updates some values.
So I'm trying to refactor some code which creates JDBC objects in a loop
For some reason I'm getting an error: statement with no effect on this statement.
I'm trying to use a break statement in a for loop, but since I'm
I have a simple for loop followed by a simple if statement: for airport
How can polymorphism replace an if-else statement or Switch inside of a loop? In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.