try catch for string and int . help

Pages: 12
Nov 8, 2012 at 1:28am
for exampl.e
concept
1
2
3
4
5
6
7
8
9
10
11
12
13

int ID = 0;
do{
    cin >> ID;
    try{
    throw ID;
    }
    catch( int e ){
        if( typeid(ID).name() != typeid(int).name()){
           cout << "Invalid input ! Only integer allow " << endl;
        }
     }
}while( typeid(ID).name() != typeid(int).name() );


it's wrong. it keep looping. why?
Last edited on Nov 8, 2012 at 1:36am
Nov 8, 2012 at 1:35am
That code doesn't even compile because you are missing a } before while.
Nov 8, 2012 at 1:37am
i just simply type. i type correctly in my compiler. but it still keep looping .
and 1 more error

even i type 123 in my cin ID . it will say invalid input also. why??
Nov 8, 2012 at 1:49am
The code doesn't keep looping for me. And you have the wrong idea about typeid. typeid(whatever).name() will always give you the same name because the type is fixed at compile time.
Last edited on Nov 8, 2012 at 1:50am
Nov 8, 2012 at 1:51am
so what should i change for my program> any idea?
Nov 8, 2012 at 2:05am
That's a bit like saying:

1
2
3
4
5
6
7
8
    int ID = 0;

    cin >> ID ;
    goto here ;
here:
    if ( /* ... */ )
    {
    }


What's wrong with the more traditional approach to error handling here?
Nov 8, 2012 at 2:09am
that's why , i don't know how to compare the string and int here.

i think sizeof . but it's won't work either.
Nov 8, 2012 at 2:14am
When a read operation fails cin goes into an error state. To check if cin is in an error state you can treat cin much like a bool variable that is false if it's in an error state and true otherwise. operator>> returns a reference to cin which makes it possible to place the whole read operation inside an if or a loop condition to check if the read operation succeed.

In your case you want to loop as long as the read operation fails so you can use !(cin >> ID) as the loop condition.

1
2
3
4
5
6
7
int ID;
while (!(cin >> ID))
{
	cout << "Invalid input ! Only integer allow " << endl;
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
}


clear is used here to leave the error state.

ignore is used to remove the invalid input from the cin, because when a read operation fails it leaves the input in cin untouched. If we didn't remove it it would try to read the same input next time and fail again, so it would be an infinite loop.
Nov 8, 2012 at 2:16am
but the algorithm to compare string and int? what's is the way ??
Nov 8, 2012 at 2:19am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
istream &operator >>( istream &stream , Employee &theEmployee ){

	bool found = 1;
	do{
	cout << "Enter Employee ID : ";
	stream >> theEmployee.EmployeeID;
	while( !( stream >> theEmployee.EmployeeID ) ){
		cout << "Invalid input ! Only integer allow " << endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}


	cout << "Enter Employee Name : ";
	stream >> theEmployee.EmployeeName;

	cout << "Enter Employee Salary : RM";
	stream >> theEmployee.EmployeeSalary;
	return stream;
}


here's the part of my code . i just want to compare the ID
Nov 8, 2012 at 2:22am
Compare the ID against what?

I don't think you want to have line 6. Just remove it.
Nov 8, 2012 at 2:24am
You need to remove line 6 so you aren't requiring a user to enter the ID twice.

Compare the ID to what?

Is EmployeeName one word?
Nov 8, 2012 at 2:27am
i using do while

my question is.
if i input : example

ID : a102 -< this contain a alphat or letter
then it will call user to re-enter
1
2
3
4
5
ID : a105
Invalid Input ! Only integer allow ! 

ID : 123
Name:


so if user enter a integer . it only will proceed to next input .
I plan to use try and catch block
Nov 8, 2012 at 2:38am
Yes it will call the user to re-enter if you input a105.
Nov 8, 2012 at 2:43am
err.
so the code should be like this? correct?
1
2
3
4
5
6
7

	cout << "Enter Employee ID : ";
        while( !( stream >> theEmployee.EmployeeID ) ){
		cout << "Invalid input ! Only integer allow " << endl;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
Nov 8, 2012 at 2:45am
but this code cannot call user to re-enter . it's stop at invalid input ! only integer allow
Nov 8, 2012 at 2:46am
You just need to change cin to stream on line 5 and 6.
Nov 8, 2012 at 2:52am
1
2
3
4
5
6
cout << "Enter Employee ID : ";
	while( !( stream >> theEmployee.EmployeeID ) ){
		cout << "Invalid input ! Only integer allow " << endl;
		stream.clear();
		stream.ignore(numeric_limits<streamsize>::max(), '\n');
	}


it's same. it's stuck at same output
Nov 8, 2012 at 2:54am
What do you mean?
Nov 8, 2012 at 3:13am
It's cannot looping to input. The output window are invalid input . only integer allow. and cannot go back to the input again d
Pages: 12