I am trying to split a number like 33245 into its digit 3,3,2,4,5 and adding them so the output is 17 but If I enter a char between them like 12m43 the output i get is 3. How may i check for the character here.I want "invalid input" as output. The problem is that it is not giving out any exception for char and that's why i am here.I have tried exception handling but it didn't worked since it is not generating any exception, i have omitted try and catch here, If it works at you please write that in comment box. Please help.Here is my code.
#include<iostream>
using namespace std;
int count(int n)
{
int count=0;
while(n>0)
{
n=n/10;
count++;
}
return count;
}
int main()
{
int sum=0;
int digit, t;
int number;
cin>>number;
if(number<0)
{
number=-number;
}
digit=count(number);
while(digit>0)
{
t=number%10;
number=number/10;
sum=sum+t;
digit--;
}
cout<<sum<<endl;
return 0;
}