try catch for string and int . help

Pages: 12
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
That code doesn't even compile because you are missing a } before while.
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??
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
so what should i change for my program> any idea?
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?
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.
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.
but the algorithm to compare string and int? what's is the way ??
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
Compare the ID against what?

I don't think you want to have line 6. Just remove it.
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?
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
Yes it will call the user to re-enter if you input a105.
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');
	}
but this code cannot call user to re-enter . it's stop at invalid input ! only integer allow
You just need to change cin to stream on line 5 and 6.
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
What do you mean?
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