473,398 Members | 2,380 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,398 software developers and data experts.

Try catch

how do i place a try catch Statement into this ?


cout <<"Enter Student Number: ";
cin >> record[n].student_number;

Making sure Student Number is only A Number ?

i cant get this to work
try
{
cout <<"Enter Student Number: ";
cin >> record[n].student_number;
if(!(cin >=a))
}
catch
{
"Please Only Enter Numbers" << endl;
}
Apr 2 '11 #1

✓ answered by horace1

you don't need to use try - catch
check if the read of an integer works, if not clear the error, remove the faulty characters from the input stream and try again, e.g.
Expand|Select|Wrap|Line Numbers
  1.     int i;
  2.     cout << " enter a number ? ";
  3.     while (!(cin >> i))
  4.        {
  5.        cout << " Error _ enter a number ? ";
  6.        cin.clear();             // clear error condition
  7.        cin.ignore(1000,'\n');   // remove faulty characters
  8.        }
  9.  

2 1956
horace1
1,510 Expert 1GB
you don't need to use try - catch
check if the read of an integer works, if not clear the error, remove the faulty characters from the input stream and try again, e.g.
Expand|Select|Wrap|Line Numbers
  1.     int i;
  2.     cout << " enter a number ? ";
  3.     while (!(cin >> i))
  4.        {
  5.        cout << " Error _ enter a number ? ";
  6.        cin.clear();             // clear error condition
  7.        cin.ignore(1000,'\n');   // remove faulty characters
  8.        }
  9.  
Apr 3 '11 #2
Thanks, the try catch thing was giving me a headache
Apr 3 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.