Validating user input twice

Hey ya'll I'm working with classes and need some help with validating user input from the main. I'm supposed to set and verify 5 array object with data entered by the user.The setter methods that I created already check for the correct user input, but I'd like to check again. My question is how do I verify that user input is okay before it is sent to the setters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int main()
{
    string user_input;
    const int SIZE = 5;
    Inventory repairs [SIZE] = {
                Inventory(),
                Inventory(),
                Inventory(),
                Inventory(),
                Inventory()
    };
    
    //prompt user
    //read in input
    //check if input is bad, if so display error
    //check for input again
    //end loop
    //call setters to fill array
    for(int i = 0; i<SIZE; i++)
    {
        cout<<"Enter your check-in number.\n";
        getline(cin,user_input);
        repairs[i].set_check_in(user_input);
        cout<<"Enter a description of damaged surfboard.\n";
        getline(cin,user_input);
        repairs[i].set_damageDesc(user_input);
        cout<<"Enter your last name.\n";
        getline(cin,user_input);
        repairs[i].set_last_name(user_input);
        cout<<"Enter your phone number.\n";
        getline(cin,user_input);
        repairs[i].set_phone_number(user_input);
        cout<<"Enter quoted price.\n";
        getline(cin,user_input);
        repairs[i].set_quote(user_input);
        cout<<"Enter amount of hours the fix took\n";
        getline(cin,user_input);
        repairs[i].set_work_hours(user_input);
    }

    for(int i = 0; i<SIZE; i++)
    {
        cout<<"Check in #: "<<repairs[i].get_check_in_number()<<"\n";
        cout<<"Repairs needed: "<<repairs[i].get_damage()<<"\n";
        cout<<"Last Name: "<<repairs[i].get_last_name()<<"\n";
        cout<<"Phone Number: "<<repairs[i].get_phone_number()<<"\n";
        cout<<"Quote: "<<repairs[i].get_quote()<<"\n";
        cout<<"Hours Worked: "<<repairs[i].get_hours()<<"\n"<<endl;
    }
    return 0;
}
Last edited on
The setter methods that I created already check for the correct user input, but I'd like to check again.

Why? Do you suppose the data will change between reading it and storing it into the array?

Instead of lines 5 through 11, write
Inventory repairs [SIZE] = {};
To the same effect.

Why isn't set_damageDesc named set_damage_desc?

My question is how do I verify that user input is okay before it is sent to the setters.

Post the definition of Inventory and the assignment.
Last edited on
One of the points in having setters is that they do any required validation. If the setters just set the given value (and the getters just get the value), there's not much point having setters/getters. The member variables might just as well be public.
Topic archived. No new replies allowed.