Thread: working on a project, hey forum, it's good to be back, computer programming problem

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    124

    Cool working on a project, hey forum, it's good to be back, computer programming problem

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #define GUESS_COUNT 3
    
    
    
    https://kodekata.net/hangman.html
    
    int main()
    {
        int a = 0;
        char word[13] = {'h', 'i', 'd', 'd', 'e', 'n', 's', 't', 'r', 'i', 'n', 'g'};
        char new_str[131313];
        int penalty = 0, length;
        int i = 0;
    
    
        /* Game is started with a word */
    
    
        length = strlen(word);
    
    
        printf("%s:\n Length of string: %d\n", word, length);
    
    
        for (i = 0; i < 26; i++)
        {
            new_str[i] = word[i];
        }
    
    
        for (i = 0; i < 26; i++)
        {
            new_str[i] = word[i];  // copy word[i] into new_str[i]
            printf("%c", new_str[i]);
        }
    
    
        printf("\n");
    
    
        char scan_string;
        int ascii = scan_string;
        // word[i] is char array
    
    
        while (a < GUESS_COUNT) // loop 3 times
        {
            printf("Enter letter: ");
            scanf("%c", &scan_string);
    
    
            /* create variable for scan_string 1 between 26 */
    
    
            for (i = 65; i < 90; i++) // If letter is in word, all instances revealed
            {
                if (new_str[i] == ascii) // Was found in search
                {
                    //printf("Found: %c\n", new_str[i]);
                    printf("Found value: %c\n", scan_string);
                }
    
    
                else
                {
                    printf("Value Not found\n");
                    ++penalty;
                    break;
                }    
            }
            a++;
        }
    
    
    
    
        return 0;
    }
    I've been on the forum a few years now, please, not that hard of a problem, couple questions for my loop, wanted to paste it, it's from link from Hangman | Implement hangman game. attached. Could someone please help fix code attached, also is a loop good, with an if() and else() included for the program. I have only worked on a few steps of the program. Also, I have moved onto to building full Python GUI (Tkinter) programs on past C on Debian linux, that's where I have been. This was built on GCC Debain, i am reading about perl, buffer overflows too, and front & back end development on visual studio now, it's been 4 years since learning C and I am starting a career.
    Last edited by _jamie; 04-24-2022 at 08:44 PM.

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    64
    Well number 1:
    Code:
    char word[13] = {'h', 'i', 'd', 'd', 'e', 'n', 's', 't', 'r', 'i', 'n', 'g'};
    Do you really want to take the strlen of word?

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,391
    @G4143, Remember that if you set the first few members of an array, the remaining members will be set to zero. So the string is valid since it is sized to be one element longer than the explicitly set elements. Still, the proper way to do it would be more like:
    Code:
    char word[] = "hiddenstring"; // the size of the array is set automatically
    Or perhaps even:
    Code:
    const char *word = "hiddenstring";
    More problematic is that he is accessing the array outside it's bounds. E.g.,
    Code:
    for (i = 0; i < 26; i+)
        new_str[i] = word[i]; // indices for word only go from 0 to 12
    Inexpicably, he does that twice.
    And then, bizarrely:
    Code:
    for (i = 65; i < 90; i++) // WTF?!
    None of it makes sense.
    A long time ago being crazy meant something. - Charles Manson

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,139
    Code:
    scanf("%c", &scan_string);
    The above likely needs to be

    Code:
    scanf(" %c", &scan_string);
    The space results in white spaces being skip on input.

    But, the OP needs to fix the other issues, also.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    64
    Quote Originally Posted by john.c View Post
    @G4143, Remember that if you set the first few members of an array, the remaining members will be set to zero. So the string is valid since it is sized to be one element longer than the explicitly set elements.
    Didn't know that! Thanks for pointing that out.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,391
    Quote Originally Posted by G4143 View Post
    Didn't know that! Thanks for pointing that out.
    It's particularly useful for initializing an entire array to 0. E.g.,
    Code:
        int a[1000] = { 0 }; // zeroes entire array
    A long time ago being crazy meant something. - Charles Manson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-14-2015, 11:58 AM
  2. Good forum for computer hardware questions?
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-27-2005, 08:17 AM
  3. Replies: 5
    Last Post: 02-20-2004, 09:36 AM
  4. Replies: 7
    Last Post: 09-08-2002, 02:20 PM

Tags for this Thread