making sure an int is an int

making sure an int is an int

Post by Danny McLindo » Sat, 14 Oct 2000 04:00:00



The scenerio:
 User inputs a number, stored as an int.

The analysis:
 The program obviously wigs out if the user (is stupid) and inputs an alpha
character.

The question:
 Is there a function that checks to see if an int is actually an int before
it actually does anything (calculations) with it.
I realize I could do this...
        char a;
        cin >> a;
        if (isdigit(a)){int x = atoi(a);}
But is there one simple function to see if an int is an int?

Thanks,
Dan

 
 
 

making sure an int is an int

Post by Daniel Longes » Sat, 14 Oct 2000 04:00:00


int x;
std::cin >> x;
if (std::cin.fail())
    :std::cout << "You didn't enter an integer" << std::endl;

Daniel

--
--------------------------------------------------
Daniel Longest

"Java isn't platform-independent; it is a platform"- Stroustrup
comp.lang.c++ http://www.parashift.com/cpp-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/
 -----------------------------------------------------------------------
 Pursuant to US Code, Title 47, Chapter 5, Subchapter II, 227,
 Any and all nonsolicited commercial E-mail sent to this address
 is subject to a download and archival fee in the amount of $500.00 US.
 E-Mailing denotes the acceptance of these terms
 -----------------------------------------------------------------------

Quote:> The scenerio:
>  User inputs a number, stored as an int.

> The analysis:
>  The program obviously wigs out if the user (is stupid) and inputs an
alpha
> character.

> The question:
>  Is there a function that checks to see if an int is actually an int
before
> it actually does anything (calculations) with it.
> I realize I could do this...
>         char a;
>         cin >> a;
>         if (isdigit(a)){int x = atoi(a);}
> But is there one simple function to see if an int is an int?

> Thanks,
> Dan


 
 
 

making sure an int is an int

Post by To » Sun, 15 Oct 2000 12:02:37


On Fri, 13 Oct 2000 22:51:52 -0500, "Danny McLindon"


>The scenerio:
> User inputs a number, stored as an int.

>The analysis:
> The program obviously wigs out if the user (is stupid) and inputs an alpha
>character.

>The question:
> Is there a function that checks to see if an int is actually an int before
>it actually does anything (calculations) with it.
>I realize I could do this...
>        char a;
>        cin >> a;
>        if (isdigit(a)){int x = atoi(a);}
>But is there one simple function to see if an int is an int?

If the int can be more than one digit, then this might be the key:

int i;
cin >> i;
if (cin.good())
{
        //i is good.

Quote:}

Tom
 
 
 

making sure an int is an int

Post by Brian Dimele » Sun, 15 Oct 2000 13:06:32


Couple points on functions:
* atoi() takes a const char *, not an individual character. You can't
use it here.
* isdigit() actually takes an integer as a param. So you could use an
int *or* a char as the argument (a char would be cast implicitly). I'm
not sure if cin allows ints to be read in as chars (I guess not), but
you could do something like this:

int x;
char a;
cin >> a;
if (isdigit(a))
    x = a - '0'; // it's a trick, but it's portable and it works.
--
virtual ~brian();
http://www.TCNJ.EDU/~dimeler2

 
 
 

making sure an int is an int

Post by Danny McLindo » Sun, 15 Oct 2000 14:40:34


This works, but this won't
 if (!cin.good())
 {
 //i is bad
 }

Any ideas?

Quote:> int i;
> cin >> i;
> if (cin.good())
> {
> //i is good.
> }

> Tom

 
 
 

making sure an int is an int

Post by Josh Sebastia » Sun, 15 Oct 2000 04:00:00


On Fri, 13 Oct 2000 22:51:52 -0500, "Danny McLindon"


>The question:
> Is there a function that checks to see if an int is actually an int before
>it actually does anything (calculations) with it.
>I realize I could do this...
>        char a;
>        cin >> a;
>        if (isdigit(a)){int x = atoi(a);}
>But is there one simple function to see if an int is an int?

#include <iostream>
#include <limits>

int main()
        {
        int x;
        std::cout << "Enter an integer =>";
        while(!(std::cin >> x))
                {
                std::cout << "You didn't enter an integer.\nEnter an integer
=>";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
                }
        std::cout << "Thank you. Bye." << std::endl;
        }
_______

"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.

Josh Sebastian

 
 
 

making sure an int is an int

Post by michae » Sun, 15 Oct 2000 04:00:00


Quote:> Couple points on functions:
> * atoi() takes a const char *, not an individual character. You can't
> use it here.
> * isdigit() actually takes an integer as a param. So you could use an
> int *or* a char as the argument (a char would be cast implicitly). I'm
> not sure if cin allows ints to be read in as chars (I guess not), but
> you could do something like this:

> int x;
> char a;
> cin >> a;
> if (isdigit(a))
>     x = a - '0'; // it's a trick, but it's portable and it works.

