rrTV-PHOTO   New HD TV
HOME   rrTV-PHOTO   GALLERIES   MY GALLERY   HELP-FAQ
myHOME PM pmRR MEMBERS 825 ONLINE 21 EVENTS SEARCH REGISTER  START HERE
 
1 page357 viewsPOST REPLY
E-flite . Next D . Fast Lad Performance

.
.
Off Topics > need help with C++ programming
 
 
yapjy
Key Veteran
Location: Singapore

Here it goes:

while (flag=true)
{
printf("xxx\n");
/*wanted to add something here so that if I press enter, it will exit the loop, but I want the loop to go on continously without pausing until I press enter*/
}

thanks in advance.
10-15-2003 Over year old.
 
 
SolarXtreme
Veteran
Location: Arroyo Grande, CA

Here is how you would do it in Windows.

while(true)
{
printf("xxx\n");
if (GetAsyncKeyState(VK_RETURN) & 0x8000)
break;
}

Here is a link to the function: http://msdn.microsoft.com/library/d...ynckeystate.asp
.

I picked a hell of a day to quit drinking

Avant EFX
Freya EVO
10-15-2003 Over year old.
HOMEPAGE  
 
 
Jim C
Veteran
Location: Indiana, PA

damn dude.. i take it that your a programmer?? i am looking to get into that when i get out of the military.. how hard is the school?? any info would help bunches

jim

http://jimsrc.com
10-15-2003 Over year old.
HOMEPAGE  
 
 
SolarXtreme
Veteran
Location: Arroyo Grande, CA

Yea i've been doing Windows application development professionally for 10 years. I was lucky enough to find an intern programming position straight out of high school so I didn't need to go to college. Actually the programming courses they are teaching in school are about 5yrs behind. You are better off buying a couple of "teach yourself" programming books. http://www.amazon.com/exec/obidos/A...0261979-8396147 or http://www.amazon.com/exec/obidos/t...glance&s=books.
.

I picked a hell of a day to quit drinking

Avant EFX
Freya EVO
10-15-2003 Over year old.
HOMEPAGE  
 
 
x86guru
Heliman
Location: Boston Area

Here is 3 more out of 1000 ways of doing it.

.
.
or
.
.


while(!(GetAsyncKeyState(VK_RETURN) & 0x8000)) printf("xxx\n");

.
.
or
.
.

do
{
printf("xxx\n");
} while(!(GetAsyncKeyState(VK_RETURN) & 0x8000));

.
.
or
.
.

Or if you're a performance freak or a compiler writer like me, you might do this instead:

char *msg = { "xxx\n"};

_asm
{
loop:
push dword ptr [msg]
call printf
push VK_RETURN
call GetAsyncKeyState
add esp, 8
and eax, 0x8000
jne loop
}


Adrian
10-18-2003 Over year old.
 
 
vitek
Key Veteran
Location: Corvallis, OR

To bad that only works in Windows. This should work in most other platforms, just not windows. Changed to using regular I/O because stdio and non-blocking stuff don't mix well.


//
// error checking omitted...
//

char buf[1];
int result;
int fd = STDIN_FILENO;

int fd_flags = fcntl(fd, F_GETFL, 0);
fd_flags |= O_NONBLOCK;

fcntl(fd, F_SETFL, fd_flags);

do {
  write(fd, "xxx\n", 4);

  result = read(fd, buf, sizeof(buf));

  if (result == -1 && errno != EAGAIN)
    break;

} while(*buf != '\n');

fd_flags &= ~O_NONBLOCK;
fcntl(fd, F_SETFL, fd_flags);


Travis
10-18-2003 Over year old.
 
 
x86guru
Heliman
Location: Boston Area

Travis, Unforntuatly that doesn't meet the basic requirements

#1: yapjy wants to see "xxx" and a newline. Your displaying back to back "xxx".

#2: yapjy wants to press Enter, not Ctrl-J to get out

#3: Unforntunatly, non-blocking stdio I/O only works with certain terminal configurations.

.
.
.
Here is another method... for DOS, or console mode Win32 applications.
.
.
.

while(1)
{
printf("xxx\n");
if (kbhit() && getch()==13) break;
}


Adrian
10-18-2003 Over year old.
 
 
vitek
Key Veteran
Location: Corvallis, OR

I don't have a compiler at home so I can test, but I'm pretty sure it will work...

The if handles the case that EOF [CTRL-D on *NIX, CTRL-J on WIN] is read, or an error occured, and the while condition checks for the ENTER key.

Travis
10-19-2003 Over year old.
 
 
SolarXtreme
Veteran
Location: Arroyo Grande, CA

Heh I don't think performance will be an issue .
.

I picked a hell of a day to quit drinking

Avant EFX
Freya EVO
10-19-2003 Over year old.
HOMEPAGE  
 
 
vitek
Key Veteran
Location: Corvallis, OR

Windows doesn't support fcntl, so it won't work on Windows at all. Unfortunately, non-blocking I/O is the only way I know to [relatively] portably check the keyboard state.
10-19-2003 Over year old.
 
 
x86guru
Heliman
Location: Boston Area

Yo Travis,

>
>The if handles the case that EOF [CTRL-D on *NIX, CTRL-J on WIN] is read, or an error occured, and the while condition checks for the
>ENTER key.
>

'\n' is 10 (LineFeed).
'\r' is 13 (Enter/Return)

Adrian
10-19-2003 Over year old.
 
 
vitek
Key Veteran
Location: Corvallis, OR

I think CTRL-Z is EOF on windows... And I forgot about the stupid windows CRLF convention being "\r\n" where everything else is "\n".

Of course, that isn't super important, because, as I said above, that code won't work on windows; fcntl isn't supported. It does work on unix though.
10-20-2003 Over year old.
 
 
1 page357 viewsPOST REPLY
Ace Hobby . Esprit Model . Thunder Power RC

.
.
Off Topics > need help with C++ programming
 PRINT TOPIC Advertisers 

Subscribe to This Topic

Thursday, December 4 - 10:32 pm - Copyright © 2000 - 2008 runryder.com | email | link to rr | runryder needs cookie