I have a main program that asks for several inputs, days, rate, expenses, and purchases.
Can I have a function that checks all of these to make sure that they are greater than 0 and then goes back to the variable that is less than zero and then the user can input a correct value?
I have done this before:
while (deposit < 0.0)
{
cout << "The deposit cannot be negative.\n";
cin >> deposit;
}
But, I would have to write this several times after each variable.
I am very beginner, so I'm thinking I would want a function to run each time to check for an invalid entry, but I don't know how I would create a generic function to test all variables and then go back to the appropriate spot to continue.
It would be done with simple control flows. If it is negative continue the loop and re-ask, otherwise go on and do something with the data, or ask for the next value in the sequence:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main(){
double value;
while (true){
std::cout << "Enter positive value: ";
std::cin >> value;
if (value < 0){
std::cout << "I said positive!" << std::endl;
continue;
}
std::cout << "doing something with the value of " << value << std::endl;
}
}