only portable between environments in which the character set encodes
numeric characters sequencially, and in the right direction.
 
 
 

making sure an int is an int

Post by To » Tue, 17 Oct 2000 04:00:00


On Sat, 14 Oct 2000 00:40:34 -0500, "Danny McLindon"


>This works, but this won't
> if (!cin.good())
> {
> //i is bad
> }

>Any ideas?

Well, if eof is reached or if a non integer is read, cin.good() will
be false. To check explicitly for it not being an int, do

if (cin.fail())
{
//i is bad

Quote:}

In what way doesn't it work?

Tom

Quote:

>> int i;
>> cin >> i;
>> if (cin.good())
>> {
>> //i is good.
>> }

>> Tom

 
 
 

making sure an int is an int

Post by Mike Wahle » Sat, 28 Oct 2000 06:45:58



Quote:> > Couple points on functions:
> > * atoi() takes a const char *, not an individual character. You can't
> > use it here.
> > * isdigit() actually takes an integer as a param. So you could use an
> > int *or* a char as the argument (a char would be cast implicitly). I'm
> > not sure if cin allows ints to be read in as chars (I guess not), but
> > you could do something like this:

> > int x;
> > char a;
> > cin >> a;
> > if (isdigit(a))
> >     x = a - '0'; // it's a trick, but it's portable and it works.

> only portable between environments in which the character set encodes
> numeric characters sequencially, and in the right direction.

That includes every environment with a compliant
implementation of the language.

-Mike

- Show quoted text -

 
 
 

making sure an int is an int

Post by Clark S. Cox, I » Sat, 28 Oct 2000 07:02:57





> > > Couple points on functions:
> > > * atoi() takes a const char *, not an individual character. You can't
> > > use it here.
> > > * isdigit() actually takes an integer as a param. So you could use an
> > > int *or* a char as the argument (a char would be cast implicitly). I'm
> > > not sure if cin allows ints to be read in as chars (I guess not), but
> > > you could do something like this:

> > > int x;
> > > char a;
> > > cin >> a;
> > > if (isdigit(a))
> > >     x = a - '0'; // it's a trick, but it's portable and it works.

> > only portable between environments in which the character set encodes
> > numeric characters sequencially, and in the right direction.

> That includes every environment with a compliant
> implementation of the language.

Where is it dictated that (for example) ('0' + 5 == '5') ?

--
Clark S. Cox, III

http://www.whereismyhead.com/clark/

 
 
 

making sure an int is an int

Post by Mike Wahle » Thu, 02 Nov 2000 01:04:35







> > > > Couple points on functions:
> > > > * atoi() takes a const char *, not an individual character. You
can't
> > > > use it here.
> > > > * isdigit() actually takes an integer as a param. So you could use
an
> > > > int *or* a char as the argument (a char would be cast implicitly).
I'm
> > > > not sure if cin allows ints to be read in as chars (I guess not),
but
> > > > you could do something like this:

> > > > int x;
> > > > char a;
> > > > cin >> a;
> > > > if (isdigit(a))
> > > >     x = a - '0'; // it's a trick, but it's portable and it works.

> > > only portable between environments in which the character set encodes
> > > numeric characters sequencially, and in the right direction.

> > That includes every environment with a compliant
> > implementation of the language.

> Where is it dictated that (for example) ('0' + 5 == '5') ?

From ISO/IEC 9899 (The ISO C standard) 5.2.1

[ list of reequired characer set members ]

"In both the source and execution basic character sets, the
value of each character after 0 in the above list of decimal digits shall be
one greater than
the value of the previous."

I didn't see a specific statement like this one in ISO/IEC 14882
(C++ standard), but I'm pretty sure it uses the above C definition
by reference.

-Mike

-Mike

- Show quoted text -

> --
> Clark S. Cox, III

> http://www.whereismyhead.com/clark/

 
 
 

1. int initConsole(int& Width, int& Height, int& FullScreen, int& Flags,

Hi,

Can you tell me what does the following do:

int initConsole(int& Width, int& Height, int& FullScreen, int& Flags,
Screen* S)

I know it's a prototype and stuff like that. But is it anything special?
Does it replace main() in a program? If it does what does it do? Like,
what's suppose to be in it and what does the function suppose to do?

Thnx in advance

2. Guru of the Week #77: #Definition

3. void (*signal(int signum, void (*sighandler)(int)))(int);

4. CFP: AAAI96 Workshop on Reasoning about actions, planning and control

5. int i; vs. int i = int();

6. Can Toshiba XM-6102B read CD-RW

7. class(int& a,b,c) => class(int& a, int& b, int& c).

8. WebBrowser submit form

9. write(int HANDLE, const void *BUFFER, insigned int COUNT);

10. Win32's outport(int portid, int value) if exists

11. int / int ?

12. when will use INT and int in windows programming?

13. Int 25 and Int 26 under Windows 3.x