Archived

This topic is now archived and is closed to further replies.

Crash

Newbie Need Help With Simple C++ Stuff

Recommended Posts

hmm my first post how nostalgic no no wrong thing nvm. OK, to get to the point I''m currently making a simple text base RPG. I''m using it as a learning experience sorta thing, I think/hope that it will teach me a few things theory wise without me having to bother with the graphics side of things yet. Anyway Im having a few problems probably very simple to you guys but I would appreciate the help. 1,What files must be included to use getch()? 2,How do you use getch()? On some screens there is alot of text so i want the screen to stop at places I''ve selected, so the player can read the text then press enter to continue. I think getch is right for this but I''m not 100% certain. 3,Is there a better or faster alternatice to getch() for what I require? 4,What is the QUICKEST way to randomly generate a number between 0 and 100? The quick bit isn''t essential for this project I know but as I''m going to use it for reference later on, I would like to have the quickest method used in my code. 5,Once I decide to move on to a new project with graphics, where (Apart From GameDev.net) is there good tutorials, e-books or refrences about graphics programming in C++ for games? Thanking You In Advance, Crash, We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites
1. You must include conio.h
2. that is up to you....wherever you put getch() it will wait until a character is pressed
3. getch is as good as any...
4.rand()%100
5. there are good tutorials all over this site and on its affiliates too

Share this post


Link to post
Share on other sites
2. Use:

if(!getch())
getch();




4. To generate better random #''s, you should use the timer as a seed, then pull the #''s. Do this each time you need a batch of #''s to make it even better.




Jim Adams
home.att.net/~rpgbook
Programming Role-Playing Games with DirectX 8

Share this post


Link to post
Share on other sites
Crash-To answer your last question, GameDev is the best resource I''ve found on the net. The articles archive has alot of really cool stuff.

"A man can''t just sit around." ''Lawn Chair'' Larry Walters (1982)

Share this post


Link to post
Share on other sites
Ok, thanks for the help everyone.

Just one more question for now, How do you use the timer as a seed? Like Jim Adams suggested.

Is it like this:

int Number;
srand((unsigned)time( NULL ));
Number = (rand() % 99) + 1;

Im pretty certain this works, but I''m not a hundred percent certain if it is what Jim was suggesting.


Anyway thanks again for the help,
Crash



We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites
Ah! Slight problem, I put all the getch()''s into my code that I needed but it''s caused a bit of a problem.

The source code looks a bit like this:

cout << "Alot of Text" << "Alot More Text" << "Even More Text";
getch();
cout << "Alot of Text" << "Alot More Text" << "Even More Text";
getch();
cout << "Alot of Text" << "Alot More Text" << "Even More Text";
getch();

I thought this code would do something along the line of:

Alot of Text Alot More Text Even More Text
(WAIT FOR USER INPUT)
Alot of Text Alot More Text Even More Text
(WAIT FOR USER INPUT)
Alot of Text Alot More Text Even More Text
(WAIT FOR USER INPUT)

But instead it does this:

Alot of Text Alot More Text Even More...
(WAIT FOR USER INPUT)
(WAIT FOR USER INPUT)
(WAIT FOR USER INPUT)
Text Alot of Text Alot More Text Even More Text
Alot of Text Alot More Text Even More Text

I''m not quite sure what Im doing wrong but I would really appreciate some help on this too,
Crash,

We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites
Oh by the way, I noticed that I hadn''t put in the code as Jim Adams suggested to do it:

if(!getch())
getch();

But I''ve tried that now and it still does the same thing.


Hope Someone Can Help,
Crash,

We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites
Hey, Crash.

Mixing cout with getch() isnt nice, causes some unpredictable results (or maybe predictable, but whatever). You should mix printf() with getch(), or cin with cout.
Myself I dont use cin/cout, so Im not completely sure how they work, but I know cin requires an ENTER, so in case you use it, be sure to warn the player to press ENTER. One thing I remember (or think I remember) is that if you are gonna mix cout with getch(), you have to do a flush(), or something similar, after the cin.
With getch() it would take any key, so its much better (with cin, not only it needs an ENTER but it will display anything you type before the ENTER).
The if (!getch()) getch(); thing is useful cause when you press extended keys (not really sure about the right name), like CTRL+F12, or some other weird key combination, it comes as 2 keys (first being a 0), so basically what you do is :

if (getch() == 0) // extended key pressed
getch(); // do another getch() to get the other "half" of the key

If you dont do that, when the player pressed a weird key combination (say, the CTRL+F12), it will count as 2 keys pressed, and 2 "screens" of text will pass instead of 1, so the player wont be able to read the 1st screen.

About the rand() thing, what rand() does is return a number between 0x0 and 0xFFFF (I think), so if you do a :

rand()%100, you will get a number between 0x0 and 0xFFFF divided by 100, and then you get the remainder of that division. Note the remainder will always be between 0 and abs(divisor-1) (a weird fact is that in C that doesnt happen, and the remainder can be negative if you do something like (-5)%3, C returns -2, while it should return 1 if it was mathematically correct, but anyway), so if you want a number between 0 and 100, as you said, you should do rand()%101, and not %100.

The srand() part is correct as you said (it works, but Im not sure also if its what Jim suggested).

So my point/answer is : Use only printf() (or puts()) and getch(), I believe that it is better for your purpose (text based RPG), but I could be wrong.

Anyway, cya, and have fun on your game,
-RoTTer

Share this post


Link to post
Share on other sites
About use with getch() (or _getch() if you program for windows) and cout: try to put a call to cout.flush() before your getch() call, since that will flush all cout streams currently in the buffer. I don''t know why you have to do that before getch() specifically, but it works for me.

A good way to seed rand() (again, in WIN32) is to make this call: srand(GetTickCount()). This function is in the windows headers, and it measures the system time in milliseconds if I am not mistaken. This pretty much makes sure that rand()''s output will be different each time.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
try using cin.get(), and i''m not sure, but i think you need to use cin.ignore() after that as well, but i can''t remember why.

Share this post


Link to post
Share on other sites
About use with getch() (or _getch() if you program for windows) and cout: try to put a call to cout.flush() before your getch() call, since that will flush all cout streams currently in the buffer. I don''t know why you have to do that before getch() specifically, but it works for me.

A good way to seed rand() (again, in WIN32) is to make this call: srand(GetTickCount()). This function is in the windows headers, and it measures the system time in milliseconds if I am not mistaken. This pretty much makes sure that rand()''s output will be different each time.

Share this post


Link to post
Share on other sites
Thanks for the help guys. I''ve got it working right now.

I tried waht grul suggested with the cout.flush() but that didn''t work so bearing in mind what Rotter and anonymous poster said I used cin.get() and cin.ignore() but this meant the user had to press enter twice, so I just put cin.get on and that works.

Thanks for the help again,
Crash

We Must Move Forwards NOT Backwards, Sideways NOT Forwards And Always Twirling Twirling Towards Success

Share this post


Link to post
Share on other sites