This particular code fragment runs till you press escape (Cred for the arrow-key detection system to Prelude )
It is supposed to show the objects of a class from a file. (Note, the program works like this. There's only one object - obj - and it is changed, then read from/written to file)
Code:
while(!esc)
{
clrscr();
esc=GetAsyncKeyState(VK_ESCAPE);
cout<<"\nRecord "<<i+1<<"\n";
fin.read((char *)&obj, sizeof(obj));
gotoxy(20,4);
cout<<"Name : "<<obj.name;
gotoxy(20,5);
cout<<"Email : "<<obj.email;
gotoxy(20,6);
cout<<"Phone number : "<<obj.phone_num;
gotoxy(20,7);
cout<<"Physical Address : "<<obj.address;
gotoxy(20,8);
cout<<"Webpage : "<<obj.web;
gotoxy(20,9);
cout<<"Yahoo ID : "<<obj.yahoo;
gotoxy(20,10);
cout<<"MSN ID : "<<obj.msn; if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
{
fin.seekg(-7,ios::cur);
i--;
}
if(GetAsyncKeyState(VK_RIGHT)&SHRT_MAX)
{
fin.seekg(7,ios::cur);
i++;
}
}
Problem is, it does not wait for the keypress...I cannot work it out this way, either -
Code:
//http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392
//Code by Lucky
#include <iostream>
#include <windows.h>
using namespace std;
bool keyHit(void)
{
HANDLE stdIn=GetStdHandle(STD_INPUT_HANDLE);
DWORD saveMode;
GetConsoleMode(stdIn, &saveMode);
SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
bool ret=false;
if(WaitForSingleObject(stdIn, 1)==WAIT_OBJECT_0)
ret=true;
SetConsoleMode(stdIn, saveMode);
return(ret);
}
bool custom_getch(TCHAR &ch)
{
bool ret=false;
HANDLE stdIn=GetStdHandle(STD_INPUT_HANDLE);
DWORD saveMode;
GetConsoleMode(stdIn, &saveMode);
SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
if(WaitForSingleObject(stdIn, INFINITE)==WAIT_OBJECT_0)
{
DWORD num;
ReadConsole(stdIn, &ch, 1, &num, NULL);
if(num==1)
ret=true;
}
SetConsoleMode(stdIn, saveMode);
return(ret);
}
TCHAR custom_getch(void)
{
TCHAR ch=0;
custom_getch(ch);
return(ch);
}
int main(void)
{
char n;
for(;;)
{
cout<<"\nPress a key";
n=custom_getch();
cout<<"\n"<<n<<"\nDone";
getchar();
}
}
...because that only takes characters.
Using getchar() after the last member of the object is shown doesnt work, either.
Please help asap...
Edit - Just to specify, I need it to wait for a keypress...else I could've easily used Sleep().