is_int question

This is a discussion on is_int question within the C++ Programming forums, part of the General Programming Boards category; hi.. i was wondering if anyone knew a c++ substitute for the php function is_int: http://www.php.net/is_int please reply if you ...

  1. #1
    Unregistered
    Guest

    is_int question

    hi..
    i was wondering if anyone knew a c++ substitute for the php function is_int: http://www.php.net/is_int

    please reply if you do (and also with which headers i need to include)

    thanks!
    laney :/

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    A variable is an int if you declare it as type int...

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    isdigit() is the closest standard function, in <cctype> or <ctype.h>.

  4. #4
    Unregistered
    Guest
    ok maybe you need to see my code then..

    i declare the variable like this:

    int range;

    and then i get input from a cin:

    cout<<"Enter number range (1-?): ";
    cin>>range;

    (this is in a loop.. the loop is here (to stop a user entering 0))

    do
    {
    cout<<"Range must be greater than 0.\r\n";
    cout<<"Enter number range (1-?): ";
    cin>>range;
    } while (range <= 0);

    the problem is if a user enters something that isn't a number then it goes into an infinite loop of the two couts above... i guessed something like is_int would fix it but i can't seem to find anything :/

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try something like -

    int i;
    cin >> i;
    while(cin.fail())
    {
    cin.clear();
    cin.ignore(1);
    cin >> i;
    }

  6. #6
    Unregistered
    Guest
    ohh works thanks

    Code:
    do
    {
    	cout<<"Range must be greater than 0.\r\n";
    	cout<<"Enter number range (1-?): ";
    	cin>>range;
    	while(cin.fail()) 
    	{ 
    		cin.clear(); 
    		cin.ignore(1); 
    		range = 0;
    	}
    } while (range <= 0);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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