Thread: How to check a variable to make sure it is the correct data type?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    18

    How to check a variable to make sure it is the correct data type?

    I have checked out lots of sources from the internet but i cant find the exact way how to do it.

    But i do found something like this:

    Code:
    #include <iostream> int main() { double d; cin>>d; if ( !cin.fail() ) cout<<"Valid data type"<<endl; else cout<<"Invalid data type"<<endl; return 0; }
    I'm a beginner, not sure about it, but i think that is C++.

    Is anyone know how to do it in C? Please help. Thanks in advanced!

  2. #2

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It depends what functions you are using to read input. Generally you need to check the return values to figure out if the function has succeeded or not. The same thing is really happening in C++ albeit, in a more unified and consistent fashion.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Check a variable to make sure it's the correct data type? You define the data type when you create the variable.

    If you mean check if the user enters the right data type, then it's possible. Look up fgets() and strtod() for user input and double conversion, respectively.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Thanks everyone for fast reply.
    Actually i use fscanf() to read data from a file eg. '234'

    data in a file doesn't have any type right?
    how can i validate that '234' is a unsigned type?

    if the value changed to 2.60 in the data file, system will crash.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by cprogbiginner View Post
    if the value changed to 2.60 in the data file, system will crash.
    Only if you don't account for the fact that the data you get might be garbage. There are generally plenty of checks you can do to ensure that you get passed something sane.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the data type of an unknown variable
    By adarpodracir in forum C Programming
    Replies: 6
    Last Post: 04-07-2012, 07:17 PM
  2. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  3. Replies: 5
    Last Post: 08-31-2009, 01:52 PM
  4. how to make our own data type ?
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2008, 05:04 AM
  5. Difference between 'data type' and variable?
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 02-02-2006, 08:48 AM