Sign in to follow this  
meetjatin88

Help Needed in a C++ program

Recommended Posts

meetjatin88    104
Hi Everyone,










I am Actually trying to make a simple C++ game in which some random alphabets will fall down and when we press one of those random alphabets keys the aphabet should disappear.

The problem that i am facing in making this code is that when i am using some function to read the key input that causes my program to wait for the user's input and hence my program comes to a stop till i press some key.

I want some logic/function which will stop the execution of my program.

Can you please suggest any such function or some logic that i can use in this program.

Please note my compiler is Microsoft Visual C++ 2010 and i am not that familiar with the functions in windows.h header file.

Thanks in advance.

Any pointers would be greatly appreciated.


My program is somewhat like this

/*Including Header Files*/
#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <conio.h>
# include <Windows.h>

int i=0,y=1;
char words[5];
int pos[5];
char c;
void gotoxy(int x,int y);
void Movement();
int readkey(void);

void random()
{
int n;
for (int t=0;t<5;++t)
{
n= rand() % 26;
pos[t] = n % 2;
words[t] = (char)(n+65);
}
}

void main()
{
random();
for(y=0;y<10;y++)
{
for(i=0;i<5;++i)
{

gotoxy(3*i,pos[i]);
printf("%c",words[i]);
pos[i]=pos[i]+1;
if(y>0)
{
gotoxy(3*i,pos[i]-2);
printf("%c",' ');
}
}
Sleep(1000);
}
getch();
}


void gotoxy(int x,int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

[img]index.php?app=core&module=task[/img]

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='meetjatin88' timestamp='1329485010' post='4913892']
Hi,

Actually i was not sure whether i should post this in beginner or game programming thread.

That why posted there also.
[/quote]
It's ok, even if you post it in the wrong one (and a lot of the programming threads could validly be posted in any of For Beginners, Game Programming, and General Programming; there isn't always One True Forum), a moderator would kindly move it for you. But if you post it in multiple places, it'll look like you're spamming the forums. Also, many of the regular users don't even visit a particular forum, as we use [url="http://www.gamedev.net/index.php?app=core&module=search&do=viewNewContent&search_app=forums&search_app_filters%5Bforums%5D%5BsearchInKey%5D=&period=today&userMode=&followedItemsOnly=0"]this feature[/url], so it doesn't even matter what forum a post is in, we'll see it (which is how I saw you created two threads in different forums). One more thing: putting your code in [code ][/code ] (without the spaces) tags will make it actually readable.

While I'm here, let me point out a few things: void main() is illegal in C++. I *has* to have a return type of int. Since you're using C++ and including <iostream>, you should actually probably use it (I can't see you using anything in <iostream>). You're using printf, but I'd highly suggest switching to using streams, like std::cout (for output) and std::cin (for input, where appropriate). Right now it looks like you're trying to program in C instead of C++.

You also said your problem is that you're using a function that stops execution, but then you say you want a function that stops execution... I'm confused.

Also, it's important to know exactly what kind of keyboard input you want to read. If you want to read characters, then there's stuff in the standard library I can suggest. If you want to read other things like arrow keys and control keys, then there's some stuff in Windows.h that can be used.

Share this post


Link to post
Share on other sites
BeerNutts    4400
Cornstalk is being a little obtuse there. it sounds to me like you want your program to continue to run, but it's also checking for keys being pressed. If a key is pressed it does something.

Try including conio.h, and using the kbhit() function., like this:

[code]
#include <conio.h>
char c;

while(1)
{
// do stuff in your main loop
if (kbhit()) {
// a keyboard key has been pressed, read it out
c = getch();

// do something with c
}
}
[/code]

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='BeerNutts' timestamp='1329491295' post='4913930']
Cornstalk is being a little obtuse there.
[/quote]
I'm not really sure what I'm being obtuse about... Could you explain further? I tried to explain how to effectively post on GameDev.net, explain clearly (though bluntly and concisely) a few common mistakes beginners make in C++, and then sought clarification on the OP's rather confusing request:
[quote name='meetjatin88' timestamp='1329484397' post='4913889']
The problem that i am facing in making this code is that when i am using some function to read the key input that causes my program to wait for the user's input and hence my program comes to a stop till i press some key.

I want some logic/function which will stop the execution of my program.
[/quote]

[size=1]P.S. and I wasn't the one who gave --rate. I'm just curious what I'm being "obtuse" about so I can know how to better explain myself in the future.[/size]

Share this post


Link to post
Share on other sites
meetjatin88    104
Hi Cornstalk,

Thanks for the information and more importantly correcting me about how to post effectively on Game dev,Well it was my first post and i think one mistake is fine.:).But will take care next time.:)

Hi BeerNutts,

Using khbit have solve my problem.Thanks a lot...Cheers :)

@ ALL Can i ask one more doubt regarding this program here only or should i start a new thread.

Actually i also want to include a timer in my program..to keep a track of how many correct random alphabets the user enters in say 60 secs..

I can you a variable and assign it value 60.and then decrease it by 1,but how to make sure that after exactly one sec it would get displayed in the screen.
i.e i need to display the counter exactly after 1 sec and this should not affect my other program functioanality .Is this possible, there must be a solution.Guys please provide some pointers/suggestions regarding the same.

Thanks
Jatin

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='meetjatin88' timestamp='1329497928' post='4913953']
Thanks for the information and more importantly correcting me about how to post effectively on Game dev,Well it was my first post and i think one mistake is fine.[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img].But will take care next time.[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]
Great to hear. Yes, making one mistake is just fine :)

[quote name='meetjatin88' timestamp='1329497928' post='4913953']
@ ALL Can i ask one more doubt regarding this program here only or should i start a new thread.
[/quote]
Asking it here is just fine.

[quote name='meetjatin88' timestamp='1329497928' post='4913953']
Actually i also want to include a timer in my program..to keep a track of how many correct random alphabets the user enters in say 60 secs..

I can you a variable and assign it value 60.and then decrease it by 1,but how to make sure that after exactly one sec it would get displayed in the screen.
i.e i need to display the counter exactly after 1 sec and this should not affect my other program functioanality .Is this possible, there must be a solution.Guys please provide some pointers/suggestions regarding the same.
[/quote]
There's a function called [url="http://www.cplusplus.com/reference/clibrary/ctime/clock/"]clock[/url] in the header <ctime> which you can use for this. clock() returns the number of ticks since the program was started. What are ticks, you might ask? Well... it's related to a second, but it's not equal to a second. For me, on my computer (and many other computers), when I use clock(), 1 tick equals 1 millisecond. The macro CLOCKS_PER_SEC will tell you how many ticks there are in one second (so on my computer CLOCKS_PER_SEC would be 1000).

You can change your program so that it keeps track of the time and checks if CLOCKS_PER_SEC number of ticks have passed. Simple example:
[code]
clock_t lastTime = clock();
while (true)
{
clock_t newTime = clock();
if (newTime >= lastTime + CLOCKS_PER_SEC)
{
lastTime = newTime;
cout << "Another second has passed!\n";
}
}
[/code]

Share this post


Link to post
Share on other sites
BeerNutts    4400
[quote name='Cornstalks' timestamp='1329495063' post='4913945']
[quote name='BeerNutts' timestamp='1329491295' post='4913930']
Cornstalk is being a little obtuse there.
[/quote]
I'm not really sure what I'm being obtuse about... Could you explain further? I tried to explain how to effectively post on GameDev.net, explain clearly (though bluntly and concisely) a few common mistakes beginners make in C++, and then sought clarification on the OP's rather confusing request:
[quote name='meetjatin88' timestamp='1329484397' post='4913889']
The problem that i am facing in making this code is that when i am using some function to read the key input that causes my program to wait for the user's input and hence my program comes to a stop till i press some key.

I want some logic/function which will stop the execution of my program.
[/quote]

[size=1]P.S. and I wasn't the one who gave --rate. I'm just curious what I'm being "obtuse" about so I can know how to better explain myself in the future.[/size]
[/quote]

Yes, you explained why his post wasn't precise. That's good. But, knowing your a smart guy, you can tell what it is he wanted, and that he just mis-typed. You should have gone ahead and given him the most obvious solution.

That's what I meant by obtuse. You probably knew what it was he wanted, but you possibly made him more confused by your answer. He's obviously a beginner.

That's just the way I see it. To each his own. My apologies if I offended.

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='BeerNutts' timestamp='1329499485' post='4913959']
Yes, you explained why his post wasn't precise. That's good. But, knowing your a smart guy, you can tell what it is he wanted, and that he just mis-typed. You should have gone ahead and given him the most obvious solution.

That's what I meant by obtuse. You probably knew what it was he wanted, but you possibly made him more confused by your answer. He's obviously a beginner.
[/quote]
Ah, I see. I really was confused though. Yes, I could've taken a guess and made a post similar to yours, but I've had a lot of experiences where I think I've correctly guessed the question and post an answer, but it turns out they were asking something else, but they think that what I've posted is the answer to their question, and when it doesn't solve it it just causes even more confusion. Not to mention that I pretty much wasted my time answering the wrong question and causing some new confusion. So I've decided to make a personal rule of thumb that a question be clearly understood before I venture on answering it.

[quote name='BeerNutts' timestamp='1329499485' post='4913959']
That's just the way I see it. To each his own. My apologies if I offended.
[/quote]
No offense taken [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] I even ++'d the post.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this