I have a problem that i have tried my best to solve..
but its not working well

in my program i take both integer and character inputs.

when typing fast i noticed that, when a user accidentally enters a character instead of integer or integer instead of character
the program goes into a frenzy loop and then quits.

Is there any way i can solve this

Thanks in advance

you can do error trapping
assume x as your input variable

if(x > 10000 || x < 0)
{
cin >> "Wrong input";
return 0;
}

I dunno if this will gonna work... lemme know if this doenst work

you can do error trapping
assume x as your input variable

if(x > 10000 || x < 0)
{
cin >> "Wrong input";
return 0;
}

I dunno if this will gonna work... lemme know if this doenst work

Thanks for the reply, but i am already doing that,..the problem i specifies is when
user enter a character A-Z in place of an integer input 0-9

I have a suggestion for you that should cause no errors and solve your problem.

Input into a string, you can then check the input.

following is a small code i have typed (not checked) just to give you an idea how i would do this (bear in mind a better alternative may exist but im no c++ pro)

string userIn;
cin>>userIn; //get users input

//this code to check for a character
if(isalpha(userIn[0]))//checks if the first character is a character 
{
  //save the character to the var you want to switch or whatever you want to do with it
}

//check for an integer
size_t n = 0;
while(isdigit(userIn[n])
   n++; //finds the number of chars that represent a decimal number

//extract the numbers
char* cNumber = new char[n+1];//create space for it

copy(userIn.begin(), userIn.begin()+n, cNumber); //copy the number characters out
cNumber[n+1] = NULL; //ensure string is null terminated

int iNumber = atoi(cNumber); //convert to integer
//use the integer

The above does not allow for leading whitespace and may not be perfecrt but it should prevent program crashes and/or give you an idea of how to prevent read fails from cin. Im sure there is a better way to get a number from a string but i dont know what it is my aplogies.

Hope this helps

well glad I helped :D please mark this thread solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.