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...
}
what did you mean with this smile?
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? :(
Re: People, this is my second day with C.
Quote:
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.
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.