0

I've developed some code that seems to satisfy what I am being asked to do in the water.c coding project, yet when I go to check it using check50 2016.water water.c it gives me :( water.c compiles. After checking the articles on this forum, I feel that my problem maybe that I am using the do-while technique wrong, but I've tried fixing it and have failed. Here is the code for reference:

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

 int main(void)
 {
     printf("minutes: ");
     int minutes = get_int();

     do
     {
          int i = get_int()
     }
     while(i < 0)

     if (minutes > 0)
     {
         int gallons = (minuets * 1.5);
         int ounces = (gallons * 128);
         int bottles = (gallons / 16);
         printf("bottles: %f\n", bottles);
     }
     else
     {
         printf("Number must be an integer\n");
     }
 }

Any help or advice would be appreciated, I just feel very lost and confused right now. Also, I am terribly sorry if I have coded a huge mess that doesn't make sense or is terribly repetitive, this is my first experience coding and all.

2 Answers 2

2

OK, you are pretty close, just some little syntax errors.

When I try to compile your source code with clang I get the following errors, so let's solve them step by step:

  1. test.c:11:26: error: expected ';' at end of declaration
            int i = get_int()
                             ^
                             ;
    

    You get that error because you forgot to insert a ; at the command, at line 11. Let's fix that and compile again.

  2. Now the first error we get is:

    test.c:13:11: error: use of undeclared identifier 'i'
        while(i < 0)
              ^
    

    That's because you declared the i inside the curly braces, so when your code gets outside the curly braces, i is out of scope, and the compiler tells you it's not declared. What you can do to solve this, is move the declaration of i before the while loop like so:

    int i;
    do
    {
        i = get_int();
    }
    while(i < 0)
    

    Lest's fix that and compile again.

  3. Now we get the following error:

    test.c:14:17: error: expected ';' after do/while statement
        while(i < 0)
                    ^
                    ;
    

    As the compiler tells you, there should be a ; after the do/while statement like so:

    int i;
    do
    {
        i = get_int();
    }
    while(i < 0);
    

    Fix and compile as always.

  4. Now the first error we get is:

    test.c:18:24: error: use of undeclared identifier 'minuets'; did you mean 'minutes'?
            int gallons = (minuets * 1.5);
                           ^~~~~~~
                           minutes
    

    Ahhh. That's a simple misspelling on line 18 (line 17 in your original code). That's easy to fix! Do it, and compile again!

  5. Now we get a warning (that's better, they are still erroneous commands, but less severe, so let's fix this too).

    test.c:21:33: warning: format specifies type 'double' but the argument has type 'int' [-Wformat]
            printf("bottles: %f\n", bottles);
                             ~~     ^~~~~~~
                             %d
    

    You want to print the value of bottles, which is an int, but in printf() you say that you will print a float, and it's confused. So replace the %f with %d to specify that you want to print an int and not a float. Fix and compile!

Now everything works! Hooray!!! Now the hard part, to make sure that your code does what it's supposed to do. I'll leave that to you. ;)

In the future, when you find yourself in similar situations, just read the error messages, from top to bottom, and try to fix them one by one, like we did together. I'm sure you will make it on your own next time!

Good luck and Happy Coding!

1
  • 1
    Thank you so much! This totally fixed the compiling problem I was having earlier. Now that step one and two are complete, it seems that I have a few math problems to fix, but that is something I'm ready to try and fix on my own. Thanks again for all your help, and I hope you have a great day! Jun 8, 2017 at 18:25
0

your while loops are not implemented. they dont have open and close brackets

1
  • I appreciate the help, but even after doing that, I keep coming up with the same error message. Here's what I've done to fix it: on lines 13-16 (the while line) I've made it While (enter) { (enter) i < 0 (and I've done) (i < 0) (enter) }. sorry for the weird formatting, but I don't have enough characters to begin a new line for each command in this comment. Jun 8, 2017 at 2:01

You must log in to answer this question.

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