Displaying error message between array inputs

Hi I've written out the following code, its part of on an OOP program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    Household Array[5];
    
    string id;
    int members;
    float income;

    for (int j=0;j<5;j++)
    {
        cout<<"\nPlease enter the four digit ID number: ";
        getline(cin,id);
                
        cout<<"\nPlease enter the annual income: ";
        cin>>income;
                
        cout<<"\nPlease enter the number of household members";
        cin>>members;
        cin.ignore();
        
        Array[j]=Household(id,income,members);
    }  

p.s I have to use an overloaded constructor

The inputs are validated in the set methods

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
void Household::setID(string D)
{
    while (D.length()!= 4)
     {
           cout<<"\nInvalid ID number! Must be 4 digits. Re-Enter ";
           getline(cin,D);
     }
     ID=D;     
}

void Household::setIncome(float I)
{
  while (I<0)
     {
           cout<<"\nIncome must be greater than 0! Re-Enter ";
           cin>>I;
     }
  Income=I;
}
 
void Household::setMembers(int M)
{
     while (M<=0)
     {
           cout<<"\nHousehold members must be greater than 0! Re-Enter ";
           cin>>M;
     }
     Members=M;     
} 


Problem:

If the input for ID is invalid, it will first ask for the income and then the members and only thereafter ask to re enter a valid ID.

If ID and income was invalid, It will first ask for members to be entered, and only thereafter ask for valid inputs of ID and income.. and so forth.
Program acts very weird after.

How can I make it such that an input is requested straight after an incorrect input is entered.

Thanks

p.p.s new to coding so if I did not use the correct terms or if any other problems you see please let me know.
Hope I explained my problem well.
> The inputs are validated in the set methods

That is fine; but the set methods should not be responsible for user input.

Write helper functions to check if the input is valid, and call them where required.
From the constructor, from the set member functions, from main().

Something like this:
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
struct house_hold
{
    static bool valid_id( const std::string& id ) { return id.size() == 4 ; }
    static bool valid_income( float income ) { return income >= 0 ; }
    static bool valid_members( int members ) { return members > 0 ; }

    house_hold() {}
    house_hold( const std::string&, float, int ) {}


    // ....

    private:
        // ...
};

int main()
{
    using namespace std ;

    constexpr int N = 5 ;
    house_hold array[N];

    string id;
    int members;
    float income;

    for( int j=0; j<N; ++j )
    {
        cout<<"\nPlease enter the four digit ID number: " ;
        while( getline(cin,id) && !house_hold::valid_id(id) )
            cout<<"\nInvalid ID number! Must be 4 digits. Re-Enter ";

        // likewise for income and members

        // ...

        array[j] = house_hold(id,income,members);
    }
}


Thanks for your reply it does make alot of sense and I understand your solution , though what the question states exactly is:

"Create a default constructor and an overloaded constructor that accepts the identification number, the annual income for the household and the number of household members. In the set methods, validate that the identification number is four digits, annual income must be positive and the number of household members can be >=0."

Doesn't this solution not validate it in the actual set method but rather in another method? Not really what they asking of me..
Last edited on
> "In the set methods, validate .."
> Doesn't this solution not validate it in the actual set method but rather in another method? .

To me, "In the set methods, validate .."means: in the set methods, do not set the values without first validating them.

It does not mean: in the set methods, do not call any other function.
For instance, it does not mean: from within void set_income( float v ), do not call valid_income(v).
Ahh I guess that could be so. Thanks again
Topic archived. No new replies allowed.