Thread: what is wrong with my function? my pointer is failed :"(

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    what is wrong with my function? my pointer is failed :"(

    i am making a guess game.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    void operation(int magicNumber, int guessnumber);
    void getNumber(int *try, int *yourguess, int *magicNum);
    int getMagicNumber(void);
    int main(void) {
        int guessnumber = 0;
        int numberOfTry = 1;
        int magicNumber = 0; 
        magicNumber = getMagicNumber();
        printf("magic number is %d\n", magicNumber);      // for test purpose only
        getNumber(&numberOfTry, &guessnumber, &magicNumber);  
    
        system("PAUSE");	
        return 0;
    }
    
    int getMagicNumber(void) {
        int number;
        srand(time(NULL));
        number = (1 + rand()%20);
        return number;
    }
    
    void getNumber(int *try, int *yourguess, int *magicNum){
         printf("\nI am thinking of a number between 1 and 20.\n");
         if (*try > 5)
           printf ("You heave tried 5 times. The magic number is: %d\n", *magicNum);
         else
           scanf ("Can you guess what it is?\n", yourguess);
           int x = *yourguess;
           printf("your guess number is %d\n", x);
           printf("Your guess number is %d. \n", *yourguess);
         *try = *try + 1;
         printf("\nYou have tried %d time(s).", *try);
        // you don't put asterisk in front of the argument because they are already address.
        // so you just pass them as address. Operation's paramter accept variable address.
        operation(*magicNum, *yourguess);   
    }
    
    void operation(int magicNumber, int guessnumber) {
         if (magicNumber > guessnumber)
            printf ("\nYour guess number is greater than magic number\n");
         else if (magicNumber < guessnumber)
             printf ("\nYour guess number is less than magic number\n");
         else
             printf ("\nCongradulation. You win the guess game with the magic number %d.\n", magicNumber);
    }
    magic number is 8

    I am thinking of a number between 1 and 20.
    2
    your guess number is 0
    Your guess number is 0.

    You have tried 2 time(s).
    Your guess number is greater than magic number

    When i enter 2, value isn't assigned to the variable in main method. I don't know why the pointer is failed.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    scanf ("Can you guess what it is?\n", yourguess);
    You're using scanf() wrong. Here's how to use it: scanf [C++ Reference]
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    2
    Quote Originally Posted by cpjust View Post
    Code:
    scanf ("Can you guess what it is?\n", yourguess);
    You're using scanf() wrong. Here's how to use it: scanf [C++ Reference]
    thank you. cpjust.
    my program works now :"D

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your program is broken in other ways as well. srand should only be called once during the execution of your program, not every time you need a random number. Just move that line into main.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  3. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 05:43 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM