hii friends..Am new to this community... am a computer science student... I need your help in creating a small c++ game... Its a project for me... I am told to do it in turbo c++...

okay... the game is so simple... A number memory game... random numbers should be displayed and the user/gameplayer must type in that number very quickly..there must be a time limit for that... the game continues till the user makes a wrong answer... there must of course be a scoring key... As i told you am very new to object oriented concepts... I just started doing small programs using class... I created a small one... it says there are 5 errors- protection errors... pls help me..here is the code..hoping to hear from you all...

#include <stdlib.h>
#include <iostream.h>
#include<conio.h>
class game
public:
int num;
    void dispnum();
    void getnum();
    void result();
 };
 void game::dispnum()
   {
     srand((unsigned)time(0));
    int random_integer = rand();
    cout << random_integer << endl;
}
void game::getnum()
   {
    cin>>num;
   }
   void game::result()
   if  random_integer==num;
   cout<<"correct answer";
   else
   cout<<"wrong answer";
   }
void main()
clrscr();
     game gam1,gam2,gam3;
     gam1.dispnum();
     gam2.getnum();
     gam3.result();
     getch();
     }

i dont know how to add time limit or a scoring key..hope u would help me... plsss

Recommended Answers

All 62 Replies

First of all, A lot of people on the internet have a problem when they read in red coloured text.

So i prefer you use [code] Tags...


Secondly, are declaring 3 variables (of your class) and then running a separate function on each of them.

As far as time comes into consideration .

http://www.cplusplus.com/reference/clibrary/ctime/

This link will help you out for sure.

Next.

Your Program should go on until the user gives a wrong answer , So you should try creating another function and use bool flags to stop it if a wrong answer is given.

Hii..Thank you some much for you tip... I didnt think about that colour problem... could anyone please create that game for me?... moreover what is this protection error?.... pls help me..its very urgent...I want this game in 2 weeks.. I dont know much it. am in big trouble

i dont know how to use bool flags...

Well No body here will be writing code for a homework. Our Main Objective is to provide help And Thats it.

So Just go into the source code and read it carefully, Then i guess you will find your mistakes.

I went through your code and found all of the mistakes.

Will be helping only IF you show effort.

Okay..I guess you are right..I must find my mistakes...Am so glad to knw that u will all help me... tomorrow i will post again with the modified code... but you see am an amateur programmer ...I just started doing c++ programs.. anyway tomorrow i will try and post am improved sourse code.. bye for now...thanks a ton 4 ur assistance ..tc

Okay..I guess you are right..I must find my mistakes...Am so glad to knw that u will all help me... tomorrow i will post again with the modified code... but you see am an amateur programmer ...I just started doing c++ programs.. anyway tomorrow i will try and post am improved sourse code.. bye for now...thanks a ton 4 ur assistance ..tc

Will Be Waiting ;)

finally I corrected most of the errors in the program...now its running..but there is some problem with the if statement.it is always showing "wrong answer "even if the answer is correct..what should i do now? am so confused.but many errors are cleared now..so sky diploma, i have made an effort..now please give me a clue..what what can i do to add a time limit,scoring key ,and to repeat the game untill the user makes a wrong entry.. how can add a loop in this case?. :-/

:S

here is the new corrected version

#include <stdlib.h>
#include <iostream.h>
#include<conio.h>
class game
{
public:
int num,random_integer;
    void dispnum();
    void getnum();
    void result();
 };
 void game::dispnum()
   {
     srand((unsigned)time(0));
     random_integer = rand();
    cout << random_integer << endl;
}
 void game::getnum()
   {
    cin>>num;
   }
 void game::result()
  {
   if (random_integer==num)
   {
      cout<<"correct answer";
   }
      else
   {
        cout<<"wrong answer";
   }
   } 
void main()
{
clrscr();
     game gam1,gam2,gam3;
     gam1.dispnum();
     gam2.getnum();
     gam3.result();
     getch();
     }

No red colour this time.... anyway pls help me

