Functios

Can someone help me out, am practising functions I first did the program bellow without functions and it worked fine then I redid it by introducing functions where necessary but the output that the system gives is wrong now.

Basically this program is supposed to calculate total charge of hours parked in a garage by subtracting exit hours from entrance hours. The problem starts at the ConvertBactToTime function (2nd functin) where extHourP variable value which was entered in the first function is lost and the system instead of returning the value entered it returns a value which i don't know where did it take it from yet I used return extHourP...

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>

using namespace std;

int InputAndValidate(int entranceHourP,int entranceMinutesP,int extHourP,int exitMinutesP)
{
    cout<<"Enter your entrance hour: ";
   cin>>entranceHourP;
   while(entranceHourP>24 ||entranceHourP<0)
   {
    cout<<"Your hour can not be more than 24hrs or less than 0hrs!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your entrance hour: ";
    cin>>entranceHourP; 
    }
    cout<<"Enter your entrance munites: ";
    cin>>entranceMinutesP;
    
   while(entranceMinutesP>59 || entranceMinutesP<0)
   {
    cout<<"Your hour can not be more than 60mins or less than 0mins!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your entrance munites: ";
    cin>>entranceMinutesP; 
    }
    
    
   cout<<"Enter your Exit hour: ";
   cin>>extHourP;
   while(extHourP>24 ||extHourP<0)
   {
    cout<<"Your hour can not be more than 24hrs or less than 0hrs!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your Exit hour: ";
    cin>>extHourP; 
    }
    cout<<"Enter your Exit munites: ";
    cin>>exitMinutesP;
    
   while(exitMinutesP>60 || exitMinutesP<0)
   {
    cout<<"Your hour can not be more than 60mins or less than 0mins!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your Exit munites: ";
    cin>>exitMinutesP; 
    }
    return entranceHourP, entranceMinutesP, extHourP, exitMinutesP;
    }


int ConvertToMinutes(int entranceHourP,int entranceMinutesP,int extHourP,int exitMinutesP,int &EntranceTimeInMinsP, int &ExitTimeInMinsP,int &minutesParkedP)
{
   EntranceTimeInMinsP=((entranceHourP*60)+entranceMinutesP);
   cout<<"Entrance Time In Mins: "<<EntranceTimeInMinsP<<endl;
   
   ExitTimeInMinsP=((extHourP*60)+ exitMinutesP);
   cout<<"Exit Time In Mins: "<<ExitTimeInMinsP<<endl;
   minutesParkedP=ExitTimeInMinsP-EntranceTimeInMinsP;
   cout<<" Time parked in minutes Parked: "<<minutesParkedP<<endl;
   return minutesParkedP; 
    }


int ConvertBactToTime(int minutesParkedP,int &parkedHoursP,int &parkedMinutesP)
{
   parkedHoursP=minutesParkedP/60;
   cout<<"Parked hours is : "<<parkedHoursP<<endl;
   parkedMinutesP= (parkedHoursP%60);
   cout<<"parked Minutes: "<<parkedMinutesP<<endl;
   return parkedHoursP,parkedMinutesP;
    }

float CalcCharge(int parkedHoursP,int parkedMinutesP, float &chargeP, float &totalChargeP)
{
       if (parkedHoursP<3)
   
    chargeP=7;
   else
   
   
   for(int r=4; r<=parkedHoursP;r++)
   {
           chargeP=(chargeP +1.50);
           }
           totalChargeP=chargeP;
   cout<<"Total charge for: "<<parkedHoursP<<"h"<<parkedMinutesP<<" is: R"<<totalChargeP<<endl;
   return chargeP, totalChargeP;
      }

main()
{
   int entranceHour,entranceMinutes;
   int extHour,exitMinutes;
   int EntranceTimeInMins,ExitTimeInMins,minutesParked;
   int parkedHours,parkedMinutes;
   float charge, totalCharge;
   
   for (int i=1;i<=10;i++)
   {
   
   
InputAndValidate(entranceHour,entranceMinutes,extHour,exitMinutes);
ConvertToMinutes(entranceHour,entranceMinutes,extHour,exitMinutes,EntranceTimeInMins,ExitTimeInMins,minutesParked);
ConvertBactToTime(minutesParked,parkedHours,parkedMinutes);
CalcCharge(parkedHours,parkedMinutes,charge,totalCharge);
   
  
} 
    system("pause");
}
Last edited on
*edit*
This has been updated to remove what was incorrect.

If you change the code below, you will see a improvement but that's not the only problem.

1
2
3
4
5
6
   
