Thread: Very simple question, problem in my Code.

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Very simple question, problem in my Code.

    Hi, I tried to build a simple program that generates some math questions. This is one of my first's program in C, so I dont know if its ok, organized, I just know that I got an error:

    I can compile the program perfectly on Dev-C++ and Borland, but when the program starts to generate the math-questions he didnt generate an rand number, he generate a giant number...

    Someone knows what is the problem here?
    And if you guys got some tip how to improve this code, please tell me, this is just my second day on C.

    Thanks.

    The code:
    Code:
    /*
      Name: Math-That, math game
      Author: Isaac Elbaz
      Description: Math-That, is a game of questions about math and more
      Date: 17/11/02 20:00
      Copyright: Make free use of the code, change it :)
    */
    
    #include <stdio.h>  //Input and outputs functions
    #include <stdlib.h> //Use system(), rand(), srand() and so on..
    #include <conio.h>  //clrscr(), gotoxy() and more..
    #include <time.h>   //needed to use "time" function and more.
    
    //Constants declaration, some configurations.
    #define POINTS_PER_QUESTION 10 //Points per correct question
    #define LOW 50 //Low level
    #define HIGH 100 //Hight Level
    
    //Functions prototypes
    int rnd(const int max); //Function the generates random numbers.
    void msleep(const int SECONDS); //Generates some delay.
    void print_points(const int POINTS); //Print the points of the player
    void print_menu(void); //Print the menu
    void start_engine(void); //Starts the engine
    void blink(int mode); //Blink some text in the window
    void print_end(void); //Print the end of the program
    
    //Variable declaration
    int level, answer, points, rNum1, rNum2, max_questions;
    int curr_question, corr_answer; //Current question, correct
    //Level, Answer, total points, random number 1, random number 2, max questions
    
    
    int main(void) //Starts the program
    {
        srand(time(NULL) + getpid());
        //With this, we can make "real" random numbers, Just INITIALIZE ONCE!
        
        print_menu(); //Print the menu option, and configure some needed things.
    START:            //This will "mark" the start of our game
        start_engine(); //This starts our game-engine.
        
        return (0); //The end of our simple game.
        
    }
    
    int rnd(const int max)
    {
        return((rand()%max)); //Use the function rand to generate a random number
                              //And then return-it (the random number)
    }
    
    void msleep(const int SECONDS)
    {
        time_t delay;
        delay = time(NULL) + SECONDS;
        while(time(NULL) < delay) //Generates the delay
             ;
    }
    
    void print_points(const int POINTS)
    {
        gotoxy(55,1); //Move the cursor to the top-left of the console
        printf("Score: %d\n",POINTS); //Write the score of the player
    }
    
    void print_menu(void)
    {
        
        printf("Hello, welcome to Math-That, a simple math game made in C\n");
        printf("Now I need to ask you some questions...\n\n");
        
        //Receive the max questions of the game
        printf("How Many questions at all game? (I.E: 10)\n");
        scanf("%d",&max_questions);
        
        //The level of the questions
        printf("What level of questions? (1-2)\n");
        scanf("%d",&level);
        
        //We got all the info... 
        msleep(1); //Delay one second before exiting, giving time to the user 
        //read the message
        printf("\nO.K, you're ready to play, good luck!\n");
        
    }
       
    void start_engine(void)
    {                
     PERGUNTA:
          //Check the level of the game, if 1
        //generate more simple questions, else if 2
        //generates more complexes questions
        
        if(level == 1) {
            rNum1 = rnd(LOW);
            rNum2 = rnd(LOW);
        }
        
        else {
            rNum1 = rnd(HIGH);
            rNum2 = rnd(HIGH);
        }
        
        corr_answer = rNum1 * rNum2; //The correct answer
        //Start generating the questions
        do
        {
            clrscr(); //Clear the screen
            print_points(points); //Print the score
            printf("\n%d X %d = "); //Print the question
            scanf("%d",&answer);
            
            if(answer == corr_answer) { //If correct...
                blink(1); //Blink correct...
                points += POINTS_PER_QUESTION;  //Make the score
                ++curr_question; //Move to the next question
                
            }
            
            else { //Incorrect
                blink(2); //Blink incorrect
                //points -= POINTS_PER_QUESTION; //Unremark if you want that an
                //Incorrect answer down points from the score
                printf("\nThe correct answer is: %d\n",answer);
                ++curr_question; //Move to the next question
                
            }
            
            if(curr_question == 10) {
                blink(3); //Blink end-of-game
                print_end(); //Print the end of the program
            }
            
            goto PERGUNTA;
        } while(answer != corr_answer);
    }
    
    void blink(int mode)
    {
        int i; //Just for the index use...
        
        if(mode == 1) { //Print correct
          for(i = 0; i <= 3; i++) {
            clrscr(); //Clear the monitor
            textcolor(RED); //Change the font color to red
            gotoxy(35,10); //Go to this coordenate
            printf("Correct!"); //print correct
            msleep(1); //delay of 1 second
            textcolor(WHITE); //change font color to white
            gotoxy(35,10); //go to this coordenate
            printf("Correct!"); //Print correct (now in white)
            msleep(1); //delay 1 second
          }
        }
          
          if(mode == 2) { //Same as one but now prints Incorrect
            for(i = 0; i <= 3; i++) {
            clrscr();
            textcolor(RED);
            gotoxy(35,10);
            printf("Incorrect!");
            msleep(1);
            textcolor(WHITE);
            gotoxy(35,10);
            printf("Incorrect!");
            msleep(1);
            
          }
        }
        
        if(mode == 3) { //Same as te others, now print END OF THE GAME!!!
          for(i = 0; i <= 3; i++) {
            clrscr();
            textcolor(RED);
            gotoxy(35,10);
            printf("END OF THE GAME!!!");
            msleep(1);
            textcolor(WHITE);
            gotoxy(35,10);
            printf("END OF THE GAME!!!");
            msleep(1);
          }
        }
    }
    
    void print_end(void)
    {
        clrscr(); //Clear the screen..
        gotoxy(35,10);
        printf("Thanks for using this program.. hope you like it\n");
        printf("Fully programmed in C with Dev-C++\n");
        printf("Thanks for the guys from Cprogramming.com for the fast and perfect\
        answers.. Thanks!\n");
        msleep(5); //Delay 5 seconds
        exit(1);   //Exit...
    }

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    And if you guys got some tip how to improve this code, please tell me, this is just my second day on C.
    Yeah right
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    what did you mean with this smile?

    ...

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    second day of C aint even "Hello, world!\n" yet
    hello, internet!

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    People, this is my second day with C.

    whats the problem with the code? this isn't my first programming language. I worked and work with js+php more than 3 years.
    we just have some if-elses statements on the code thats it.

    Ahh, and the most difficults parts of the problem was the delay and the random, and this things I didnt knew, I asked here...

    Hmm, so no one got an solution to the problem?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: People, this is my second day with C.

    Originally posted by Vber
    whats the problem with the code?
    ...
    Hmm, so no one got an solution to the problem?
    The problem is that no one here wants to read two hundred lines of code to pinpoint your problem. If you've done any programming before, you should be well versed with debugging.

    Break your program down into small programs that test each function one at a time. Or sprinkle printf statements throughout your functions which display the values of the variables so you can debug by catching problems that way.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Unhappy quzah... I tried.. and ther most interessant

    that I did the same thing: srand(time(NULL) + getpid());
    in another program and he generated perfectly random numbers, Ok, thanks for helping.

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Ohh Thanks :)

    I knew it that I got a newbie error on the code.
    Salem thanks a lot for looking at the code!

    You made me the day, or better the night
    Thanks!
    Last edited by Vber; 11-16-2002 at 04:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple code question
    By potatopuff in forum C Programming
    Replies: 7
    Last Post: 07-15-2008, 01:31 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 06:27 AM
  4. Large code Simple Question need help
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 10-25-2002, 07:55 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM