Help Needed in a C++ program

Started by
8 comments, last by Cornstalks 12 years, 2 months ago
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);
printf("%c",words);
pos=pos+1;
if(y>0)
{
gotoxy(3*i,pos-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);
}

index.php?app=core&module=task
Advertisement
You really shouldn't cross post.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Hi,

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

That why posted there also.

Hi,

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

That why posted there also.

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 this feature, 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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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:


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

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


Cornstalk is being a little obtuse there.

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:

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.


[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=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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

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.smile.png.But will take care next time.smile.png

Great to hear. Yes, making one mistake is just fine :)


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

Asking it here is just fine.


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.

There's a function called clock 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:

clock_t lastTime = clock();
while (true)
{
clock_t newTime = clock();
if (newTime >= lastTime + CLOCKS_PER_SEC)
{
lastTime = newTime;
cout << "Another second has passed!\n";
}
}
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

[quote name='BeerNutts' timestamp='1329491295' post='4913930']
Cornstalk is being a little obtuse there.

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:

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.


[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.
[/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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


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.

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.


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

No offense taken smile.png I even ++'d the post.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement