Function that doesn't allow user inputted negative numbers

I'm trying to work a function into my program that won't allow the user to input a negative number. The function I have so far is:

1
2
3
4
5
6
7
8
9
10
11
12
int negatives ()
{
     int input;

     cin >> input;

     if (input < 0)
     {
          cout << "Input cannot be less than 0." << endl;
     }
     return input;
}


However, when I insert it into another function:

1
2
3
4
5
6
7
8
9
10
void userinput(double &feet, double &inches)
{

	cout << "Enter number of feet: ";
	cin >> negatives ();

	cout << "Enter number of inches: ";
	cin >> negatives ();

}


the program fails to compile. I'm probably making yet another horribly amateur mistake but I'm completely stumped. Any suggestions?
closed account (3qX21hU5)
Your writing cin >> negatives (); // Trying to write into a function call Delete the cin and the >> operator.

Also I would suggest you reread about functions and how they work because your not stoping the user from entering a negative number with your function and its not even storing the number it is giving it basically just tells the user if the number is negative then erases everything.
Last edited on
Thank you for the reply. How would I go about not only informing the user that they entered a negative number but prevent my program from continuing to the next input option?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int get_non_negative()
{
    int value ;
    std::cout << "enter a non-negative integer: " ;

    if( std::cin >> value && value >= 0 ) return value ;
    else
    {
        std::cin.clear() ; // clear possible error state
        std::cin.ignore( 1000, '\n' ) ; // empty the input buffer
        return get_non_negative() ; // and try again
    }
}

int main()
{
    int value = get_non_negative() ;
    // use value
}
JLBorges,

Thank you for the example. I tried to implement it into my program but once the user enters positive numbers (after entering negative numbers and being told to enter non-negatives) my equation function will use the initial negative inputs instead of the corrected inputs.

I placed the "get_non_negative()" function after the cin >> in my input coding
1
2
3
4
5
6
7
8
9
10
void userinput(double &feet, double &inches)
{
	cout << "Enter number of feet: ";
	cin >> feet;
        get_non_negative();

	cout << "Enter number of inches: ";
	cin >> inches;
        get_non_negative();
}


I tried inserting the get_non_negatives() three different positions (before cout, after, and after cin) to no avail.
First, modify get_non_negative() to accept and return a non-negative real number:
1
2
3
4
5
6
7
8
9
10
11
12
13
double get_non_negative()
{
    double value ;
    std::cout << "enter a non-negative number: " ;

    if( std::cin >> value && value >= 0 ) return value ;
    else
    {
        std::cin.clear() ; // clear possible error state
        std::cin.ignore( 1000, '\n' ) ; // empty the input buffer
        return get_non_negative() ; // and try again
    }
}


Then, modify userinput() to use the return value of get_non_negative():
1
2
3
4
5
6
7
8
9
10
void userinput( double &feet, double &inches )
{
	std::cout << " number of feet? ";
	// cin >> feet; 
        feet = get_non_negative(); // ***

	std::cout << " number of inches? ";
	// cin >> inches;
        inches = get_non_negative(); // ***
}
Topic archived. No new replies allowed.