| hombakazij (14) | |||
|
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...
| |||
|
Last edited on
|
|||
| SamuelAdams (322) | |||||
|
*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.
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.
| |||||
|
Last edited on
|
|||||
| Fransje (245) | |
Functions can only have one return value, so statements like this: return chargeP, totalChargeP; are invalid.
| |
|
|
|
| Zereo (428) | ||||
|
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
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.
This returns istream in, but also changes the vector since the parameter is a reference. | ||||
|
Last edited on
|
||||
| SamuelAdams (322) | |||||
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.
Output should look like
| |||||
|
Last edited on
|
|||||
| Zereo (428) | |||
|
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
| |||
|
Last edited on
|
|||
| hombakazij (14) | |
|
@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 | |
|
|
|
| Zereo (428) | |
|
@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! | |
|
|
|
| hombakazij (14) | |||||||
|
@Zereo thanks a lot. Can you tell me? if I have cin two variables in func1 e.g
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
so that
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
|
|||||||