How would I do something like this? I know that this will not work, I'm just using it to show what I need with help. When a user is suppose to enter a number, but instead enters a string or char, it gives him a cout message saying it doesn't work.

int Variable;
string SomeString
if (int Variable = SomeString)
    cout << "Must enter a number" << endl;

and the other way around

int Variable;
string SomeString
if (SomeString = Variable)
    cout << "Must enter a string" << endl;

Recommended Answers

All 3 Replies

cin's overloaded >> operator returns a reference to the stream, which can in turn be used in a boolean context to determine the state. If you ask for a number and an invalid format is encountered, the stream enters an error state:

if (!(cin>> number))
{
  cout<<"Must enter a number\n";
}

Of course, there's more to it if you want to recover from the erroneous input because it won't be extracted and the next request for a number will fail again:

while (!(cin>> number))
{
  // Prompt for more input
  cout<<"Invalid input. Please enter a number: ";

  // Clear the stream error state
  cin.clear();

  // Remove the erroneous characters (clear the stream)
  cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

cin's overloaded >> operator returns a reference to the stream, which can in turn be used in a boolean context to determine the state. If you ask for a number and an invalid format is encountered, the stream enters an error state:

if (!(cin>> number))
{
  cout<<"Must enter a number\n";
}

Of course, there's more to it if you want to recover from the erroneous input because it won't be extracted and the next request for a number will fail again:

while (!(cin>> number))
{
  // Prompt for more input
  cout<<"Invalid input. Please enter a number: ";

  // Clear the stream error state
  cin.clear();

  // Remove the erroneous characters (clear the stream)
  cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

Is number an int? I tried that on my program and every time i type a char, it spams my while loop. Heres my code

while (H)
      {
         int Month;
         int Day;
      
         cout << "Date: ";
         cout << "  Day: ";
	      cin >> Day;
         cout << "\tMonth: ";
         cin >> Month;

         if ( Month > 0 && Month <=12 && Day <= 31 && Day >=1 )
         {
            Bird[K].Date = DatetoString(Day,Month);
            H = false;
            break;
         }
         else if ( Month > 12 || Month < 0 )
            cout << "Invalid Month" << endl;
         else if ( Day > 31 || Day < 1 )
            cout << "Invalid Date" << endl;
         else if (!(cin>>Day))
            cout << "Please enter a number from 1-31" << endl;
      }

I don't see the clearing of cin in your code. Here's a full program using Narue's code. Upon failure, you need to clear it and throw away the bad characters.

#include <iostream>
using namespace std;


int main ()
{
    int number = 0; // anything but -1.

    while (number != -1)
    {
        cout << "Enter an integer.  Enter -1 to quit : ";
        while (!(cin>> number))
        {
            // Prompt for more input
            cout<<"Invalid input. Please enter a number: ";

            // Clear the stream error state
            cin.clear();

            // Remove the erroneous characters (clear the stream)
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        cout << "You entered " << number << endl;
    }

    cout << "You entered -1, so I am quiting.  Have a nice day." << endl;
    return 0;
}
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.