Thread: self-taught newbie searching for help

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    6

    self-taught newbie searching for help

    Hello everyone,
    I have been trying to learn C and C++ programming for a while now, by watching youtube tutorials mostly. Recently, i came across a crossword tutorial to be completed in C and I am not sure what i am doing wrong.
    Firstly, i have to say that the info wasn't very clear on what the user was required to do. Most of the code was given and to be completed/corrected by the C programmer, so i added a main menu to make it easier to follow, with the following options: 1. Start a New Puzzle, 2. Solve the Puzzle and 3. Exit.
    I managed (i think to have option 2 and 3 to work (almost) as expected but i can't get a new puzzle to start.
    Also, the putHorizontalWord section of the code works (it was given) but inverting
    Code:
    [rRow][rCol + i]
    to
    Code:
    [rRow + i][rCol]
    or
    Code:
    [rRow + i][rCol + i]
    does not work for vertical and diagonal words
    What am i doing wrong?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROWS 10
    #define COLUMNS 10
    
    char puzzle[ROWS][COLUMNS];
    
    char allWords[20][10] = {"GIRL" , "BOY" , "SHIP" , "CAT" , "FOG" , "KITE" , "BAG" , "STAMP" , "ZOOM" , "JOY", "CAR" , "BUS" , "VAN" , "BOAT" , "BIKE" , "TURBO" , "SCHOOL" , "DOVIC" , "VIRUS" , "STAR"};
    
    char fourWords[4][10];
    
    char getRandomCharacter(void)
    {
        int r = (rand() % 26) + 65;
        return (char)r;
    
    }
    
    void getFourRandomWords(void) {
        int draws[4];
        // draw 4 words from the list, no duplicates
        for (int i = 0; i < 4; i++) {
            int n = rand() % (20 - i);
            int j;
            for (j = 4 - i; j < 4 && n >= draws[j]; j++) {
                draws[j - 1] = draws[j];
                n++;
            }
            draws[j - 1] = n;
            strcpy(fourWords[i], allWords[n]);
        }
    }
    
    void createBlankPuzzle()
    {
        int i , j;
    
        for(i=0; i<ROWS; i++)
        {
            for(j=0; j<COLUMNS; j++)
            {
                puzzle [i][j] = '*';
            }
        }
    }
    
    void createNewPuzzle()
    {
        int i , j;
    
        for(i=0; i<ROWS; i++)
        {
            for(j=0; j<COLUMNS; j++)
            {
                puzzle [i][j] = getRandomCharacter();
            }
        }
    }
    
    void displayPuzzel()
    {
        int i , j , rowNum = 0;
        char x = 'A';
    
        // First display column names
        printf("  ");
        for(i=0; i<COLUMNS; i++)
        {
            printf("%c ",x + i);
        }
        printf("\n");
    
        for(i = 0;i < ROWS;i++)
        {
            printf("%d ",rowNum);
            rowNum++;
            for(j=0; j<COLUMNS; j++)
            {
                printf("%c ",puzzle[i][j]);
            }
            printf("\n");
        }
    }
    
    void putHorizzontalWord(char word[10])
    {
        int rRow, rCol , ok , i;
    
        do
        {
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            ok = 1;
            if(rCol + strlen(word) < 10)
            {
                for(i = 0;i < strlen(word);i++)
                {
                    if(puzzle[rRow][rCol + i] == '*' ||
                        puzzle[rRow][rCol + i] == word[i])
                    {
                        puzzle[rRow][rCol + i] = word[i];
                    }
                    else
                    {
                        ok = 0;
                    }
                }
            }
            else
            {
                ok = 0;
            }
        }
        while(ok == 0);
    }
    
    void putVerticalWord(char word[10]) //this, doesn't seem to work'
    {
        int rRow, rCol , ok , i;
    
        do
        {
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            ok = 1;
            if(rRow + strlen(word) < 10)
            {
                for(i = 0;i < strlen(word);i++)
                {
                    if(puzzle[rRow + i][rCol] == '*' ||
                        puzzle[rRow + i][rCol] == word[i])
                    {
                        puzzle[rRow + i][rCol] = word[i];
                    }
                    else
                    {
                        ok = 0;
                    }
                }
            }
            else
            {
                ok = 0;
            }
        }
        while(ok == 0);
    }
    
    void putDiagonalWord(char word[10]) //this, doesn't seem to work'
    {
        int rRow, rCol , ok , i;
    
        do
        {
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            ok = 1;
            if(rRow + strlen(word) < 10)
            {
                for(i = 0;i < strlen(word);i++)
                {
                    if(puzzle[rRow + i][rCol + i] == '*' ||
                        puzzle[rRow + i][rCol + i] == word[i])
                    {
                        puzzle[rRow + i][rCol + i] = word[i];
                    }
                    else
                    {
                        ok = 0;
                    }
                }
            }
            else
            {
                ok = 0;
            }
        }
        while(ok == 0);
    }
    
    void fillPuzzleWithWords()
    {
        int i , orientation;
        getFourRandomWords();
    
        for(i=0; i<4; i++)
        {
            orientation = 0; //rand() % 3; // To generate a random number from 0, 1, & 2
            if(orientation == 0)
            {
                putHorizzontalWord(fourWords[i]);
            }
            else if(orientation == 1)
            {
                putVerticalWord(fourWords[i]);
            }
            else
            {
                // put word diagonal
            }
        }
    }
    
    void mainMenu()
    {
        char menuChoice;
        do
        {
            printf("------------------------");
            printf("\nUse coordinate to solve\nthe puzzle; i.e. C3, G3\n");
            printf("------------------------");
            printf("\n~~~ DAILY CROSSWORD ~~~\n");
            printf("1. New game\n");
            printf("2. Solve Puzzle\n");
            printf("3. Exit\n");
            menuChoice = getchar();
    
            switch (menuChoice)
            {
                case '1': createNewPuzzle(); break;
                case '2': displayPuzzel(); break;
            }
    
        } while (menuChoice != '3');
    }
    
    int main()
    {
        srand(time(NULL));
    
        createBlankPuzzle();
        displayPuzzel();
        fillPuzzleWithWords();
    
        mainMenu();
        getchar();
    
        printf("Thank you for playing today! :)\nGood Bye");
        return 0;
    }
    Any help would be highly appreciated!

    p.s.: i am using KDevelop on a Linux machine and a lot of commands, apparently, accepted on VSCode, show as building error in KDevelop i.e.: gets instead of fget(name, size, stdin) and scanf_s instead of scanf
    Last edited by however; 12-14-2022 at 03:32 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    If you are compiling on a Linux computer, WHY are you using "KDevelop" instead of gcc???
    gets instead of fget(name, size, stdin)
    gets() has been removed from the later C Standards, and any up to date compiler including gcc!!! I assume you actually meant fgets().

    Now, if you are attempting to learn from painfully inadequate online tutorials, YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach, 2nd Edition
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    KDevelop is an IDE and probably is using gcc as the compiler, I'm not sure what compiler VSCode is actually using on Linux.

    By the way, as already stated above, current versions of gcc are using C11 without the "optional" "safe" functions like scanf_s(), and gets() has been removed from this standard so there should not be any support for that function.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You need to check whether the word will fully fit before modifying the puzzle, otherwise you will get partially-inserted words.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Dec 2022
    Posts
    6
    Thanks everyone,
    yes KDevelop is an IDE that uses the last 'gcc' to build and compile code.
    rstanley
    If you are compiling on a Linux computer, WHY are you using "KDevelop" instead of gcc???

    gets instead of fget(name, size, stdin)


    gets() has been removed from the later C Standards, and any up to date compiler including gcc!!! I assume you actually meant fgets().
    my bad explaining. I meant that 'my ide' returns an error when using gets; and it likes fgets(name, size, stdin) better.

    Anyhow, from that code, the crossoword does not get populated with any words, just random characters
    self-taught newbie searching for help-crossword_output-png
    however, if/when I remove the putVerticalWord and putDiagonalWord functions, it DOES work adding only horizontal words (probably, explaining myself badly again). This is a screenshot I took before getting rid of option 2 and 3 in the menu' (since the menu was not required, i thought of adding one to make it more user friendly not considering that it also made my life much more complicated, so i only decided to have 2 options: 1. Play and 2. Exit)

    I don't think this is related to IDE or compiler; surely I am missing something trivial in the code.

    p.s.:
    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.
    thank you for the suggestions, i will definitely look at those (I also wish a day was made of 25hrs ).

    Regards,

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    my bad explaining. I meant that 'my ide' returns an error when using gets; and it likes fgets(name, size, stdin) better.
    That's because gets() is a very dangerous function that can never be used safely so the C standards committee has removed gets() from the standard so it is no longer available. fgets() is much safer and you should use this function in place of the dangerous gets() function.

  7. #7
    Registered User
    Join Date
    Dec 2022
    Posts
    6
    Thank you for the hint (i, somehow, had learned that)

    I am still thankful for any suggestions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 10 Reasons Why Self-Taught Engineers are the Best in the World
    By anthonyemuobo in forum General Discussions
    Replies: 23
    Last Post: 12-08-2015, 12:28 AM
  2. who is self taught among u ppl?
    By Kuro Tensai in forum Tech Board
    Replies: 40
    Last Post: 02-26-2013, 10:55 AM
  3. who is self taught among u ppl?
    By Kuro Tensai in forum C++ Programming
    Replies: 29
    Last Post: 02-17-2013, 06:15 AM
  4. Anyone purely self-taught?
    By guitarscn in forum A Brief History of Cprogramming.com
    Replies: 103
    Last Post: 03-15-2008, 04:58 AM
  5. Will the upcoming C++ standard change how Programming languages are taught
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-14-2008, 01:07 PM

Tags for this Thread