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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. hangman code in C

hangman code in C

Scheduled Pinned Locked Moved C#
csharpdata-structuresregexhelpquestion
2 Posts 2 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\_word\[
    
    L 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\_word\[
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Member 11189395 wrote:

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

      I hate to disappoint you, but that is not C#.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      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