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

For me it doesn't give a warning... I'm using Dev-C++ and it works completely fine.

hii

I tied adding the time limit.. got 6 errors\11:| is there anyway to get the "error log" in turbo c"" so that i could paste it here...

here is the code

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

      #include<process.h>

      #include<time.h>
   
      class game
   
      {
   
      public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()
  
      {
  
      int i=0;
      
      score=0;
      
      StartTyping=clock();

  
      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((clock()-StartTyping)/CLOCKS_PER_SEC>

      {

         cout<<"It took you too long!"<<endl;

      cin.get();

      exit(0);

       }

else

      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();
  
      }

anyone please tell me about exit(0);!! why isnt it working?? should i replace it with a break;? ant what abt an infinte loop which works till the user makes a wrong answer or the user presses "esc"!! for "esc" to be working i think i should define it with #define..right? how can i do it?... :idea:

Well Just Put The Function outside the Class Declaration and it will do fine.

Dont forget to add

"void Game::"

You get errors because you didn't write a number a closed brackets after "if((clock()-StartTyping)/CLOCKS_PER_SEC>"
(Example: "if((clock()-StartTyping)/CLOCKS_PER_SEC>5)"

Also, you should put the "StartTyping=clock();" inside the while loop.

clock() returns the time in milliseconds since the program started. CLOCKS_PER_SECOND is a defined constant and its value is 1000 (makes clock()/CLOCKS_PER_SECOND return the seconds since the program started).

well..i didnt get you..! you mean the init_screen() function,right?.. where should i put it?... Am confused..please please help me

well..i didnt get you..! you mean the init_screen() function,right?.. where should i put it?... Am confused..please please help me

He means that you should move the function declaration outside the class declaration.
Instead of:

public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame()
  
      {
  
      int i=0;
      
      score=0;
      
      StartTyping=clock();

  
      while(i<=3)
  
      {
  
      dispnum();
  
      getnum();
  
      result();
  
      i++;
  
      }
  
      }
  
      };

do it like that:

public:
   
      int num,random_integer,score;
   
      void dispnum();
   
      void getnum();
  
      void result();
  
      void playgame();
  };

void game::playgame()
      {
  
      int i=0;
      
      score=0;
      
      StartTyping=clock();

  
      while(i<=3)
  
      {
  
      dispnum();
  
      getnum();
  
      result();
  
      i++;
  
      }
  
      }

hey..did the modifications as you told..now got 5 errors..!


the code is

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

      #include<process.h>

      #include<time.h>
   
      class game
   
      {
   
      public:
   
      int num,random_integer,score,StartTyping;

      void dispnum();
   
      void getnum();
  
      void result();

      void playgame();

      };

      void playgame()

      int i=0;

      score=0;


      while(i<=3)

      {

       StartTyping=clock();//told me to put it inside the while loop


      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((clock()-StartTyping)/CLOCKS_PER_SEC>10)

      {

         cout<<"It took you too long!"<<endl;

      cin.get();

      exit(0);

       }

else

      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;
  
      }

what is the diffence between return 0 and return(0) ?

hey..did the modifications as you told..now got 5 errors..!


the code is

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

      #include<process.h>

      #include<time.h>
   
      class game
   
      {
   
      public:
   
      int num,random_integer,score,StartTyping;

      void dispnum();
   
      void getnum();
  
      void result();

      void playgame();

      };

      void playgame()

      int i=0;

      score=0;


      while(i<=3)

      {

       StartTyping=clock();//told me to put it inside the while loop


      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((clock()-StartTyping)/CLOCKS_PER_SEC>10)

      {

         cout<<"It took you too long!"<<endl;

      cin.get();

      exit(0);

       }

else

      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;
  
      }

what is the difference between return 0 and return(0) ?

sorry for posting the same thing a 2nd time..it was a mistake

what is the difference between return 0 and return(0) ?

I don't think there is a difference, except for the way it's written.
About your code, you forgot to put "game::" before the function declaration, and a "{" when you start declaring it.

>what is the difference between return 0 and return(0) ?
Personal preference. Redundant parentheses are allowed, and the effect is the same.

hiii.. now the program is running... but the clock seems to be out of order..! no message is coming up even after 20 secs...


here is the code

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

      #include<process.h>

      #include<time.h>
   
      class game
   
      {
   
      public:
   
      int num,random_integer,score,StartTyping;

      void dispnum();
   
      void getnum();
  
      void result();

      void playgame();

      };

       void game::playgame()

      {

      int i=0;

      score=0;


      while(i<=3)

      {

       StartTyping=clock();//told me to put it inside the while loop


      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((clock()-StartTyping)/CLOCKS_PER_SEC>10)

      {

         cout<<"It took you too long!"<<endl;

      cin.get();

      exit(0);

       }

else

      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;
  
      }

For me it works fine... except it doesn't show the message - it closes it program immediately. Maybe you better add a Sleep(TIME) in your code instead of cin.get().
Also, you may want to put the following code to show the player how many seconds it took him:
cout<<"It took you "<<(clock()-StartTyping)/CLOCKS_PER_SEC<<" seconds!";

hii..i added cin.get(); before exit(0);.. now whenever a wrong answer is entered it comes straight to the source code.. "Gameover " is not being showed.. i guess exit(0); wont allow it.. so should i add flag instead of exit(0); but i dont know how to add flag.. pls help me
cout<<"wrong answer! Gameover!"<< endl;

cout<<"Your final score is "<<score;

cin.get();

exit(0);

Simply declare a variable (name doesn't matter. In this case I'll call it ExitFlag).
At the beginning initialize it to 0, and when the game is over, make its value 1. Furthermore, change the while loop to "while(ExitFlag==0)" or "while(!ExitFlag)"

You can make the flag value anything you like, just remember the values of each situation.
For your convenience, you can define constant variables to help you remember.
I, for example, use the following definitions many times:
#define YES 1
#define NO 0

('true' and 'false' have the same values of my 'YES' and 'NO')

The flag should be of bool type, and you don't have to define anything (if it is really C++)

HEY TRIED adding sleep(TIME) added dos.h.. yet error shows that spee should have prototype..i thought #include<dos.h> is it prototype.. but what is the value of TIME here?.. should i declare it? I got to go now.. will add the modified code tomorrow.. Many thanks to everyone who help me... and anyone please help me abt that graphics function.. init_screen()

i would love to add so font styles to it


bye

Jeevan

I dont know how to add a flag..where should i add the flag..? I dont want the program to get complicated.. would like to make simple.. pls help me

I dont know how to add a flag..where should i add the flag..? I dont want the program to get complicated.. would like to make simple.. pls help me

Flag is just like a normal variable, but changes values according to the situation and the program is tracking its value to know what to do.

About the Sleep() function, instead of the "TIME" put a number. The program will sleep for that number*milliseconds and then will continue. The header should be <windows.h>

hey.... it says " unable to open windows.h.. what should i do?.... is there any other method to stop the program when the user enter the wrong answer.?..anyone pls help me.... how to add bool??..what exactly is that?? .....

am using turbo c++ v3.0.. why is windows.h missing?..how can i add it..? what might be the problem?


here is the code...

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

      #include<process.h>

      #include<time.h>

      #include<DOS.h>

      #include<windows.h>

      class game

      {

      public:

      int num,random_integer,score,StartTyping;

      void dispnum();

      void getnum();

      void result();

      void playgame();

      };

       void game::playgame()

      {

      int i=0;

      score=0;


      while(i<=3)

      {

       StartTyping=clock();//told me to put it inside the while loop


      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((clock()-StartTyping)/CLOCKS_PER_SEC>10)

      {

	 cout<<"It took you too long!"<<endl;

	 cout<<"It took you "<<(clock()-StartTyping)/CLOCKS_PER_SEC<<" seconds!"<<endl;

      Sleep(10);

      exit(0);

       }

else

      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;

      cin.get();

      exit(0);

      }

      }






      int main()

      {

      clrscr();

      game gam1;

      gam1.playgame();

      cin.get();

      return 0;

      }

It works fine for me... but you made the program sleep 10 milliseconds (1/100 the second). You need to put the seconds*1000 inside the parentheses (examples for ten seconds: Sleep(10000) or Sleep(10*CLOCKS_PER_SEC) ).

Hii..I added sleep(10000).. but the problem is its is unable to open windows.h..what might be the problem???? could we avoid sleep slep and go for any other method..? pls help me

Hii..I added sleep(10000).. but the problem is its is unable to open windows.h..what might be the problem???? could we avoid sleep slep and go for any other method..? pls help me

If it is so.. I'd suggest you use the goto statement which will lead to the cin.get() line in main().

how can i use[inline]goto[/inline] ? could goto be used to go to clrscr(); in main() to clear the screen before the display of next random number? or is there anyother way to clear the screen outside main()?.....
here is my final code...pls help me with goto and clearing the screen..

#include <stdlib.h>

      #include <iostream.h>

      #include<conio.h>

      #include<process.h>

      #include<time.h>


      class game

      {

      public:

      int num,random_integer,score,StartTyping;

      void dispnum();

      void getnum();

      void result();

      void playgame();

      };

       void game::playgame()

      {

      int i=0;

      score=0;


      while(i<=3)

      {

       StartTyping=clock();//told me to put it inside the while loop


      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((clock()-StartTyping)/CLOCKS_PER_SEC>10)

      {

	 cout<<"It took you too long!"<<endl;

	 cout<<"It took you "<<(clock()-StartTyping)/CLOCKS_PER_SEC<<" seconds!"<<endl;



     exit(0);

       }

else

      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;

      cin.get();

      exit(0);

      }

      }






      int main()

      {

      clrscr();

      game gam1;

      gam1.playgame();

      cin.get();

      return 0;

      }

goto is a statement that tells the program to jump to another place in the program, ignoring everything in between.
It's quite dangerous to use, but I don't see any other simple solution in this case. Just create a label before cin.get() and a goto statement when the game is over.
Example of use:

if(GameOver==true) goto end;
else // do something else

end:
cin.get();

what is ture here??ii dont get you..!! pls explain!

It seems now this thread looks like a horrible mash of valued receipts and consumate gibberish advices.
You can't use windows.h header with aged 16-bit Turbo C++ 3.0 compiler.
You can't use goto statement to jump into the main function from outside its body.
You can't do the simplest thing: to present your snippets in a proper way:
[code=cplusplus] your sources

[/code]
Free advice: stop this thread, start from the beginning. Declare well-defined problem in the header of a new thread, present selected suspicious code then ask help on the problem...
Of course, it's my own opinion only ;)...

commented: Well said :P +1

hii... am sorry for breaking some of the rules...:) it was just because I am an ametuuer programmer and a beginner at this website.. But everyone out here help me a lot in solving my problem..[though it isnt completely finished..!]... I am once again thanking you all for your kind help and advices..and hoping that you would help me again! ......

My school is at a small village with limited infrastructure.. so we are taught turbo c++ at school... as you mentioned its very old..yet i love it.. but as it is lacks most features am going to install Visual c++ express edition... now...let me tell this again..Am an amateur programmer..what are the main differences between turbo c++ and visual c++?

pls help me

hii... am sorry for breaking some of the rules...:) it was just because I am an ametuuer programmer and a beginner at this website.. But everyone out here help me a lot in solving my problem..[though it isnt completely finished..!]... I am once again thanking you all for your kind help and advices..and hoping that you would help me again! ......

My school is at a small village with limited infrastructure.. so we are taught turbo c++ at school... as you mentioned its very old..yet i love it.. but as it is lacks most features am going to install Visual c++ express edition... now...let me tell this again..Am an amateur programmer..what are the main differences between turbo c++ and visual c++?

pls help me

One is extremely old (your version of Turbo C++) and one is current (Visual C++). Visual C++ Express is free, as is Dev C++ and Code Blocks, which both use modern compilers. You can download all three for free via the internet and install them and run them, so there is really no reason to use the old stuff, in my view, particularly if you're starting now. Your school's lack of resources are not a problem, since like I said, it's free. Why not use the most current version, which will let you get rid of the old, antiquated headers. Very few people are going to have that compiler installed, so we can't run most of your code without changing it. I agree with ArkM on this one. The thread has gotten too long and meandering. Try to consolidate the advice into a working program, take what is useful, and throw out the rest. Get an up to date compiler, get rid of the all the old headers with .h at the end of them, and start over. If need be, start a new thread.

commented: I agree :] +4

thnk you for your advice.. i am going to install vc++ express edition... will start a new thread..

thanks a lot for everyone who has helped me


regards
Jeevan

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.