No red colour this time.... anyway pls help me

No red color, but also no CODE-TAGS. Learn to use them by clicking the link.

Next a few things:
- code formatting
- void main/int main/main
- getch() isn't standard C, getchar() is.
- clrscr();

And now for your problem. This piece of code..

game gam1,gam2,gam3;
gam1.dispnum();
gam2.getnum();
gam3.result();

..creates 3 classes and does stuff with them. This is wrong. You get a number for gam2 and try to give the result of gam3. But gam3 didn't get any input yet nor did gam1.
What you probably meant was:

game gam1;
gam1.dispnum();
gam1.getnum();
gam1.result();

Not only That.

You can add another function to your code

something like.

void playgame()
{
while(//Put in a boolean member over here)
{
game::dispnum();
game::getnum();
game::result();
}
}

That way You can just call

int main()
{
game gam1;
gam1.playgame();
}

Hiiii .Fantastic..!! Thank you so much..that solved the problem.. now it is detecting right and wrong answers...! am new to this concept of classes.. now what should i do to repeat the game till the users enters a wrong answer..? also how can i add time limit..? can i add a loop to the function? or a flag? how can i do that? I will try and do that.. will post very soon.. hoping that you all will be here to help me

hii sky diploma ... am so sorry i didnt see you msg in the 2nd page..! Just found it... i tried adding a playgame function.. got 1 warning and 4 errors!...
the code is

#include <stdlib.h>
#include <iostream.h>
#include<conio.h>
class game
{
public:
int num,random_integer;
    void dispnum();
    void getnum();
    void result();
    void playgame()
    {
    while(3)
    {
    game::dispnum();
    game::getnum();
    game::result();
    }

 };
 void game::dispnum()
   {
     srand((unsigned) time(0));
     random_integer = rand();
    cout << random_integer<< endl;
}
 void game::getnum()
   {
    cin>>num;
   }
 void game::result()
  {
   if (num==random_integer)
   {
   cout<<"correct answer";
   }
   else
   {
   cout<<"wrong answer";
   }
   }


void main()
{
clrscr();
     game gam1;
     gam1.dispnum();
     gam1.getnum();
     gam1.result();
     gam1.playgame();
     getch();
     }

how can i get the error log in turbo c++ v3.0 so that i could plaste it here?


the errors are :-
warning: fuctions containing while are not expanded inline
errors: multiple declaration for game::dispnum(), game::getnum(), and game::result()
and a declaration terminally incorrect at the end of the code.. //below getch()
and
} //


pls help me

why should i use getchar??... Am programming in c++ so i thought getch() is the standard one...

what are the advantages of getchar over getch?

#include <stdlib.h>
#include <iostream.h>
#include<conio.h>
class game
{
public:
int num,random_integer;
    void dispnum();
    void getnum();
    void result();
    void playgame()
    {
    while(3)
    {
    game::dispnum();
    game::getnum();
    game::result();
    }

 };
 void game::dispnum()
   {
     srand((unsigned) time(0));
     random_integer = rand();
    cout << random_integer<< endl;
}
 void game::getnum()
   {
    cin>>num;
   }
 void game::result()
  {
   if (num==random_integer)
   {
   cout<<"correct answer";
   }
   else
   {
   cout<<"wrong answer";
   }
   }


void main()
{
clrscr();
     game gam1;
     gam1.dispnum();
     gam1.getnum();
     gam1.result();
     gam1.playgame();
     getch();
     }

Make life easier for yourself and indent a little more consistently. Make it so the brackets line up and so that nested blocks are indented more than the blocks they are indented from. When you do so, you'll notice that you have no closing bracket to your playgame function. Stick a closing bracket before line 20 to close that function.

You don't have the line using namespace std; in your code, so either put that at the top or add std:: in front of all of your cin , cout , and endl statements (i.e. change cout to std::cout ).

Lines 1 and 2. You can take the .h off of both of these. Add a c in front of stdlib

#include <cstdlib>
#include <iostream>

Lines 15 through 17. This a class member function so you can leave game:: off of these function calls. The compiler already knows you are referring to game:: .

niek already mentioned void main versus int main .

Yes Just as Vernon Mentioned, You will need to remove "game:: " in the playgame() function and secondly in your main function, I guess you should remove the other functions and only keep the playgame() function. Because currently the game will run infinite times, So i guess compiling the below code will help you out.
I prefer that you examine your previous code and your current code. to see what mistakes you have commited.

Programming is all about understanding how a particular mechanism works(Consider FlowCharts). Once you understand that, it will just be a piece of cake.

#include <stdlib.h>
#include <iostream.h>
#include<conio.h>
class game
{
public:
int num,random_integer;
    void dispnum();
    void getnum();
    void result();
    void playgame()
    {
int i=0;
    while(i<=3)
    {
    dispnum();
    getnum();
    result();
i++;
    }
}
 };
 void game::dispnum()
   {
     srand((unsigned) time(0));
     random_integer = rand();
    cout << random_integer<< endl;
}
 void game::getnum()
   {
    cin>>num;
   }
 void game::result()
  {
   if (num==random_integer)
   {
   cout<<"correct answer";
   }
   else
   {
   cout<<"wrong answer";
   }
   }


int main()
{
clrscr();
     game gam1;
     gam1.playgame();
     cin.get();
}

I don't think a class is neccessary here. It's alot of overhead for doing something very simple. Instead, just use an array representing the right values, and a single buffer for value entry and comparison. To check times, use this:

#include <ctime>
#include <iostream>

int main()
{
int iVal(0);
//since it takes a bit of time to get to this state, offset the clock variable
int iCurTime = clock();
std::cout << "Enter value: ";
std::cin >> iVal;
iCurTime = clock() - iCurTime;
std::cout << "That took " << iCurTime / CLOCKS_PER_SEC << " second(s) to answer!";
std::cin.ignore();
std::cin.get();
return 0;
}

In the library ctime, clock() represents the number of clock ticks since the program execution, and CLOCKS_PER_SECOND represents the clock ticks per second (this always seems to be 1000 for some reason, although I would think it should be very much higher...).

hii thank you so much..The loops is working correctly... but as i wanted the game to end when the user makes a wrong answer i added exit(0); Also tried adding a scoring system.. both failed miserably..! its showing random numbers as score.. and the loop is not working..! exit(0); is also not working..! what might be the problem?

Here is the code

#include <stdlib.h>
   
      #include <iostream.h>
   
      #include<conio.h>

      #include<process.h>
   
      class game
   
      {
   
      public:
   
      int num,random_integer;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()
  
      {
  
      int i=0;
  
      while(i<=3)
  
      {
  
      dispnum();
  
      getnum();
  
      result();
  
      i++;
  
      }
  
      }
  
      };
  
      void game::dispnum()
  
      {
  
      srand((unsigned) time(0));
  
      random_integer = rand();
  
      cout << random_integer<< endl;
  
      }
  
      void game::getnum()
  
      {
  
      cin>>num;
  
      }
  
      void game::result()
  
      {
      int score=0;

      if (num==random_integer)

      {

      cout<<"correct answer"<< endl;
       score=score+1;
      cout<<"score = "<<score;
      }
      else

      {

      cout<<"wrong answer! Gameover!"<< endl;
      cout<<"Your final score is "<<score;
      exit(0);
      }

      }
  
       
       int main()
  
      {
  
      clrscr();
  
      game gam1;
  
      gam1.playgame();
  
      cin.get();
  
      }

exit (0) worked fine for me. It exits the program when you guess wrong. That's what you want it to do, right? Make sure you put return 0; at the bottom of main. You still need to put the std:: in front of cin , cout, and endl , and you should get rid of the .h 's on your #include statements and use the modern C++ #include statements. If you are using an old compiler, you may want to update to a newer one. I had to change some stuff around to get it to compile on mine. You said this compiled succesfully?

Stick an endl at the end of this line so you can see what is going on better:

cout<<"score = "<<score;

It runs into your random number display so you can't see what is what. But here's the problem:

void game::result()
  
      {
      int score=0;

      if (num==random_integer)

      {

      cout<<"correct answer"<< endl;
       score=score+1;
      cout<<"score = "<<score;
      }
      else

      {

      cout<<"wrong answer! Gameover!"<< endl;
      cout<<"Your final score is "<<score;
      exit(0);
      }

Line 5 - You have score declared as a local variable and you initialize it to 0 so the old score is lost. Make score a game class variable, not a local variable, and initialize it at the top of the playgame function instead. That way score 's value will be retained.

Finally, you really need to indent your code.

hiii..I am doing this programme on turbo C++ v3.0... that is why..I'll have to do this on turbo c++.. i still didnt get you..!what is wrong with score? :S hope u would reply

hiii..I am doing this programme on turbo C++ v3.0... that is why..I'll have to do this on turbo c++.. i still didnt get you..!what is wrong with score? :S hope u would reply

One, you have no newline after you display score, so if score = 1 and the random number is 23456, the two run together and you see this:

123456

Display a newline after you display score, like this, and they'll be on separate lines:

cout<<"score = "<<score << endl;

Two, as I said in the last post, you have score as a local variable and you have it so it resets itself to 0 each time the result function is called, so it can never be more than one since you keep resetting it. Make it a class variable:

class game
   
      {
   
      public:
   
      int num,random_integer, score;

Delete this line:

void game::result()
  
      {
      int score=0;   // delete this line

Add this line at the top of playgame, BEFORE the while loop:

void playgame()
  
      {
  
      int i=0;
      score = 0; // add this line
  
      while(i<=3)

That way you are initializing score to 0 only once instead of every round.

hiii... I did as you instructed... now the initial value is 8006 then its increasing by 1.. but the initial value is 8006..!! what is wrong?
here is the code

#include <stdlib.h>
   
      #include <iostream.h>
   
      #include<conio.h>

      #include<process.h>
   
      class game
   
      {
   
      public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()
  
      {
  
      int i=0;
      int score=0;
  
      while(i<=3)
  
      {
  
      dispnum();
  
      getnum();
  
      result();
  
      i++;
  
      }
  
      }
  
      };
  
      void game::dispnum()
  
      {
  
      srand((unsigned) time(0));
  
      random_integer = rand();
  
      cout << random_integer<< endl;
  
      }
  
      void game::getnum()
  
      {
  
      cin>>num;
  
      }
  
      void game::result()
  
      {


      if (num==random_integer)

      {

      cout<<"correct answer"<< endl;
       score=score+1;
      cout<<"score = "<<score<<endl;
      }
      else

      {

      cout<<"wrong answer! Gameover!"<< endl;
      cout<<"Your final score is "<<score;
      exit(0);
      }

      }

  
       
 
       
  
      int main()
  
      {
  
      clrscr();
  
      game gam1;
  
      gam1.playgame();
  
      cin.get();
  
      }

hiii... I did as you instructed... now the initial value is 8006 then its increasing by 1.. but the initial value is 8006..!! what is wrong?

void playgame()
  
      {
  
      int i=0;
      int score=0;

Delete int in red above. Putting int there makes a NEW local variable. We want to use the game class's score. Make it this and that will happen:

void playgame()
  
      {
  
      int i=0;
      score=0;

hey it worked..! Thank you so much.. but now i have a doubt... with int deleted how is "score" declared?

and how what about exit(0); ? why isnt it working?... and how can i add a time limit for entering the code.... ?the allowed time should decrease as the game progresses.. and the it should show that "the game is over and your final score is ______ ! or should i add a break; instead of exit(0); ?

and how can i add an infinte while loop [a loop which end only when the user makes a wrong entry or the user quits by pressing "esc" ?

please do help me

You can add a time limit by adding something like:

clock_t StartTyping;

and every time the game runs do:

StartTyping=clock()

Then, before checking if the answer is correct, check if it was on time.
Example:

if((clock()-StartTyping)/CLOCK_PER_SEC>/*number of seconds here*/)
{
cout<<"It took you too long!"<<endl;
cin.get();
exit(0);
}
else
//the rest of your code

EDIT: Just don't forget to add a "#include <ctime>" line in the head :)
On a side note, I'd suggest you use the c++ headers (those without the .h),
like <iostream> instead of <iostream.h> and <cstdlib> instead of <stdlib.h>

If he's running turbo C++ v3, I don't think he has headers without .h

If he's running turbo C++ v3, I don't think he has headers without .h

Didn't know about that...
Thanks for the info.! :)

Yeah..I am using turbo c++ v3.0.. so am forced to use the old methods... and I wasnt able to add that the time.. I dont know about it..where should i add it?... is it a function? and the biggest problem is that , exit(0); isnt working..! it is intended to to stop the gmae whenever the user makes a wrong entry..! and how can i clear the screen when the score is showed ?... i want to clear the screen befor the display of next random number... but as clrscr(); would work only in "main" is there any way to do that?.... and it would be great if the score is displayed on any of the corners ..! how can i align it?
one more thing..I tried adding an intial screen with some fonts and graphics in it... that too failed..! no errors..but with that init_screen function the whole programme isnt working!! anyone please help me... and could anyone tell me more abt the Time? I pretty confused about it...

here is the code with a new initial screen function

#include <stdlib.h>
   
      #include <iostream.h>
   
      #include<conio.h>

      #include<process.h>

      #include<graphics.h>

      class game

      {

      public:

      int num,random_integer,score;

      void init_screen();

      void dispnum();

      void getnum();

      void result();

      void playgame()

      {

      int i=0;
      score=0;

      while(i<=3)

      {

      init_screen();

      dispnum();

      getnum();

      result();

      i++;

      }

      }

      };
      void game::init_screen()
    {
      setbkcolor(0);

      setcolor(4);

      settextstyle(GOTHIC_FONT,0,6);

      outtextxy(35,22,"MEMORY GAME");

      settextstyle(0,0,1);

      outtextxy(140,240,"This is a small project");

      outtextxy(140,260,"For Number Memory Game");

      outtextxy(160,460,"Press any key to continue.......");

    }
      void game::dispnum()

    {

      srand((unsigned) time(0));

      random_integer = rand();

      cout << random_integer<< endl;

      }

      void game::getnum()

      {

      cin>>num;

      }

      void game::result()

      {


      if (num==random_integer)

      {

      cout<<"correct answer"<< endl;
       score=score+1;
      cout<<"score = "<<score<<endl;
      }
      else

      {

      cout<<"wrong answer! Gameover!"<< endl;
      cout<<"Your final score is "<<score;
      exit(0);
      }

      }

  
       
 
       

      int main()

      {

      clrscr();

      game gam1;

      gam1.playgame();

      cin.get();

      return(0);
      }

ooops.. one more thing.. while compiling the program i got a warning :- Functions containing while are not expanded inline ..What do is that?:s

hey it worked..! Thank you so much.. but now i have a doubt... with int deleted how is "score" declared?

It's declared here:

class game
   
      {
   
      public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()

and how what about exit(0); ? why isnt it working?... and how can i add a time limit for entering the code.... ?the allowed time should decrease as the game progresses.. and the it should show that "the game is over and your final score is ______ ! or should i add a break; instead of exit(0); ?

Just put a "cin.get();" before the "exit(0);"
You can't put a "break;" simply because the compiler won't recognize it as a loop.
If it doesn't work, you can add the header windows.h and put a "Sleep(2000);"
or something, so that the program will wait some time before shutting down.

and how can i add an infinte while loop [a loop which end only when the user makes a wrong entry or the user quits by pressing "esc" ?

I believe that you should change this line:

while(i<=3)

to

while(1)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.