-2

I am really new to programming and I just started trying out some problem sets on harvard's CS50. Would appreciate it if anyone can point out to me why my code is wrong.

After I compiled and run the code, there isn't any output.

On another note, can anybody explain to me how "round" works? I don't really get it from man round on the terminal. thanks!


#include <stdio.h>
#include <math.h>
int main(void)
{   
  printf(" O hai! How much change is owed?\n");

  float change;
  change=GetFloat();
  double round(double change); 
  int x= change*100;

  int i=0;

  while(x>25) { 
    x=x-25;
    i++;
    return i;
  }

  while(x>10) { 
    x= x-10;
    i++;
    return i;
  }

  while(x>5) {
    x=x-5;
    i++;
    return i;
  }

  while(x>1) {
    x=x-1;
    i++;
    return i;
  }

  printf("%d\n",i);

}
Turtle
  • 1,369
  • 1
  • 17
  • 32
cornstar94
  • 57
  • 2
  • 11

6 Answers6

1

First thing after you input the dollars by the user you should multiply it by 100 right after that to convert it to cents and using the round function. Use

change = round(change) ;

In your code as change is a floating point value but we need the money in integers to count the number of coins used so we use round to change it to integer and as we cannot count the number of coins used using a floating point value

then: the return i ; statement should not be used in every loop remove it from each loop.

As, if you are using it in each lopp it'll calculate those particular coins and will then exit the program. So remove all of those return i statements and you can write return 0 ; after the printf("%d\n", i); statement !

MikeT
  • 51,415
  • 16
  • 49
  • 68
Ankur
  • 21
  • 5
0

One problem might b the calling of the round function.You call the function like

double value=round(variable);

where value gives you the rounded off number. See this link---

http://fresh2refresh.com/c/c-arithmetic-functions/c-round-function/

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29
0

Your code doesn't work because you are returning i in each of the while loop.

When the line return i executes, the main function returns and the program terminates.

The possible solution is to remove all the return statements inside the loops.

0

The return statement signifies the end of a function. Loops are not functions, so the return statements are not necessary for them.

You are not receiving output because Main itself is a function. So, when "return i" is reached the machine believes the end of the Main function has been reached and ceases to run.

As a result, everything below that first while loop isn't going to run. This is why you are not seeing the printf output.

Later on in programming you will use multiple return statements to signify where a function (or the program itself) should stop running. But in this first assignment you should only have one return statement at the end.

JPG
  • 1
  • 1
0

First I would recommend you to read cs50Style and also add some comments so the code is more legible not just for us, but for you too. Another recommendation is adding some "tests" during the program, for example if you want to know if some part of the program is being executed add some code like:

printf("TEST1\n");   //check 1

By doing this you can check when your program is reaching a certain point or failing in another. (This tests will be deleted before sending or checking the problems, don't forget that).

Now let's get to the main problem. For knowing what a function or any function does, you can visit [reference cs50]: https://reference.cs50.net or writting in your "cs50 appliance" console: man followed by the name of the function in this chase man round.

Basically this function rounds a number to its nearest integer value (2.70 to 3.00).

Regarding the question of why your program doesn't work.

First I would look a way of implementing a do..while loop in the input section:

printf(" O hai! How much change is owed?\n");
float change;
change=GetFloat();

Because if I write down -5.00, what would be the result of that?

Second, it's not neccesary to return the value of i in each iteration.

Third, I see a problem with the symbols, for example: x>25, x>10. What if the remaining coin is 10 if you have written x>10?. It's really easy to fix this problem tho :)

Fourth, if you think about it, you dont really need to write the last part of your code

while(x>1)
{
x=x-1;
i++;

because the coins values will be 1 so you can add the amount of remaing 1 coins to the total (i+x).

And try to take adventage of style using this when writting code

x = x-1;

is equal to

x -= 1;

And lastly I would like to tell you that there is way of resolving this problem without any while(x >=25, x>=10,...), just by doing sums, divisions(think of type int property) and modulo operations.

Moreover I hope that you don't give up in this course, it's challenging but really funny once you get things learned, it's like some kind of snowball.

Good luck!

Drexpp
  • 28
  • 1
  • 6
0

As JPG mentioned, the reason why it isn't giving you any output is because whenever you use the return statement, it immediately tries to exit the currently active function. When that function is main, it means the program itself is going to try to exit.

Your idea of trying to do something with the left over value is on the right track, this is what you will need to do, however you will need to do it a different way. You might be able to use division, but I recommend looking into the mod (%) operator. It'll be a lot cleaner.

Also, there's flow problem with your loop. Nested Loops work their way down through each level. In order to work how you are expecting, each of those loops needs to be on the same level. You will run across nested loops very soon and it's really important to understand the flow of logic.

This is actually the first time I've ever seen this attempted and it's quite interesting to be honest! If you removed the return i's, you would actually get (incorrect) output. There are simpler ways to get the result, but this way will work just fine once you fix the logic errors.