int entranceHour=0,entranceMinutes=0;
   int extHour=0,exitMinutes=0;
   int EntranceTimeInMins=0,ExitTimeInMins=0,minutesParked=0;
   int parkedHours=0,parkedMinutes=0;
   float charge=0, totalCharge=0;



add this to a few places in your code.
cout << "Enter: " << entranceHourP <<":" <<entranceMinutesP << " Exit: " << extHourP << ":" << exitMinutesP << endl; // Test

try changing your starting values and see what changes occur.

1
2
3
4
5
   int entranceHour=1,entranceMinutes=1;
   int extHour=1,exitMinutes=1;
   int EntranceTimeInMins=0,ExitTimeInMins=0,minutesParked=0;
   int parkedHours=0,parkedMinutes=0;
   float charge=0, totalCharge=0;
Last edited on
Functions can only have one return value, so statements like this: return chargeP, totalChargeP; are invalid.
closed account (3qX21hU5)
Also when declaring your functions put the declarations on top of the page, and put the whole functions on the bottom of the page.

Like Fransje said
Functions can only have one return value, so statements like this: return chargeP, totalChargeP; are invalid.

so you can only have one return value per function, there are ways to get around this in certain instances by having a parameter that is a reference to a object you would want to place one of your results from the function like this following code that I used for a previous exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
istream &read_hw(istream &in, vector<double> &hw)
{
    if (in)
    {
        hw.clear();
        
        double x;
        while (in >> x)
            hw.push_back(x);
            
        in.clear();
    }
    return in;
}


This returns istream in, but also changes the vector since the parameter is a reference.
Last edited on
This is by no means finished but it works better. I left my debug statements in so you can see what I did to help find what was wrong.



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<iostream>

using namespace std;

   int entranceHour=0,entranceMinutes=0;
   int extHour=0,exitMinutes=0;
   int EntranceTimeInMins=0,ExitTimeInMins=0,minutesParked=0;
   int parkedHours=0,parkedMinutes=0;
   float charge=0, totalCharge=0;


int InputAndValidate()
{

cout << "\t\t\t\t\t\t\t\t1 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

    cout<<"Enter your entrance hour: ";
   cin>>entranceHour;
   while(entranceHour>24 ||entranceHour<0)
   {
    cout<<"Your hour can not be more than 24hrs or less than 0hrs!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your entrance hour: ";
    cin>>entranceHour;
    }
    cout<<"Enter your entrance munites: ";
    cin>>entranceMinutes;
    
   while(entranceMinutes>59 || entranceMinutes<0)
   {
    cout<<"Your hour can not be more than 60mins or less than 0mins!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your entrance munites: ";
    cin>>entranceMinutes;
    }
    
    
   cout<<"Enter your Exit hour: ";
   cin>>extHour;
   while(extHour>24 ||extHour<0)
   {
    cout<<"Your hour can not be more than 24hrs or less than 0hrs!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your Exit hour: ";
    cin>>extHour; 
    }
    cout<<"Enter your Exit munites: ";
    cin>>exitMinutes;
    
   while(exitMinutes>60 || exitMinutes<0)
   {
    cout<<"Your hour can not be more than 60mins or less than 0mins!"<<endl;
    cout<<"Try and enter the correct time!"<<endl;
    cout<<"Enter your Exit munites: ";
    cin>>exitMinutes; 
    }

cout << "\t\t\t\t\t\t\t\t2 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

    return entranceHour, entranceMinutes, extHour, exitMinutes;
    }


int ConvertToMinutes()
{

cout << "\t\t\t\t\t\t\t\t3 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

   EntranceTimeInMins=((entranceHour*60)+entranceMinutes);
   cout<<"Entrance Time In Mins: "<<EntranceTimeInMins<<endl;
   
   ExitTimeInMins=((extHour*60)+ exitMinutes);
   cout<<"Exit Time In Mins: "<<ExitTimeInMins<<endl;
   minutesParked=ExitTimeInMins-EntranceTimeInMins;
   cout<<" Time parked in minutes Parked: "<<minutesParked<<endl;
   return minutesParked; 
    }


int ConvertBactToTime(int minutesParked,int &parkedHours,int &parkedMinutes)
{

// cout << "\t\t\t\t\t\t\t\t4 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

   parkedHours=minutesParked/60;
   cout<<"Parked hours is : "<<parkedHours<<endl;
   parkedMinutes= (parkedHours%60);
   cout<<"parked Minutes: "<<parkedMinutes<<endl;
   return parkedHours,parkedMinutes;
    }

