jeudi 21 juillet 2022

Writing a hangman game in C language but the rand function is not being recognized

[EDIT]

I now edited the code as follows (Thank you MikeCat);

int main()
{
    //randomwordgenerator
    char word[ARRAY_SIZE][200] = {"tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat"};

    
    int x = rand() % ARRAY_SIZE; 
    printf("%s\n", word[x]);

    system("pause");
    return 0;
}

and it isn't outputting any more errors (Thank you everyone else for helping me by pointing it out), since I was overloading it previously. Though now for some reason it is only choosing the second value in the array, lion.

///

I am trying to make a hangman game with a set of random words to choose from. Unfortunately, when I output the following code it only prints out the first word tiger. Why is it not understanding the rand function?

word[x][11] = rand();

Seems like I am getting the error; C6386 - Bugger overrun while writing to 'words[x]': the writable size is '10' byts, but '11' bytes might be written.

I tried changing it to 11 but I get the same error.


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#include <string>
#define ARRAY_SIZE 10

int main()
{
    //randomwordgenerator
    char word[ARRAY_SIZE][10] = {"tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat"};

    int x = 0;
    srand(time(NULL));

    while (x < ARRAY_SIZE - 9)
    {
        word[x][10] = rand();
        printf("%s\n", word[x]);
        x++;
    }

    system("pause");
    return 0;
}



Aucun commentaire:

Enregistrer un commentaire