Multiplication program: need help (beginner to C++)
Hey everyone, I'm 12 going on 13 in October and I need some help. Since I just learned about if/and statements in C++ For Dummies, I decided to make a multiplication program. Though I do need some help.
The whole concept is that there are 100 multiplication problems shown one at a time on the screen with 20 minutes total to complete them. So, say my little sister types the answer to 6X5 but puts 25, I want it to still go to the next problem and tell you your score out of 100. I was just wondering if you guys at GameDev.net could teach me how to have a 20 minute time limit incorporated in my program, how they can display the score at the end, and randomize the multiplication problems given so its different almost every time. And maybe some tricks to save me some typing . I know that I should use those IF statements to display if they got it right then they move to the next problem and the next. I'll be screwng around on Visual Basic C++ if I'm on the right track. Please help me out, I'm desperate :D.
I know it isn't nice to be flat-out asking you but I'd really appreciate some help. I know pretty much how to make the program minus the time limit, the score card, and randomizing the numbers. Thanks for making the effort for any input at all!
[edited by - Manny Calavera on September 7, 2002 1:19:40 PM]
Well, I'm not sure how much you know, so I can't say for sure this will help you, but here are some functions you might find useful, anyway.
time_t time( time_t *timer); // found in time.h
this function will return the time. So you call it once when the application starts, and whenever you feel the need you can call it again, and compare it with the old time. When the return value is greater than or equal to 20*60, you know twenty minutes have passed.
void srand( unsigned int seed ); // found in stdlib.h
int rand( void ); // also in stdlib.h
You can use srand (give it the time as a parameter) to seed the random number generator, and then you can use rand to produce a random number. If you want to produce a random number smaller than 40, you'd do this:
srand(time(NULL));
int number = rand()%40;
This may not be the best way to do it, but for now I'd say it's the easiest, and certainly good enough for the program you describe.
As for the score, all you'd need to do (in that case, anyway) is have one variable, and have it increment each time the user gets a problem right. i.e.:
int answer;
cout << "6x5 = ";
cin >> answer;
if (answer == 30)
score++;
And then when the program ends (either due to having answered all the questions, or time running out you'd simply do this:
cout << "Your score was " << score << endl;
Hope some of this helps!
-Arek the Absolute
[edited by - Arek the Absolute on September 7, 2002 2:01:36 PM]
time_t time( time_t *timer); // found in time.h
this function will return the time. So you call it once when the application starts, and whenever you feel the need you can call it again, and compare it with the old time. When the return value is greater than or equal to 20*60, you know twenty minutes have passed.
void srand( unsigned int seed ); // found in stdlib.h
int rand( void ); // also in stdlib.h
You can use srand (give it the time as a parameter) to seed the random number generator, and then you can use rand to produce a random number. If you want to produce a random number smaller than 40, you'd do this:
srand(time(NULL));
int number = rand()%40;
This may not be the best way to do it, but for now I'd say it's the easiest, and certainly good enough for the program you describe.
As for the score, all you'd need to do (in that case, anyway) is have one variable, and have it increment each time the user gets a problem right. i.e.:
int answer;
cout << "6x5 = ";
cin >> answer;
if (answer == 30)
score++;
And then when the program ends (either due to having answered all the questions, or time running out you'd simply do this:
cout << "Your score was " << score << endl;
Hope some of this helps!
-Arek the Absolute
[edited by - Arek the Absolute on September 7, 2002 2:01:36 PM]
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Thanks Arek! Could you please clarify with your time and score code. I understand most of the score code but it just keeps saying score is not initialized. This is my practice code:
#include <stdio.h>
#include <iostream.h>
int main(int arg, char* pszArgs[])
{
int p1;
int score;
cout<<"6 X 4=: ";
cin>>p1;
if (p1==24)
{
score++
cout<<"Good job!\n";
}
else
{
cout<<"WRONG!\n";
}
{
cout << "Your score was " << score << endl;
}
return 0;
}
Theres a problem with score!
#include <stdio.h>
#include <iostream.h>
int main(int arg, char* pszArgs[])
{
int p1;
int score;
cout<<"6 X 4=: ";
cin>>p1;
if (p1==24)
{
score++
cout<<"Good job!\n";
}
else
{
cout<<"WRONG!\n";
}
{
cout << "Your score was " << score << endl;
}
return 0;
}
Theres a problem with score!
First off, don''t use iostream.h, use iostream. iostream.h is deprecated.
Then add std:: before all your cout''s, endl''s and cin''s.
Set score to 0 before doing anything. After all, the player starts with no points.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Then add std:: before all your cout''s, endl''s and cin''s.
Set score to 0 before doing anything. After all, the player starts with no points.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Thanks both Arek and siaspete! The program works smoothly!! Thanks for your help.
*Arek and siaspete are awarded a cookie.*
EDIT: Is there anyway I could make a 100 problems without repeating the SAME process for just ONE problem?
[edited by - Manny Calavera on September 7, 2002 3:49:58 PM]
*Arek and siaspete are awarded a cookie.*
EDIT: Is there anyway I could make a 100 problems without repeating the SAME process for just ONE problem?
[edited by - Manny Calavera on September 7, 2002 3:49:58 PM]
Hmmm... I got bored, so I wrote my own little version of your program. How's this look?
Oh. And if you wanted to figure it out on your own, I'd advise you don't look at this. I just figure sometimes its nice to have something like this when it gets frustrating.
[edit-] Just cleaning up a few things in the code... Sorry I didn't notice your response, Manny. I figure I'll leave this up though, in case you want to see how I would do it. And yes, I know I shouldn't technically do iostream.h, just iostream, but... Eh.
-Arek the Absolute
[edited by - Arek the Absolute on September 7, 2002 3:59:34 PM]
Oh. And if you wanted to figure it out on your own, I'd advise you don't look at this. I just figure sometimes its nice to have something like this when it gets frustrating.
[edit-] Just cleaning up a few things in the code... Sorry I didn't notice your response, Manny. I figure I'll leave this up though, in case you want to see how I would do it. And yes, I know I shouldn't technically do iostream.h, just iostream, but... Eh.
// include all the headers needed#include <iostream.h>#include <time.h>#include <stdio.h>#include <stdlib.h>int main(){ long start_time = time(NULL); // store the starting time int score = 0; // this will hold the score // loop through 100 questions for (int loop = 0; loop < 100; loop++){ // seed the random number generator, and generate two random numbers srand(time(NULL)); int num1 = rand()%100; int num2 = rand()%100; // print the question cout << num1 << " x " << num2 << "= "; // get the user's answer int answer; cin >> answer; // test if the user was right if (answer == num1*num2){ cout << "Correct!" << endl; score++; // increment the score if so } else { cout << "Wrong!" << endl; } // if time has run out, break the loop if ((time(NULL)-start_time) > 20*60){ cout << "Time up!" << endl; break; } } // now that everything's over, print the score cout << "Your score was " << score << endl; return 0;}
-Arek the Absolute
[edited by - Arek the Absolute on September 7, 2002 3:59:34 PM]
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
I find score++ not such a good practice, try score+=1.
And I think you need to initialize score like: int score=0;
ALthough Im just fucking my ass off, so dont listen to me.
And I think you need to initialize score like: int score=0;
ALthough Im just fucking my ass off, so dont listen to me.
quote: Original post by Pipo DeClown
I find score++ not such a good practice, try score+=1.
And I think you need to initialize score like: int score=0;
ALthough Im just fucking my ass off, so dont listen to me.
He is. Most programmers use ++. I suggest you do the same. It won''t make any difference to how the program actually runs, but other programmers will easily recognize that you''re incrementing the score.
Don''t listen to me. I''ve had too much coffee.
quote: Original post by Manny Calavera
2 errors in your program it says! bwhahahaah, Arek is fallible
Heh. I just fixed it. In my copy ''n pasting I missed a #include. It should work fine now.
-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement