Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 18th, 2007, 9:09 PM   #1
IceCold19
Newbie
 
Join Date: Jan 2007
Posts: 2
Rep Power: 0 IceCold19 is on a distinguished road
Find if something is an int

I need to read in a value, declared as

int anIntVariable = 0;

via cin

cin >> anIntVariable

I now need to check if it is indeed an int value. How do I check if this is something other than an integer?
IceCold19 is offline   Reply With Quote
Old Jan 18th, 2007, 9:28 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
Check the state of the stream. If it can't complete an operation, it will be in an error state. For example;
#include <iostream>

int main()
{
    int value;
    while (std::cin.eof())
    {
       std::cin >> value;
       if (std::cin.bad())    // if bad data in stream
          std::cin.clear();   //  clear the error state
    }
}
Will loop forever attempting to read an int from cin until an end-of-file indication occurs. I'll leave it as an exercise as to how a user can generate an end-of-file condition in cin, as the means of doing that are system dependent.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 2:35 AM
Vector problem - find max, min and positions codylee270 C++ 12 Mar 20th, 2006 1:57 AM
Find a matching element in an XML file Eleo C# 2 Feb 16th, 2006 9:25 PM
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
Program that can find a patern Anyways C++ 2 Mar 22nd, 2005 3:27 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC