Boolean functions

I'm confused as to how call a function. I know I'm doing it wrong, but I can't find anything that describes how to call it. I want main to call isInvalid to determine if input is invalid. Once its determined I want main to take invalid input and do one thing and take valid input and do another thing.

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
bool isInvalid (int grade, int hoursWorked, int IDnum, bool invalid)

while (!myInFile.eof()){
bool invalid = false;

     isInvalid (grade, IDnum, hoursWorked, invalid);
      if (invalid = true){
          myOutFile << setw(12) << grade << setw(12) << IDnum << setw(12) 
                << hoursWorked << setw(12) << " -- INVALID INPUT LINE -- SKIPPED"
                << endl;
                }
      else if ( invalid = false){
      computewages (grade, IDnum, hoursWorked, wages) ;
      myOutFile << fixed << gradeString(grade) << " plumber number " << 
                   setw(12) << IDnum << "worked " << setw(6) << hoursWorked 
                   << "hours, and is paid $" << setw(12) << setprecision(2)
                   << wages << endl;
      }

bool isInvalid (int grade, int hoursWorked, int IDnum, bool invalid)  {
const int MIN_GRADE = 0;     //minimum grade 
const int MAX_GRADE = 2;     //maximum grade
const int MAX_ID_NUM = 999999; //maximum ID number
const int MAX_HOURS = 80;     //maximum hours that can be worked

   if ((grade < MIN_GRADE) || (grade > MAX_GRADE) || (IDnum > MAX_ID_NUM) ||
       (hoursWorked > MAX_HOURS))
       invalid = true;
   else {
       invalid = false;}




Last edited on
You need to have the function return the result

1
2
3
4
5
6
7
8
9
10
11
12
bool isInvalid (int grade, int hoursWorked, int IDnum)  {
const int MIN_GRADE = 0;     //minimum grade 
const int MAX_GRADE = 2;     //maximum grade
const int MAX_ID_NUM = 999999; //maximum ID number
const int MAX_HOURS = 80;     //maximum hours that can be worked

   if ((grade < MIN_GRADE) || (grade > MAX_GRADE) || (IDnum > MAX_ID_NUM) ||
       (hoursWorked > MAX_HOURS))
       return = true;
   else {
       return = false;
}


you can then treat the function call as if it were a boolean.
Since it's boolean, you can simplify the If..Else as there is no need to check the value in the else - if it is not true, it must be false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (!myInFile.eof()){

     if (isInvalid (grade, IDnum, hoursWorked)){
          myOutFile << setw(12) << grade << setw(12) << IDnum << setw(12) 
                << hoursWorked << setw(12) << " -- INVALID INPUT LINE -- SKIPPED"
                << endl;
                }
      else {
      computewages (grade, IDnum, hoursWorked, wages) ;
      myOutFile << fixed << gradeString(grade) << " plumber number " << 
                   setw(12) << IDnum << "worked " << setw(6) << hoursWorked 
                   << "hours, and is paid $" << setw(12) << setprecision(2)
                   << wages << endl;
      }
Topic archived. No new replies allowed.