float CalcCharge(int parkedHours,int parkedMinutes, float &charge, float &totalcharge)
{
       if (parkedHours<3)
   
    charge=7;
   else
   
   
   for(int r=4; r<=parkedHours;r++)
   {
           charge=(charge +1.50);
           }
           totalcharge=charge;
   cout<<"Total charge for: "<<parkedHours<<"h"<<parkedMinutes<<" is: R"<<totalcharge<<endl;
   return charge, totalcharge;
      }

main()
{
   for (int i=1;i<=10;i++)
   {
InputAndValidate();
entranceHour, entranceMinutes, extHour, exitMinutes;
cout << "\t\t\t\t\t\t\t\tM1 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

ConvertToMinutes();
cout << "\t\t\t\t\t\t\t\tM2 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

ConvertBactToTime(minutesParked,parkedHours,parkedMinutes);
cout << "\t\t\t\t\t\t\t\tM3 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test 

CalcCharge(parkedHours,parkedMinutes,charge,totalCharge);
cout << "\t\t\t\t\t\t\t\tM4 Enter: " << entranceHour <<":" <<entranceMinutes << " Exit: " << extHour << ":" << exitMinutes << endl; // Test    
  
} 
    system("pause");
}


Output should look like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
C:\Temp>testxxx
                                                                1 Enter: 0:0 Exit: 0:0
Enter your entrance hour: 1
Enter your entrance munites: 2
Enter your Exit hour: 3
Enter your Exit munites: 4
                                                                2 Enter: 1:2 Exit: 3:4
                                                                M1 Enter: 1:2 Exit: 3:4
                                                                3 Enter: 1:2 Exit: 3:4
Entrance Time In Mins: 62
Exit Time In Mins: 184
 Time parked in minutes Parked: 122
                                                                M2 Enter: 1:2 Exit: 3:4
Parked hours is : 2
parked Minutes: 2
                                                                M3 Enter: 1:2 Exit: 3:4
Total charge for: 2h2 is: R7
                                                                M4 Enter: 1:2 Exit: 3:4
                                                                1 Enter: 1:2 Exit: 3:4
Enter your entrance hour:
Last edited on
closed account (3qX21hU5)
The reason the int a, and b change to 1 and 2 is because they are global variables and it is a SIDE EFFECT of your function to change a to 1, and b to 2. Your funtion is not returning both values. The only reason they change is because the variables are global and the function has access to them so it can change the value of them.

if you run this it wont work
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
// function example
#include <iostream>
using namespace std;


int ab(;

int main ()
{
    int a=0;
    int b=0;
    ab();
    cout << a << " " << b << endl;
    return 0;
}

// This function really does not do anything but create a local variables in
// the function named a and b and assigns 1 and 2 to them.
int ab()
{
    int a, b;
    a = 1;
    b = 2;
    return (a,b);
}


Last edited on
@SamuelAdams Thanks, now I see that i should have made my variables global, and the program is working.
@Francej & Brandon Jumbeck- I did not know that am not supposed to return many variables but now I know and thanks for your inputs
closed account (3qX21hU5)
@SamuelAdams global variables are very dangerous and can cause alot of problems in programs specially the way you are using them. Please learn how functions work before teaching them to new people ;p. SO PLEASE DONT TEACH PEOPLE TO USE GLOBAL VARIABLES UNLESS THEY KNOW WHAT THEY DO AND HOW IT AFFECT THE PROGRAM IN WHOLE.

@hombakazij Actually you should not use global variables in this program. The code that SamuelAdams posted is not in any means the correct way to use functions you can only have 1 return from a function. I would highly recommend you look back into functions here are some sources that you can use.

http://www.cplusplus.com/doc/tutorial/functions/

and

http://www.cplusplus.com/doc/tutorial/functions2/

Hopefully them will help you understand them more. Also would like to stress once again that using global variables like samueladams did in his code is NOT the correct way to re write your program to use functions!
@Zereo thanks a lot. Can you tell me? if I have cin two variables in func1
e.g
1
2
3
4
5
6
7
8
9
[code]int Func1(int a, int b )
{
 cout<<"Enter first number:";
 cin>>a;
 cout<<"Enter Sec number:";
 cin>>b;
 
 return(a*b);
}

and I want to use the same variables and their values for calculations in Func2 how do I make the values of variables a nd b available in Func2
e.g
1
2
3
4
5
6
int Func2(int a, int b )
{
  
 return (a/b);
 
}

so that
1
2
3
4
5
6
7
mai()
{
  int x,y;
  cout<<Func1(x,y);
 cout<<Func2(x,y);
 return 0;
}

because it seems to me as if the value of a in Func2 is available but the value of b get lost and the system would give b any random value as if it was not initialised value resulting in an incorrect output for func2.
Last edited on
Topic archived. No new replies allowed.