473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem with _kbhit and i/o exception handling

hi,
I want to generate random numbers in c++ (using vc++ 6.0) on hit of
enter. I am using _kbhit(), but don't know how to reset it, so that I
can use it again, can anybody help me with this.
code is say like following.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace::std;
int main(int argc, char* argv[])
{
for(int i=0; i<10; i++)
{
cout << "hit enter to generate random number" << endl;
int ran1 = func1();
cout << "hit enter to generate random number" << endl;
int ran2 = func2();
}

return 0;
}
int func1()
{
int ret;

while(!_kbhit())
{
ret = rand()%10;
Sleep(10);
}
return ret;
}
int func2()
{
int ret;

while(!_kbhit())
{
ret = rand()%20;
Sleep(10);
}
return ret;
}

There is another problem with I/O exception handling:
i tried to read an int correctly using following code. but in case of
exception it is caught but cin doesn't execute inspite of clearing it:

int main(int argc, char* argv[])
{
int i;
bool check = false;

cin.exceptions(ios_base::failbit);

while(!check)
{
try{
cout << "enter a number: ";
cin >> i;
check = true;
}catch(ios_base::failure){
cin.clear();
}

}

return 0;
}

Thanks,
vikas

Oct 19 '05 #1
2 2119
vikas wrote:
hi,
I want to generate random numbers in c++ (using vc++ 6.0) on hit of
enter. I am using _kbhit(), but don't know how to reset it, so that I
can use it again, can anybody help me with this.
code is say like following.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace::std;
int main(int argc, char* argv[])
{
for(int i=0; i<10; i++)
{
cout << "hit enter to generate random number" << endl;
int ran1 = func1();
cout << "hit enter to generate random number" << endl;
int ran2 = func2();
}

return 0;
}
int func1()
{
int ret;

while(!_kbhit())
{
ret = rand()%10;
Sleep(10);
}
return ret;
}
int func2()
{
int ret;

while(!_kbhit())
{
ret = rand()%20;
Sleep(10);
}
return ret;
}
_kbhit is a non-standard Microsoft extension. You'll want to ask this
question in a Microsoft newsgroup (e.g.
comp.os.ms-windows.programmer.win32).

There is another problem with I/O exception handling:
i tried to read an int correctly using following code. but in case of
exception it is caught but cin doesn't execute inspite of clearing it:

int main(int argc, char* argv[])
{
int i;
bool check = false;

cin.exceptions(ios_base::failbit);

while(!check)
{
try{
cout << "enter a number: ";
cin >> i;
check = true;
}catch(ios_base::failure){
cin.clear();
}

}

return 0;
}


What do you mean by "doesn't execute"? Why does the input fail? I'm
guessing that you typed an invalid number (say, "xyz"), and clearing
the state doesn't clear the underlying buffer (or something to that
effect). Try adding this after the cin.clear():

cin.seekg( 0, ios::end );

Cheers! --M

Oct 19 '05 #2
you are right, problem is due to abnormal condition of underlying
buffer. I didn't knew how to do that.
I checked with
cin.seekg( 0, ios::end );

it does solve the problem

thanks M,

mlimber wrote:
vikas wrote:
hi,
I want to generate random numbers in c++ (using vc++ 6.0) on hit of
enter. I am using _kbhit(), but don't know how to reset it, so that I
can use it again, can anybody help me with this.
code is say like following.
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace::std;
int main(int argc, char* argv[])
{
for(int i=0; i<10; i++)
{
cout << "hit enter to generate random number" << endl;
int ran1 = func1();
cout << "hit enter to generate random number" << endl;
int ran2 = func2();
}

return 0;
}
int func1()
{
int ret;

while(!_kbhit())
{
ret = rand()%10;
Sleep(10);
}
return ret;
}
int func2()
{
int ret;

while(!_kbhit())
{
ret = rand()%20;
Sleep(10);
}
return ret;
}


_kbhit is a non-standard Microsoft extension. You'll want to ask this
question in a Microsoft newsgroup (e.g.
comp.os.ms-windows.programmer.win32).

There is another problem with I/O exception handling:
i tried to read an int correctly using following code. but in case of
exception it is caught but cin doesn't execute inspite of clearing it:

int main(int argc, char* argv[])
{
int i;
bool check = false;

cin.exceptions(ios_base::failbit);

while(!check)
{
try{
cout << "enter a number: ";
cin >> i;
check = true;
}catch(ios_base::failure){
cin.clear();
}

}

return 0;
}


What do you mean by "doesn't execute"? Why does the input fail? I'm
guessing that you typed an invalid number (say, "xyz"), and clearing
the state doesn't clear the underlying buffer (or something to that
effect). Try adding this after the cin.clear():

cin.seekg( 0, ios::end );

Cheers! --M


Oct 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Páll Ólafsson | last post by:
Hi I have a problem with the Microsoft.ApplicationBlocks.ExceptionManagement? I can't get it to work in RELEASE mode? If I run the project in debug mode the block works fine but when I run the...
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
6
by: JM | last post by:
Hi, I need a function for a character based program (console) that works exactly like the INKEY$() function in (old) QBasic: Read only 1 keystroke and then move on. Console.Read;...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
6
by: Robin Riley | last post by:
Hi, I have a .NET solution that contains a dll project and a tester application project which, of course, invokes the dll. The dll project has exception handling in it. What's happening is that...
25
by: JC | last post by:
Hi People, Please I need your help. This code run a thread ok but Not close later. thanks... private void RunServer(int aPortNumber) {
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.