-2

I was assigned to write a program in C++ to take user input, perform some calculation, then present the user with the information after calculations were performed.

The specifics of the program are not needed, since my question is in reference to input validation specifically. What is considered to be better practice / more practical for validating multiple user inputs?

  1. Using a while loop in int main() to catch bad input after every cin statement

OR

  1. Passing input to a function with 1 while loop to validate the input, the return the input back.

I am fairly new to programming in general, and have only been working in C++ for a few weeks, and my greenhorn instincts would tell me to create a few while loops in int main() and call it a day. Something tells me a function would be a better idea, because, you know, object orientation.

Thanks in advance. - Wes

Wes Koerber
  • 71
  • 1
  • 8
  • 1
    Might be relevant: https://stackoverflow.com/questions/2075898/good-input-validation-loop-using-cin-c – aug Oct 11 '17 at 02:02
  • My advice is to try both and then decide for yourself. You will gain much better experience from being hands on and not relying on other peoples subjective preferences. Having said that you may find if you have to take multiple nested inputs and want to be able to quit out from any of those, the function is better for the nested inputs as it avoids the issue of `break`ing from nested loops. – Paul Rooney Oct 11 '17 at 02:05

3 Answers3

0

You're correct in that you should put it into a function, encapsulation is always easier in the long run (even if you're never gonna look at this program again), and it's useful to keep good practice no matter the project!

If you're not intending on making any changes to the input and it's a case of valid input or invalid input, then you could always return a bool so you could do something like

if(inputValidator() == true)
{
    //perform calculations
}
else 
{
    //Tell the user their input is incorrect and prompt for the input again
}
user0042
  • 7,917
  • 3
  • 24
  • 39
Kris H
  • 26
  • How would I do that without initializing a variable to pass to `inputValidator()`? For instance, I want the user to input a number, which will be stored in `int a`, but when performing logic checks in a condition, such as `if (inputValidator(a) == true)`, the compiler throws an error, saying that `int a` is uninitialized. Am I missing something? – Wes Koerber Oct 11 '17 at 02:24
  • when creating the variable a just initialize it to 0 or something else of your choosing. It would look something like this. int a = 0; – Kris H Oct 12 '17 at 07:26
-1

Making a function to validate an input is an interesting way to solve the problem. But I doubt that it will not be necessary at the level you are coding - since you said you are a beginner programmer. If you are only checking one input at a time the best way to go will be just a simple while if you are checking multiple looks you can use something similar to while but use a for loop to go through all the entries. Best of luck to you!

D.H.
  • 186
  • 14
-1

In the scope which you described, it is usually not necessary to create a function, as you can just clump everything into int main and not have much issue.

However, if your list is a variable size, you could use a for loop based on the number of inputs you receive, and create an exception checker for them. Such as:

string myOutput;
for (int i = 0; i < totalInputs; i++) {
    cin << myInput;
    myOutput = exeptionHandle(myInput);
    cout << myOutput;
}