You are here:

C++/sir, i want to restrict the...

Advertisement


Question
sir,
i want to restrict the user to enter the particular data to the particular data type variable.suppose i am getting a integer input from the user,if he enters a character the program crashes in actual .so how to avoid it.
i.e int a; i should restrict the user to enter only integer .if he enter any character,how to avoid it in c++ ?before checking the ascii code the program crashes?
looking for your reply.
jayakumar

Answer
Dear Jaya,
Thankyou for your question. I apologize for the
delay, because i was out for some days and it
didn't show my status on vacation.

Well Jaya, first of all, please clarify your problem
statement clearly, or if you can send me the query
again, it would be good.

What i understood from your question was that, you
want the user to enter only the digits, or integer
value, if the user would enter the character, the
program should show an invalid input.

Well, in C++ there is a built in fuction which you
can use i.e.

if( isdigit(integer value) )
{
//Process
}

else
{
cout<<"User entered an invalid input";
exit(0);
}

The following function is defined in
<ctype.h>

I hope this will help, clarifying the confusion. You
can further, restate your problem, so that i can clear
it to you.

Regards
Professional

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Professional

Expertise

I can answer any question about functions,pointers,structures,object oriented programming basics of classes and data structures.My strong field is structured programming.

Experience

I have got 2 years experiece under C . I am able to answer about the structured concepts pointers to a little extent,OOP concepts. I have also experience in data Strucutres like Linked List, Stacks , Queues, Heaps, B Trees, Red Black Trees. I will try to satisfy with my knowledge. I am the Student of an expert here Martin, what i have learnt today, i just owe my every knowledge to him. He is the greatest.

©2016 About.com. All rights reserved.