Thread: Defenition Game not working with score

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    1

    Unhappy Defenition Game not working with score

    Hello everyone, I am making a game in C for defenitions it works fine except the score which I cannot get to work. It might not be very efficient but its simple...
    Here is my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        system("mode con cols=150 lines=100"); //Non-important code. Used for axi below
        printf(" ______   _______  _______  _______  __    _  ___   _______  ___   _______  __    _ \n");
        printf("|      | |       ||       ||       ||  |  | ||   | |       ||   | |       ||  |  | |\n");
        printf("|  _    ||    ___||    ___||    ___||   |_| ||   | |_     _||   | |   _   ||   |_| |\n");
        printf("| | |   ||   |___ |   |___ |   |___ |       ||   |   |   |  |   | |  | |  ||       |\n");
        printf("| |_|   ||    ___||    ___||    ___||  _    ||   |   |   |  |   | |  |_|  ||  _    |\n");
        printf("|       ||   |___ |   |    |   |___ | | |   ||   |   |   |  |   | |       || | |   |\n");
        printf("|______| |_______||___|    |_______||_|  |__||___|   |___|  |___| |_______||_|  |__|\n");
        printf("------------------------------------------------------------------------------------\n");
        printf("\nIn this game the objective is simple! You will be given a defenition! You must work out the correct word!\nType it in on your keyboard!\n");
        printf("\nOne more rule you CANNOT add spaces in it\n");
        game();
        return 0;}
    void game(){
    char *words[] = {"Potato", "McDonalds", "Mafia", "Xbox", "Computer", "Star Wars", "HTML", "BBC", "USB", "Watch", "Door", "Window", "Dog", "Obama", "Derby", "College", "BOP", "test1", "test2", "test3`" };
    char *questions[] = {"Can be mash or roast", "Best fast food chain", "Italian notirious gang", "Gaming console by Microsoft", "Like a lapotop but a tower", "7 films featuring han solo, luke skywalker etc.", "langauage for programming websites", "Channel 1 on TV", "Portable device we use at college", "Something you wear on your wrist to tell the time", "Use this to get in and out of houses", "You look through these in houses", "A pet that goes ruf ruf", "First African American president of America", "Where we live", "Where we go everyday for torture", "Chicken side", "test1", "test2", "test3"};
    char answer[20][255];// Answer array - for more questions increase first bracket
    int score =0;
    int vec[20] = { 0 };
    int i, j, word;
    srand(time(NULL));//Function for creating random numbers
    
        for (i = 0; i < 20; i++) {//Creates the position in the array for the defenition
            int okay = 0;
            while (!okay) {
                vec[i] = rand() % 20;//20 is place holder - Increase for more questions
                okay = 1;
                for (j = 0; j < i; j++) {
                    if (vec[i] == vec[j]) okay = 0;
                }
            }
            word=vec[i];
            printf("%s\n",questions[word]); //print a question
            scanf("%s",answer[i]);// wait for the user to type the word
            printf("%s\n",answer[i]);// show what the user typed - will be used later!!!!!!!!!!!!!!!!!!!!!!!
            printf("\nit was %s\n\n",words[word]);//show the answer
            printf("\n\nYour score is %d\n", score);//show the score
    
    
        }
    }
    This is the actual code for the score
    if (words[word] == words[word]) {
    score++;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You cannot compare strings with == in C. Look into "strcmp" instead.

    A few additional notes:
    • The correct spelling is "definition".
    • You need to declare your functions before "main" if they're defined after "main".
    • "srand" should only be called once, near the beginning of the program. (You actually are only calling it once, but if you put "game()" into a loop, then you would be calling it each iteration.)
    • You need to include "time.h" for the "time()" function.
    • Avoid magic numbers like 20. Instead, use named constants.
    • It appears your "while(!okay)" loop is trying to fill an array with all the valid numbers in random order. It would be more efficient to just fill them with 0 - 19, then perform a shuffle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help On Keeping Score For Dice Game
    By AdAstra in forum C Programming
    Replies: 1
    Last Post: 03-18-2011, 06:08 PM
  2. Game score increasing rapidly....
    By sunnyli in forum Game Programming
    Replies: 3
    Last Post: 11-03-2010, 10:15 PM
  3. Simple Dart Score Program Not Working Correctly, Help Please.
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 09-25-2010, 11:57 PM
  4. High Score Not Working...
    By pbjorge12 in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2005, 01:17 AM

Tags for this Thread