A real answer is further down
Take note to how I formatted your code according to CS50's style guide. Well formatted code is important if you expect others (or even yourself at a later date) to read your code. I also took myself the liberty to rename your variables to something more descriptive, because descriptive variable names is also important.
How is bottles
getting the value of minutes
?
First off, to begin figuring out the answer to that question, you'll have to follow the order of execution, that is, what code gets executed in which order.
So, we begin in main() where getMinute() is called and the result of that is stored in minutes
, henceforth execution takes place in getMinute() where a variable called minutes
is declared, the program then forces the user to enter a value less than or equal to zero. That value is then returned to main()
Now that we are back in main(), bottleCalc() is then called whose return value is stored in bottles
. Execution now takes point in bottleCalc() where (a different) int bottles
is is declared and also initialized. At this point bottles
get returned to main().
Back again in main(), A printf() call is executed, where the value of bottles
gets substituted for the %i
.
In conclusion, b
never gets the value of i
. As far as I can tell it must be a coincidence. This should also answer your question "how does it work?".
Update:
- With
int b = b * 192 / 16
I presume you actually mean int b = * 192 / 16
since b
doesn't actually exist in the scope of getBottles(), that should get rid of uninitialized variable 'b'
.
- For the last error, when you say
int m = getMinute()
you're basically saying to the compiler "hey I want to store the result of getMinute() in this variable", but then of course you're actually not using it. So to fix the error you will want to somehow use the variable m
in you getBottles() function.
For that to happen you will want to provide an argument to getBottles() int bottles = bottleCalc(m)
, so your bottleCalc implementation will then take int minutes
as a parameter (remember the prototype will have to match this.).
Feel free to message me again if you have any additional concerns!