How can i write it so that my program only accepts positive integers that are great than or = 0?

I want my program to ask "How many devices do you wish to add?"

And if a user enters a letter, or anything negative it will ask them to enter it again until they enter positive that is 0 or greater.

How do i tackle this?

Recommended Answers

All 7 Replies

int theint = -1;
while (theint < 0){
    //your code to collect data from user.
}

Use a while loop and the isdigit function.

Here's a starting point:

#include <ios>
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    unsigned value;

    cout << "Please enter a positive number: ";

    while (!(cin >> value) || value < 0)
    {
        if (!cin)
        {
            cout << "Invalid input\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        cout << "Please enter a positive number: ";
    }

    cout << "You entered " << value << '\n';
}

sorry for disturbing this thread. but i have confusions here:

if (!cin)

how does this statement is evaluated ?however i have played with cin and cout but never used them as such.

and one more thing :

!(cin >> value)

what does it mean ? you are placing this inside a loop(with or operator) , therefore i assume that it will return either true or false , don't know the exact value.

after seeing such statements , i think that either i learned in bad manner or books are bad(because they don't discuss more on this).

atlast , one more thing:

why are you using ios header file ? , i don't think that program really require this header file.

would you please explain these aspects(totally hidden for me upto now) ?
thanks.

When used as an expression, cin results in an istream reference that can be used in boolean context to determine its state. if (!cin) is basically the same as if (!cin.good()). Likewise, the >> operator will evaluate to the istream reference, hence if (!(cin >> value) corresponds to if (!(cin >> value).good()) or

cin >> value;

if (!cin.good())

why are you using ios header file ?

That's where the std::streamsize typedef is defined. Typically <ios> will be included along with <iostream>, but it's not strictly portable to rely on that behavior.

Typically <ios> will be included along with <iostream>, but it's not strictly portable to rely on that behavior.

The IS specifies that the header <iostream> must include the header <ios>

27.4 Standard iostream objects [iostream.objects]

27.4.1 Overview [iostream.objects.overview]

Header <iostream> synopsis

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

namespace std {
  extern istream cin;

  // etc.
}
//NOTE Oops ...
unsigned value;

cout << "Please enter a positive number: ";

while (!(cin >> value) || value < 0) // Oops value is 'unsigned' so will never be < 0
// but ... may accept, i.e. no error flag raised, input of neg values ... but re-expressed as a different positive value

An alternate way:

#include <iostream>

using namespace std;

int main()
{
    int value;

    for( ; ; )
    {
        cout << "Please enter a positive number: ";
        if( cin >> value && value >= 0 && cin.get() == '\n' )
        {
            break;
        }
        else
        {
            cout << "Invalid input\n";
            cin.clear();
            while( cin.get() != '\n' ) ; // 'flush' cin stream
        }
    }

    cout << "You entered " << value << '\n';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.