Writing if/else statement as a function

Hello, again guys. I'm trying to properly write out an if/else statement as a function that returns a true or a false to indicate if the end time is greater than the start time but I'm having a little trouble getting it right. Any help would be appreciated.

1
2
3
4
5
6
7
8
9
    if (end > start) //If/else to determine if end time is greater than start time. 
  {
    cout << "Your elapsed time is " << timeDifference(start, end) << " minutes."<<endl; // Display the elapsed time
  }
   else
   {
    cout <<"Error - End time must be greater than start time." <<endl; // Display error if start time is greater than end time
   }  
1
2
3
4
5
6
bool isEndGreaterThanStart( int end, int start)
{
  if (end > start) return true;

  return false;
}
You haven't made a function, unless you're referring to timeDifference.

Do you want 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
// Example program
#include <iostream>

using std::cout;
using std::endl;

int timeDifference(int start, int end)
{
    return end - start;   
}

bool myFunc(int start, int end)
{
    if (end > start)
    {
        cout << "Your elapsed time is " << timeDifference(start, end) << " minutes." << endl;
        return true;
    }
    else
    {
        cout << "Error" <<endl;
        return false;
    }  
}

int main()
{
    if (myFunc(3, 5))
    {
        std::cout << "Test 1 passed. True expected\n";   
    }
    if (!myFunc(5, 3))
    {
        std::cout << "Test 2 passed. False expected.\n";   
    }
}
If I understood it right, you need to put it into a functions that returns a boolean value. So, you can either write the function itself over the main function, or you can write its signature at the top of the main and writing its implementation under the main.
Well, you need to specify the function's return type, in your case is boolean. Than, you need to write its name, the function name, you can use timeCompare. Now you place your function parameters, that I believe that they are both doubles. Finally, you put that code you wrote into de function declaration, and add "return" statements. Something like this:

1
2
3
4
5
6
7
8
9
bool timeCompare(double end, double start){
    if(end > start){
        cout << "Your elapsed time is " << timeDifference(start, end) << " minutes."<<endl;
        return true;
    } else {
        cout <<"Error - End time must be greater than start time." <<endl;
        return false;
    }
}


Ok, than now you just call that function whenever you need it, like that:

 
timeCompare(end, start);

And also, expect a boolean value when calling it;

That's it, hope it helps
I prefer this style. Its just that, style... no real effect.
1
2
3
4
bool isEndGreaterThanStart( int end, int start)
{
  return(end > start);
}
Thank you both for your assistance. So I can essentially place the if/
else within the function?
As you can see, I just copied your if/else code inside the function and added return statements.
So I've edited my original code but am a little confused as to where to call the "bool . . ." function. Also with my code like this, the calculations don't occur.

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
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
int timeDifference(int start, int end); // function declaration  
int minutes(int time);// function declaration
bool endGreaterThanStart(int Start, int end); 
int main()
 { 
  int start, end;
  char again ='Y';// Identify sentinel as the letter Y
  while (again == 'y' || again == 'Y') // While loop to ask user if they would like to re-run program.
  {
         cout << "Please input your start time in military time(example: 830=2030): "; // Asking user for start time
         cin >> start;
         cout << "Please input your end time: "; //Asking user for end time
         cin >> end;
         
  //if (end > start) //If/else to determine if end time is greater than start time 
  //{
  //  cout << "Your elapsed time is " << timeDifference(start, end) << " minutes."<<endl; // Display the //elapsed time
//  }
  // else
   {
    cout <<"Error - End time must be greater than start time." <<endl; // Display error if start time is greater than end time
   }  
    cout << "\n\nTo run the program again enter y or Y)."; // Ask user if they would like to re-run the program
         cin >> again ;
  }
 return 0;
}//end main
  int minutes(int time) //Declaring function
 {
    int hours = time/100; //determining hours
    int minutes = time%100; //determining minutes
    return hours*60 + minutes; // hours plus minutes
 }

  int timeDifference(int start, int end) //declaring function
 {
    return minutes(end) - minutes(start); //determing end minutes minus start minutes
 }

bool endGreaterThanStart(int start, int end)
	{
    if (end > start)
    {
        cout << "Your elapsed time is " << timeDifference(start, end) << " minutes." << endl;
        return true;
    }
    else
    {
        cout << "Error - End time must be greater than start time." <<endl;
        return false;
    }  
}

line 18 just say
if ( endGreaterThanStart()) cout … etc
Thanks jonnin, if I understand you correctly the code should look 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;
int timeDifference(int, int); // function declaration
int end;
int start;
int minutes(int time);// function declaration
bool endGreaterThanStart(int start, int end);
int main()
 { 
  int start, end;
  char again ='Y';// Identify sentinel as the letter Y
  while (again == 'y' || again == 'Y') // While loop to ask user if they would like to re-run program.
  {
         cout << "Please input your start time in military time(example: 830=2030): "; // Asking user for start time
         cin >> start;
         cout << "Please input your end time: "; //Asking user for end time
         cin >> end;
         
  if (endGreaterThanStart(start, end)) //If/else to determine if end time is greater than start time
  {
    cout << "Your elapsed time is " << timeDifference(start, end) << " minutes."<<endl; // Display the elapsed time
  }
   else
   {
    cout <<"Error - End time must be greater than start time." <<endl; // Display error if start time is greater than end time
   }  
    cout << "\n\nTo run the program again enter y or Y)."; // Ask user if they would like to re-run the program
         cin >> again ;
  }
 return 0;
}//end main
  int minutes(int time) //Declaring function
 {
    int hours = time/100; //determining hours
    int minutes = time%100; //determining minutes
    return hours*60 + minutes; // hours plus minutes
 }

  int timeDifference(int start, int end) //declaring function
 {
    return minutes(end) - minutes(start); //determing end minutes minus start minutes
}
bool endGreaterThanStart(int start, int end)
	{
    if (end > start)
    {
        cout << "Your elapsed time is " << timeDifference(start, end) << " minutes." << endl;
        return true;
    }
    else
    {
        cout << "Error - End time must be greater than start time." <<endl;
        return false;
    }  
}


When I run this code the program runs but I get either the calculated minutes or the error message printed twice on the screen depending upon the user's inputs. How would I go about correcting that?
Delete lines 48 and 53.
Last edited on
Thank you, Ganado, I don't know why I didn't think of that. lol
yes but now consider what we told you above...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool endGreaterThanStart(int start, int end)
	{
    if (end > start) //possible unnecessary jump statement, and text bloat (more code and whitespace than needed to express the idea). 
    {
        return true;
    }
    else  //unnecessary jump?  Probably not, but if you chained more elses it could
    {
        return false;
    }  
}

just say this:

bool endGreaterThanStart(int start, int end)
{
   return (end > start);
}


Jumps are not always bad; it depends on the final cpu instruction level code and cpu family and more (in truth, the ones here are likely not a problem), but it is still generally faster code to avoid unneeded ones as a habit.
Last edited on
Topic archived. No new replies allowed.