-2

I accidentally clicked something within the CodeBlocks environment and now when I run the C file one of my empty char arrays become populated with several garbage values. I redownloaded CodeBlocks, verified PATH and restarted my computer to no avail. Although I transferred my .c file to my laptop and everything worked correctly there my environment on my desktop still produces this behavior. Results of file run

The "X->" is the garbage value that appeared on this run but they vary every run.

Edit: The program isn't finished yet because of the error. Also, This is for my intro to C class and my code is probably terribly inefficient. Edit2: Upon running my file I hear the windows error sound.


int playRound(char starword[], char answer[])
{
    int strikeTotal = 0;
    int size = strlen(answer);
    char userGuesses[27];
    char userguess;
    int correctInput = 0;
    int counter = 0;
    int starsInWord = strlen(starword);
    int winCon = 0;

    printf("Welcome to the round!\n");
    printf("The size of the word has %d letters.\n",size);

    while(strikeTotal <= 6)
    {
        printf("You currently have %d strike(s).\n",strikeTotal);
        printf("Letter(s) you have guessed: %s\n\n",userGuesses);
        printf("%s \n", starword);

        correctInput = 0;
        while(correctInput == 0)
        {
            printf("Enter your guess: ");
            scanf(" %c", &userguess);
            if ((userguess>= 65)&&(userguess<= 90))
            {
                userguess = userguess + 32;
                correctInput++;
            }
            else if((userguess >= 97) && (userguess <= 122))
            {
                correctInput++;
            }
            else
            {
                printf("Invalid Input!\n");
            }
        }
        userGuesses[counter] = userguess;
        counter++;
        printf("\nYou currently have %d strike(s).\n",strikeTotal);
        printf("Letter(s) you have guessed: %s \n", userGuesses);

}

M.Hunter
  • 13
  • 4
  • 1
    Was it a local array? If so, it's usually going to start out containing unpredictable garbage, unless/until you explicitly initialize it. Can you show us your code? – Steve Summit Nov 15 '21 at 20:09
  • Please add the code. You are probably not initializing the array. – 001 Nov 15 '21 at 20:09
  • @JohnnyMopp The char array is initialized by "char userGuesses[27];" Working on adding my code now... – M.Hunter Nov 15 '21 at 20:13
  • 1
    `char userGuesses[27];` doesn't initialize the array. If defined inside a function then its contents will be *indeterminate* (as in random garbage). – Some programmer dude Nov 15 '21 at 20:15
  • [How to initialize array to 0 in C?](https://stackoverflow.com/a/2589751) – 001 Nov 15 '21 at 20:15
  • @0___________ Code is added – M.Hunter Nov 15 '21 at 20:17
  • @M.Hunter Thanks, although in this case it wasn't really necessary. That's too much code to look at — you're supposed to trim it down to a minimal example. But that's not necessary, because we already told you what you need to do: initialize the array! – Steve Summit Nov 15 '21 at 20:19
  • It sounds like you inadvertently turned on a CodeBlocks feature designed to help you discover a kind of programming error that you were in fact committing. – John Bollinger Nov 15 '21 at 20:21
  • 1
    Note also: there is no such thing as an empty array in C. There are sometimes arrays that do not contain any *meaningful* data, but that doesn't make them empty. – John Bollinger Nov 15 '21 at 20:24
  • @M.Hunter I'm sorry I suggested trimming your code down, because now you've deleted the part where you try to fill in and use `userGuesses`. – Steve Summit Nov 15 '21 at 20:25
  • @SteveSummit My apologies for vocabulary. I declared the array there and later update it's elements with each individual user guess. This seems to be a codeblocks error instead of a problem directly with my code as it compiles and runs with no error on my other device. – M.Hunter Nov 15 '21 at 20:26
  • @M.Hunter Your code has _undefined behaviour_ (google that). Undefined behaviour includes "apparently working fine". – Jabberwocky Nov 15 '21 at 20:28
  • 1
    @M.Hunter A comment of mine was deleted, presumably because it contained 0.1% snark. You said, "This seems to be a codeblocks error". This is incorrect. It is your code that has an error. – Steve Summit Nov 15 '21 at 20:37
  • You have two problems with `userGuesses` (that I can see): 1) You use it before you have initialized it in any way, in the second line of the ` while(strikeTotal <= 6)` loop; And 2) You never null-terminate it as a string, which means when you print it it will go beyond the initialized parts (once you start initializing it). Possibly you can go out of bounds of the array. – Some programmer dude Nov 15 '21 at 20:41
  • BTW: don't post pictures of text. Post text as text. Output is text that can be copy/pasted. – Jabberwocky Nov 15 '21 at 20:43

1 Answers1

1

By following the thread posted in the comments by JohnnyMopp I've found the solution that got rid of the error noise and display of garbage values.

From:

 char userGuesses[27];

To:

char userGuesses[27] = {0};
M.Hunter
  • 13
  • 4