checking a variable

This is a discussion on checking a variable within the C++ Programming forums, part of the General Programming Boards category; can u please tell me how to check whether an integer variable has a number stored in it or not?...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    16

    checking a variable

    can u please tell me how to check whether an integer variable has a number stored in it or not?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by rambright View Post
    can u please tell me how to check whether an integer variable has a number stored in it or not?
    On most architectures (if not all), an integer variable always has a number stored in it.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    43
    Usually, when not assigned a number, it will contain whatever junk the memory has from the running of other programs. For example, if you declare somewhere in main:
    Code:
     int x;
    just like so, and then you write:
    Code:
     cout << x;
    you might expect a very strange and arbitrary number, such as -83239 or something like it. Therefore, an integer number is always likely to have something in it. Well, at least in C++. I do not know places where this does not happen, as EVOex says. But anyway, whatever you are placing in the memory has to contain something. Having nothing in it is not likely possible at all.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    He probably means input validation:

    Code:
    int x;
    if (std::cin >> x) {
        //x has a number
    }
    else {
        //input failed
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    16
    ya i meant input validation...... i thank u all for your help......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 04:35 AM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 07:57 PM
  3. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  4. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 09:46 AM
  5. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21