Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. hangman code in C

hangman code in C

Scheduled Pinned Locked Moved C / C++ / MFC
csharpdata-structuresregexhelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11155132
    wrote on last edited by
    #1

    hello guys, i have to write a simple project ... it is a hangman code in C# i have this...but when I run it... i keeps printing twice some sentences... if anybody can help me... and if the user type something different from l ..when it is asked...everything goes wrong...

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

    /* contant declarations */
    #define NUM_TRIES_ALLOWED 10

    /* function prototypes */

    main()
    {
    /* variable declarations */
    int num_letters = 0, /* length of word char array */
    count = 0, /* for word char array */
    tries = 0, /* total tries user has used */
    num_vis_chars = 0, /* # of visible characters */
    correct_guesses = 0, /* # of correct guesses */
    correct_flag = 0, /* was guess correct? */
    repeat_flag = 0, /* was guess a repeat? */
    choice;

    char guess,guessword;

    /* array declarations */
    char word[255] = " ";
    char incorrect_letters[255] = " ";
    /* get word */
    puts( "Enter a word for player to guess." );
    gets( word );

    printf("Ready to start!\n");
    num_letters = strlen( word );
    char visible_word[num_letters]; /* displays correct guesses */

    /* initialize visble_word */
    for( count = 0; count < num_letters; count++ )
    visible_word[count] = '*';

    visible_word[num_letters] = '\0';

    if (guess == visible_word[count]){

    while( tries < NUM_TRIES_ALLOWED )
    {

      printf( "The word is: %s\\n\\n", visible\_word );
      printf("Number of turns remaining: %d", NUM\_TRIES\_ALLOWED-tries);
      printf("\\nWould you like to guess the word \[w\] or guess a letter \[l\]:");
        choice = getchar();
    
            if (choice=='l'){
    
      printf( "\\nWhat letter have you chosen?:\\t " );
      scanf( " %c", &guess );}
    
            /\* match guess against previous guesses \*/
      for( count = 0; count < num\_letters; count++ )
         if( guess == visible\_word\[count\] || guess == incorrect\_letters\[count\] )
         {
            repeat\_flag  = 1;
            correct\_flag = 1;
            break;
         }
    
      if( repeat\_flag == 0 )
         /\* check for matches in string \*/
         for( count = 0; count < num\_letters; count++ )
         {
            if( guess == word\[count\] )
            {
            visible\_wo
    
    L D 2 Replies Last reply
    0
    • U User 11155132

      hello guys, i have to write a simple project ... it is a hangman code in C# i have this...but when I run it... i keeps printing twice some sentences... if anybody can help me... and if the user type something different from l ..when it is asked...everything goes wrong...

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

      /* contant declarations */
      #define NUM_TRIES_ALLOWED 10

      /* function prototypes */

      main()
      {
      /* variable declarations */
      int num_letters = 0, /* length of word char array */
      count = 0, /* for word char array */
      tries = 0, /* total tries user has used */
      num_vis_chars = 0, /* # of visible characters */
      correct_guesses = 0, /* # of correct guesses */
      correct_flag = 0, /* was guess correct? */
      repeat_flag = 0, /* was guess a repeat? */
      choice;

      char guess,guessword;

      /* array declarations */
      char word[255] = " ";
      char incorrect_letters[255] = " ";
      /* get word */
      puts( "Enter a word for player to guess." );
      gets( word );

      printf("Ready to start!\n");
      num_letters = strlen( word );
      char visible_word[num_letters]; /* displays correct guesses */

      /* initialize visble_word */
      for( count = 0; count < num_letters; count++ )
      visible_word[count] = '*';

      visible_word[num_letters] = '\0';

      if (guess == visible_word[count]){

      while( tries < NUM_TRIES_ALLOWED )
      {

        printf( "The word is: %s\\n\\n", visible\_word );
        printf("Number of turns remaining: %d", NUM\_TRIES\_ALLOWED-tries);
        printf("\\nWould you like to guess the word \[w\] or guess a letter \[l\]:");
          choice = getchar();
      
              if (choice=='l'){
      
        printf( "\\nWhat letter have you chosen?:\\t " );
        scanf( " %c", &guess );}
      
              /\* match guess against previous guesses \*/
        for( count = 0; count < num\_letters; count++ )
           if( guess == visible\_word\[count\] || guess == incorrect\_letters\[count\] )
           {
              repeat\_flag  = 1;
              correct\_flag = 1;
              break;
           }
      
        if( repeat\_flag == 0 )
           /\* check for matches in string \*/
           for( count = 0; count < num\_letters; count++ )
           {
              if( guess == word\[count\] )
              {
              visible\_wo
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Try using your debugger to identify exactly where the problem(s) occur.

      U 1 Reply Last reply
      0
      • L Lost User

        Try using your debugger to identify exactly where the problem(s) occur.

        U Offline
        U Offline
        User 11155132
        wrote on last edited by
        #3

        my debugger screen is grey, I tried to do this....but I couldnt

        L 1 Reply Last reply
        0
        • U User 11155132

          my debugger screen is grey, I tried to do this....but I couldnt

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I cannot even get your code to compile so I have no idea how you are managing to run it.

          CPalliniC 1 Reply Last reply
          0
          • L Lost User

            I cannot even get your code to compile so I have no idea how you are managing to run it.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            :-D My up-vote for trying.

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

            In testa che avete, signor di Ceprano?

            L 1 Reply Last reply
            0
            • CPalliniC CPallini

              :-D My up-vote for trying.

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              A quick desk check suggested a number of errors, but I wanted to be sure. :thumbsup:

              1 Reply Last reply
              0
              • U User 11155132

                hello guys, i have to write a simple project ... it is a hangman code in C# i have this...but when I run it... i keeps printing twice some sentences... if anybody can help me... and if the user type something different from l ..when it is asked...everything goes wrong...

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

                /* contant declarations */
                #define NUM_TRIES_ALLOWED 10

                /* function prototypes */

                main()
                {
                /* variable declarations */
                int num_letters = 0, /* length of word char array */
                count = 0, /* for word char array */
                tries = 0, /* total tries user has used */
                num_vis_chars = 0, /* # of visible characters */
                correct_guesses = 0, /* # of correct guesses */
                correct_flag = 0, /* was guess correct? */
                repeat_flag = 0, /* was guess a repeat? */
                choice;

                char guess,guessword;

                /* array declarations */
                char word[255] = " ";
                char incorrect_letters[255] = " ";
                /* get word */
                puts( "Enter a word for player to guess." );
                gets( word );

                printf("Ready to start!\n");
                num_letters = strlen( word );
                char visible_word[num_letters]; /* displays correct guesses */

                /* initialize visble_word */
                for( count = 0; count < num_letters; count++ )
                visible_word[count] = '*';

                visible_word[num_letters] = '\0';

                if (guess == visible_word[count]){

                while( tries < NUM_TRIES_ALLOWED )
                {

                  printf( "The word is: %s\\n\\n", visible\_word );
                  printf("Number of turns remaining: %d", NUM\_TRIES\_ALLOWED-tries);
                  printf("\\nWould you like to guess the word \[w\] or guess a letter \[l\]:");
                    choice = getchar();
                
                        if (choice=='l'){
                
                  printf( "\\nWhat letter have you chosen?:\\t " );
                  scanf( " %c", &guess );}
                
                        /\* match guess against previous guesses \*/
                  for( count = 0; count < num\_letters; count++ )
                     if( guess == visible\_word\[count\] || guess == incorrect\_letters\[count\] )
                     {
                        repeat\_flag  = 1;
                        correct\_flag = 1;
                        break;
                     }
                
                  if( repeat\_flag == 0 )
                     /\* check for matches in string \*/
                     for( count = 0; count < num\_letters; count++ )
                     {
                        if( guess == word\[count\] )
                        {
                        visible\_wo
                
                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Member 11189395 wrote:

                i have to write a simple project ... it is a hangman code in C#

                If this is true, then you should be in the C# forum. Otherwise, what you have shown looks like C code.

                Member 11189395 wrote:

                i have this...but when I run it... i keeps printing twice some sentences...

                Have you stepped through the code using the debugger? Are you using Visual Studio, or some other IDE?

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups