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.
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?
#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
}
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.