I'm doing water.c of pset1 and wanted to use functions to practice and understand it more. After getting so many errors, I finally got it to work but I don't understand why and how it worked. How is 'b' getting the value of 'i' and printing it? What am I missing here? Here is the code:

int getMinute(void);
int bottleCalc(void);

int main(void)
    int minutes = getMinute();
    int bottles = bottleCalc();
    printf ("You spend about %i bottles of water everytime you take a shower. Save your water :)\n",bottles);

int getMinute(void)
    int minutes;
        printf("How many minutes does it take you to take a shower?\n");
        minutes = GetInt();
    while (minutes <= 0);

    return minutes;

int bottleCalc(void)
    int bottles = bottles * 192 / 16;

    return bottles;


Edit: Just got a reply on reddit. I wrote and ran it locally using Atom. But I just ran it through CS50 IDE and it is giving me error. I don't know why and how it is working on my computer.

Here is a screenshot of it: Imgur

Im so confused.


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.

  1. 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()

  2. 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().

  3. 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?".


  1. 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'.
  2. 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!

  • Hello Kleiven. Thank you for your time. If bottles is not getting the value of minutes, how is bottlesCalc calculating the user input I entered? As I said in the edit I made, it is giving me an error in cs50 ide about "unused variable i" and "variable 'b' is uninitialized". I have no idea how it managed to run on my computer. – Dulguun Nov 13 '16 at 16:55
  • See my updated answer. – kluvin Nov 13 '16 at 17:15
  • Oh and also for the part where you could run it on your own PC, this is probably because of the -werror flag, which tells the compiler to treat all warnings as errors. – kluvin Nov 13 '16 at 17:58

Read more about functions and got it working. Checked on CS50 IDE and success! Thanks :)

#include <cs50.h>
#include <stdio.h>

int getMinute();
int bottleCalc();

int main(void)

    int minutes = getMinute();
    int bottles = bottleCalc(minutes);
    printf ("You spend about %i bottles of water everytime you take a shower. Save your water :)\n", bottles);

int getMinute (void)

    int input;
    do {
          printf ("How many minutes does it take you to take a shower?\n");
          input = GetInt();
    while (input <= 0);
    return input;

int bottleCalc (int min)

    int calc = min * 192 / 16;
    return calc;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .