473,386 Members | 1,720 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,386 software developers and data experts.

Console Number Exception Handling.

I am attempting to use exception handling in the console to trap an
invalid character into a numeric field. Unfortunately, using this example:
================================================== ====================
#include <exception>

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
double number = 0;

while (number <= 0) {
std::cout << "Gimme a number: ";
try {
std::cin >number;
}
catch (exception e) {
std::cout << "Oops, you did it again.";
}
}

}
================================================== ====================
all I get is an endless loop. What exception should I be using and what
header file if not above. Any clues appreciated.
Oct 10 '08 #1
4 2088
On Oct 11, 6:46*am, Ed Dana <EDan...@Cox.netwrote:
I am attempting to use exception handling in the console to trap an
invalid character into a numeric field. Unfortunately, using this example:
================================================== ====================
#include <exception>

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
* *double number = 0;

* *while (number <= 0) {
* * *std::cout << "Gimme a number: ";
* * *try {
* * * *std::cin >number;
* * *}
* * *catch (exception e) {
* * * *std::cout << "Oops, you did it again.";
* * *}
* *}

}

================================================== ====================
all I get is an endless loop. What exception should I be using and what
header file if not above. Any clues appreciated.
something like this:
http://www.parashift.com/c++-faq-lit....html#faq-15.5

--
Best Regards
Barry
Oct 11 '08 #2
Thanks. That was definitely the clue I needed. (Certainly not the one I
expected.)

Barry wrote:
>
something like this:
http://www.parashift.com/c++-faq-lit....html#faq-15.5

--
Best Regards
Barry
Oct 11 '08 #3
Ed Dana wrote:
Thanks. That was definitely the clue I needed. (Certainly not the one I
expected.)

Barry wrote:
>>
something like this:
http://www.parashift.com/c++-faq-lit....html#faq-15.5
Btw, you can use exceptions for this if you like, but you must enabled them
explicitly first.
Oct 11 '08 #4
Rolf Magnus wrote:
>
Btw, you can use exceptions for this if you like, but you must enabled them
explicitly first.
Any examples of this would be appreciated. :)

Ed.
Oct 12 '08 #5

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

Similar topics

7
by: RSB | last post by:
Hi Every one, i am using the try Catch block.. and the Exception object has a Message Property but i want to Catch the Error Number so that based on the Error number i can display Different error...
7
by: Job Lot | last post by:
How can I terminate console application in Try…Catch…Finally…End Try block so that code in Finally gets executed. If I use End statement Finally does not get executed. Following is my code...
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...
5
by: Simon Johnson | last post by:
Recently, a thread appeared which asked how to create a "catch all" method for when an exception occurs within a program. The solution given only works for Windows Forms applications, as far as i...
3
by: Dean Slindee | last post by:
I have a exception handling class that could be called from either a windows project app or a console project app. Is there any way for this class to determine which type of app called it without...
5
by: Nak | last post by:
Hi there, I have a very simple console application that I'm trying to handle the thread exception on, as I understand it there is no Application object available to a console application, so I...
0
by: TrtnJohn | last post by:
I've recently converted some VB 6 code over that used the old non-structured exception handling. I also have some .NET classes that raise custom exception classes. When the code was VB6 I used to...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
6
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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,...

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.