0

This is a code where the user has a 'Word Search Game', the user is has a menu where he can choose to do a new puzzle and then show it and try to work it out. The user needs to enter 4 words and those inputted words need to be shown in a matrix for other users to find and play. My problem with my code is when the user has to enter 4 words, the system crashes and exists the program. Also how can i put those words which are written by the user inside the matrix?

Code below:

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

int main(void)
{
    srand((unsigned)time(NULL));
    char Matrix[10][10];
    char Location[4][4];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            Matrix[i][j] = (rand() % (91 - 65)) + 65;
        }
    }
    int menuChoice;
    do
    {
        system("cls");
        puts("WORD SEARCH GAME PUZZLE");
        puts("-----------------------");
        puts("1. New Puzzle");
        puts("2. Show Puzzle");
        puts("3. Exit");
        puts("-----------------------");
        puts("Select your choice: ");
        scanf_s("%d", &menuChoice);

        switch (menuChoice)
        {
        case 1: newPuzzle(); break;
        case 2: showPuzzle(Matrix); break;
        }
    } while (menuChoice != 3);
    return 0;
}

int newPuzzle()
{
    int i;
    char word[7];
    system("cls");
    printf("Enter 4 words of your choice to be entered in the puzzle: \n");
    for (i = 0; i < 4; i++)
    {
        printf("Enter word %d: ", i + 1);
        scanf_s("%s", &word);
    }
    return 0;
}

int showPuzzle(char m[10][10])
{
    system("cls");
    printf("    ");
    char c;
    for (c = 'A'; c <= 'J'; ++c)
    {
        printf("%c ", c);
    }
    printf("\n\n");
    for (int i = 0; i < 10; i++) {
        printf("%d   ", i);
        for (int j = 0; j < 10; j++) {
            printf("%c ", m[i][j]);
        }
        printf("\n");
    }
    getch();
    return 0;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Bleach101
  • 21
  • 1
  • 5
  • 1
    There are no function prototypes - you are calling functions which are not declared. Please see [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). That is about `scanf` but also applies to `getchar`. --- Oh but hang on, what is `getch`? It is a non-standard function, and you have not included the relevant library header. Please enable and act on all compiler warnings. – Weather Vane Jan 06 '18 at 21:22
  • 1
    Please note too, `getch` and numerous other input functions return the type `int` not `char`. So `char letter` should be `int letter`. It would pay you to read the man page for every function you use. – Weather Vane Jan 06 '18 at 21:29
  • @WeatherVane i edited the code a bit..but now the program crashes and stops after the user enters a word. – Bleach101 Jan 06 '18 at 21:41
  • when editing the posted code, ADD a whole new code block (and comment that original block is obsolete) Changing the code makes some comments meaningless and just adds confusion. – user3629249 Jan 06 '18 at 22:01
  • regarding: `system("cls")` is not portable. Suggest using the ANSI terminal escape sequences to manipulate the cursor and screen layout and for clearing the screen of the terminal – user3629249 Jan 06 '18 at 22:07
  • 1
    regarding: `scanf_s("%s", &word);` Doesn't this function have a 3rd parameter that states the max number of characters to read? – user3629249 Jan 06 '18 at 22:11
  • when writing a `switch()` statement, ESPECIALLY with user input, it is best to include a `default:` case. NEVER trust the user to do the right thing. Note: this statement: `scanf_s("%d", &menuChoice);` would be much better written as: `menuChoice = getchar();` amongst other reasons because `getchar()` is much less CPU intensive – user3629249 Jan 06 '18 at 22:16
  • what happens when the word entered by the user is greater than 6 characters? ... Buffer overflow, undefined behavior and probably a seg fault event. – user3629249 Jan 06 '18 at 22:20
  • the function: `newpuzzle()` fails to place the 4 words entered by the user into the puzzle. – user3629249 Jan 06 '18 at 22:21
  • the functions: `newPuzzle()` and `showPuzzle()` are returning a value, but that value is never used/checked. Strongly suggest those two functions return type be `void` and anything about returning a value be removed from the function bodies – user3629249 Jan 06 '18 at 22:25
  • when compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Here are the options for Visual Studio: [options](https://msdn.microsoft.com/en-us/library/19z1t1wy.aspx) – user3629249 Jan 06 '18 at 22:31

0 Answers0