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);
}
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.
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..
> "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).