Random Number Guessing Game

This is a discussion on Random Number Guessing Game within the C Programming forums, part of the General Programming Boards category; Code: #include <conio.h> #include <time.h> #include <stdlib.h> #include <stdio.h> #define MAX 100 int GetGuess(); void random(); int play(int guess1, int ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Random Number Guessing Game

    Code:
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <stdio.h>
    #define MAX 100
    
    
    int GetGuess();
    void random();
    int play(int guess1, int answer1);
    int compare(int guess1, int answer1);
    
    
    int guess1;
    int answer1;
    
    
    int main()
    {
        int answer;
        random();
        FILE *inptr;
        inptr = fopen("File1.txt","r");
        answer1 = fscanf(inptr,"%d", &answer);
        play(guess1,answer1);
        getch();
    }
    int play(int guess1, int answer1)
    {
        guess1 = GetGuess();
        compare(guess1, answer1);
        
    }
    
    
    int GetGuess ()
    {
        int guess;
        printf("Enter your guess:\n");
        scanf("%d",&guess);
        return guess;
    }
    
    
    int compare(int guess1, int answer1)
    {
        if (answer1 == guess1)
        {
            printf("Congratulations you are a lucky winner\n");
        }
        else
        {
            printf("not working");
        }
        
    
    
    }
    
    
    void random()
    {
        //declare variables
        int i;
        int num;
        FILE *bzvz;
        //open file to save numbers
        bzvz=fopen("File1.txt","w");
        srand(time(NULL));//seeds the number generator to randomize
        //for(i=0;i<20;i++)
        //{
            num = rand()%MAX +1;//calculates a random number from 1 and 10
            fprintf(bzvz,"%d ",num);
        //}
        fclose(bzvz);
    }


    I am completely stuck when assigning values to and passing variables around. It seems that the problem occurs with fscanf function. I have also guessed that it might be passing and reading it as a character, even though I said it will be an integer, and tried atoi() with no luck. I troubleshooted the error as I tried to print the given values after I assign it, with no luck. I am doing this for a friend I have recently been programming django, so I am completely out of the loop when it comes to C. Can someone please help me diagnose or point me to where I am assigning converting passing in a wrong way?

    Thank you.
    Last edited by Borko Kovacev; 02-13-2013 at 02:01 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > answer1 = fscanf(inptr,"%d", &answer);
    > play(guess1,answer1);
    answer1 is the success/failure status of the fscanf call.
    Since you're trying to read one thing, possible values for answer1 are 1, 0 or EOF

    answer (only if answer1 == 1) is the number read from the file.
    You probably want to use this number to play.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Borko!

    line #24 is a problem. You are putting the value from the file, into answer -- and then you never use it. The answer1 variable is going to be given the number of items that fscanf() has scanned and assigned a value to.

    So change &answer in that line, to &answer1, and let answer get assigned the value from fscanf(), on the left side of the equal sign.

    Since guess1 is global, you don't have to pass it in a parameter, and you can assign it a value, in any function. GetGuess() doesn't have to return the guess, it can be assigned to guess1, right inside GetGuess.

    See how that works, now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Guessing Game
    By iMattbckr in forum C++ Programming
    Replies: 14
    Last Post: 07-26-2012, 04:00 AM
  2. Making a random number guessing game (exit failure? wtf)
    By muffinman8641 in forum C++ Programming
    Replies: 36
    Last Post: 03-04-2011, 09:09 AM
  3. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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