Exception handling errors

Hello all, i'm new to this forum.
I need help with this code. Its actually a sample from a large program. Here the computer would ask for an integer input. When I give say 1 or 2 or 3 , the program continues and assigns new values to n, as long as the loop goes. but if I give '.' or '/' , the value of n remains the previous value and the compiler does not ask for the value of n anymore. What should I do to handle this exception?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

void main()
{
	int n;
	do
	{
		cout<<"Enter a value of n:";
		cin>>n;
		cout<<"\n The value of n: "<<n<<endl;
                system("pause");
	}while(n!=0);
}


Enter a value of n:6
 The value of n: 6
Press any key to continue . . .
Enter a value of n:9
 The value of n: 9
Press any key to continue . . .
Enter a value of n:4
 The value of n: 4
Press any key to continue . . .
Enter a value of n:.
 The value of n: 4
Press any key to continue . . .
Enter a value of n: The value of n: 4
Press any key to continue . . .
Enter a value of n: The value of n: 4
Press any key to continue . . .
Enter a value of n: The value of n: 4
Press any key to continue . . .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;

void main()
{
	char n(0);
	do
	{
		cout<<"Enter a value of n:";
		cin>>n;
                if(!isdiget(n))
                    continue;
		cout<<"\n The value of n: "<< n<<endl;
                system("pause");
	}while(n!='0');
}
Thanx Denis for the reply. I'll check it out immediately and let u know.
Hello Denis, I tried ur code. But it didnt work. This is the error its giving me.

Error 1 error C3861: 'isdiget': identifier not found

Do you mean isdigit ? I tried with that, still it dint work. Or do you mean we have to define isdiget() ? Thanx in advance.
He probably meant isdigit().

http://cplusplus.com/reference/std/locale/isdigit/

Don't for get to #include the appropriate library.
you are right I mean isdigit.

But you should add new include
 
#include <cctype> 
Ok Denis I think isdigit() is a function. Now its working fine. Another small thing I would like to know.

char n(0)

What is its speciality?
Last edited on
Topic archived. No new replies allowed.