I am writing a program that asks for numeric input until a negative number is given. My question is how do I test for no input? I currently have:

if ( userInput == "" )
    {
       cout << "NOTHING TO PROCESS" << endl;
     }

But I get: " ISO C++ forbids comparison between pointer and integer". What do I need to do in order to get this to work properly?

What type of thing is userInput?

it is an integer (sorry forgot to include that).

the program is two functions to fin the minimum and maximum values of those that the user input

Member Avatar for Mouche

If userInput is an int, it's never going to be an empty string.

Ah, that's easier. Just loop until userInput < 0 .

while (true) {  // do loop forever
  // get input here --> userInput
  if (userInput < 0) break;  // quit if user gives a neg num
  // do the min/max stuff here
  }
//print the min/max here

Hope this helps.

Here is what I have so far.

#include <iostream>
using namespace std;

void minmax ( int &minimum, int &maximum );

int main()
{

	int min, max;
	min = max = 0;
	
	minmax ( min, max );
	
	cout << min << ":" << max << endl;

    cin >> min;

    return 0;

}

void minmax( int &minimum, int &maximum )
{
 
	int userInput;
    userInput = 0;
    
    cout << "Please enter a number (negative value to end):  ";
    cin >> userInput;
    
    minimum = maximum = userInput;
    
    do
    {
         cout << "Please enter a number (negative value to end):  ";
	     cin >> userInput;
	     
         if ( userInput < 0 )
              break;
         else if ( userInput <= minimum )
              minimum = userInput;
         else if ( userInput >= maximum )
              maximum = userInput;
	     
    }
    while ( userInput >= 0 );
    
    
    
}

The assignment specifically asks that if there is no input, to print "NOTHING TO PROCESS". If I can't do it with an integer, can I have it input as a string and then convert it to the numeric equivalent? How would I go about doing it?

The program works the way it is supposed to, I just can't figure out how to implement the "null" input instance.

Oh. For that just say:

cin >> userInput;
if (!cin) {
  cout << "NOTHING TO PROCESS\n";
  break;
  }

This still trips if you enter something other than an integer, but to be more specific means you'd have to read the input as a string then convert it to an integer. Ask your professor if he would rather you read input as a string or as an integer.

Hope this helps.

Ok I tried if (!cin) but it just goes to the next line not accepting null input.

I can't figure this out, please HELP!

#include <iostream>
using namespace std;

void minmax ( int &minimum, int &maximum );

int main()
{

	int min, max;
	
	minmax ( min, max );
	
    cout << min << ":" << max << endl;
	
	cin >> min;

    return 0;

}

void minmax( int &minimum, int &maximum )
{
 
	int userInput;
	userInput = 0;

    minimum = maximum = userInput;
    
    do
    {
         cout << "Please enter a number (negative value to end):  ";
	     cin >> userInput;
	     
	     if ( minimum == 0 && maximum == 0 )
	          minimum = maximum = userInput;
              
         if ( userInput < 0 )
              break;
         else if ( userInput <= minimum )
              minimum = userInput;
         else if ( userInput >= maximum )
              maximum = userInput;
	     
    }
    while ( userInput >= 0 );
    
}

Take lines 2..5 of my last post and stick them in front of line #37 you'll get what you want.

After doing that, you can delete line #15. You don't need to get min again after you have finished everything else. (Or are you just using this line to keep the console showing?)

Testing an istream for a boolean value returns whether or not you have reached EOF. So saying if (!cin) is the same as saying if (I_tried_to_read_past_the_EOF()) Hope this helps.

No dice. Is there a header I need to include?

No dice. Is there a header I need to include?

Headers don't do anything. The problem is cin requires input when you specify a variable. You can't enter nothing. Using cin.getline() will work, though.

It works fine for me.

If you are using cin you should be aware that the standard input stream only ends when you press Ctrl-Z, ENTER on Windows or Ctrl-D on Linux.

Otherwise, you will have to get input as a string and convert it. (Sorry I was so slow on the uptake here...)

string userInputString;
int userInput;
...
if (!getline( cin, userInputString ) or userInputString.empty()) {
  cout << "NO INPUT\n";
  break;
  }
if (!(stringstream( userInputString ) >> userInput)) {
  cout << "NOT A NUMBER\n";
  break;
  }
if (userInput < 0) break;
...

Before you go sticking this in your code though, you ought to go talk to your professor and see what he really wants you to do.

Good luck.

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.