Like Tree1Likes
  • 1 Post By memcpy

Word based game - function problem

This is a discussion on Word based game - function problem within the C Programming forums, part of the General Programming Boards category; Hi there I'm creating a word based game, whereby characters are read from a file until a comma is received, ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    Word based game - function problem

    Hi there

    I'm creating a word based game, whereby characters are read from a file until a comma is received, and then placed into a string. The string is then displayed for a short space of time before clearing. After the user has to spell back what they have seen.

    I have come across a problem in the void wait() function which has an error code of 2143, and states I need a semi colon, if some one could shed some light on this i would be thankful, as I believe that the right syntax has been enforced.

    Many Thanks

    Code:
    #include <stdio.h> // libraries declared
    #include <string.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    #define WORD_SEPERATOR ',' // end of word signified by a comma
    #define Sentinel 1 // condition for exiting a do while loop
    
    
    int difficultyfunction();        //function prototypes declared
    char getword();
    void wait (int seconds);
    void clearscreen(int difficultyresponse);
    int answer(char collectword[100]);
    int result(int percentage);
    
    -----------------------------------------------------------------
    #include "wordgame_header.h" // libaries and function prototypes defined in the header file
    
    
    int main()
    {
    int response = 0;
    int x = 0;
    int i;
    int* score = 0;
    int turns = 3;
    int count = 0;
    char* word[100];
    
    
        printf("QUICK SPELL\n");
        
        do
        {
            count++;
            response = difficultyfunction();
                
                for(i=0; i<3; i++)
                {
                    getword();
                    clearscreen(response);
                    *score = answer(word[100]);
                }
            
            printf("To quit press 1, or press any other button to play again\n");
            scanf("%d",x);
        }
        while (x != Sentinel); // will play game again if the answer is not 1
    
    
        turns = (turns*count); 
        *score = (*score / turns)*100;
    
    
        result(*score);
    
    
        return 0;
    }
    -----------------------------------------------------------------
    
    #include "wordgame_header.h" 
    
    
    int difficultyfunction()
    {
        int difficultyselect = 0;
    
    
            do {
                printf("Please Select Difficulty\n");
                printf("\t Press 1 for Easy\n");
                printf("\t Press 2 for Medium\n");
                printf("\t Press 3 for Hard\n");
                scanf("%d",&difficultyselect);
                }
                while(difficultyselect != 1 && difficultyselect != 2 && difficultyselect != 3);  
    
    
        return difficultyselect; 
    }
    
    
    char getword()
    {
        char* collectword[100];
        FILE *fp;
        int len = 0;
        int i;
                
                    fp = fopen("dictionary.txt", "r"); 
                    
                    while((fgetc(fp)) != WORD_SEPERATOR){
                        for (i = 0; i<99; i++)
                        {
                            *collectword[i] = fgetc(fp);
                        }
        
            len    = strlen(*collectword) -1;
        
            for(i = 0; i < len; i++){
                printf("%s", collectword[i]);
        }
        return 0;
    }            
    
    
    void wait (int seconds)
    {
        clock_t endwait;
        endwait = clock () + seconds * CLOCKS_PER_SEC ;
        while (clock() < endwait) {}
    }
    
    
    void clearscreen(int difficultyresponse)
    {
        int n;
    
    
            switch (difficultyresponse)
            {
    
    
            case 1:        for(n = 1; n > 0; n--)                            /*1 seconds clear screen*/    
                        {    wait(1);
                            system ("cls"); break;}                        /*end case*/
        
            case 2:        for(n = 2; n > 0; n--)                            /*2 seconds clear screen*/
                        {    wait(2);
                            system ("cls"); break;}                        /*end case*/
        
            case 3:        for(n = 3; n > 0; n--)                            /*3 seconds clear screen*/    
                        {    wait(3);
                            system ("cls");    break;}                        /*end case*/
            }    
    }
    
    
    
    
    int answer(char collectword[100])
    {
        int score = 0; 
        char answer;
    
    
        printf("Please input answer\n",&answer);
    
    
            if(*collectword == answer)
            {printf("congratulations you have got the answer correct!\n");
            score++;}
        
            else
            {
                system ("cls");
                printf("The answer is incorrect\n");
                printf("You answered: %s\n",answer);
                printf("The correct answer was: %s\n", *collectword);
            }
    
    
        return score;
    }
    
    
    int result(int percentage)
    {
        
            FILE * Output;
            Output = fopen("results.txt", "a+");
            fprintf(Output, "\n%d%", percentage);
            fclose(Output);
        
        return 0;
    }
    Last edited by openwindow; 03-03-2012 at 02:10 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm not going to search your entire code looking for where you missed a semicolon.

    However, you need to fix the do-while/scanf loop. Sentinel is a value of 1, whereas the character '1' is (0x30, i think?). So, even if you did scanf correctly (change %d to %c), it would still be incorrect because 1 =/= '1'.
    Nyah Check likes this.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    194
    in the char getword() function you opened a file and did not close at after reading the characters so it would generate bugs even though the code may run. also , use the ferror () and feof() functions to determine the errors as the code executes instructions from the files. hope it helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By beene in forum Game Programming
    Replies: 7
    Last Post: 07-09-2007, 03:39 AM
  2. Non-Frame based game
    By zacs7 in forum Game Programming
    Replies: 2
    Last Post: 04-15-2007, 06:41 AM
  3. text based game
    By PippinofTook in forum Game Programming
    Replies: 5
    Last Post: 08-24-2005, 06:56 PM
  4. 2D Tile Based Game
    By cboard_member in forum Game Programming
    Replies: 6
    Last Post: 07-29-2005, 01:04 AM
  5. Text-Based Game Help
    By Sentral in forum Game Programming
    Replies: 6
    Last Post: 07-01-2005, 01:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21