0

My code

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

int main(void)
{
float sum;
int sumInt;
// Check user input and convert it to cents
do
{
    //eprintf("about to prompt user for a number\n");
    sum = get_float("Hello! How much sum is owed? ");
    sumInt = (int) round(sum * 100);
    printf("%i\n", sumInt);
}
while (sum < 0);

//Calculate how many quorters in sum
int moduloQuarters, amountOfQuarters;
moduloQuarters = sumInt % 25;
// If moduloQuarters is zero, divide input on 25 and show amount of coins
//eprintf("about to find quarters in number\n");
if (moduloQuarters == 0)
{
    amountOfQuarters = sumInt / 25;
    //printf("%i\n", amountOfQuarters);
}
// If moduloQuarters is greater than zero, divide sum by 25 and round a value
else
{
    amountOfQuarters = round(sumInt / 25);
    //printf("%i\n", amountOfQuarters);
}
//Calculate how many dimes in sum
int moduloDimes, amountOfDimes;
moduloDimes = moduloQuarters % 10;
// If moduloDimes is less than 10, do nothing
//eprintf("about to find amount of dimes\n");
if (moduloDimes < 10)
{
    //do nothing;
}
// If moduloDimes is greater than 10, divide sum by 10 and round a value
else
{
    amountOfDimes = round(moduloDimes / 10);
    //printf("%i\n", amountOfDimes + amountOfQuarters);
}
//Calculate how many nickels in sum
int moduloNickels, amountOfNickels;
moduloNickels = moduloDimes % 5;
//eprintf("about to find nickels in number: \n");
// If moduloNickels is less than 5, do nothing
if (moduloNickels < 5)
{
    //do nothing;
}
// If modulo is greater than 5, divide sum by 5 and round a value
else
{
    amountOfNickels = round(moduloNickels / 5);
    //printf("%i\n", amountOfQuarters + amountOfDimes + amountOfNickels);
}
//Calculate how many pennies in sum
int moduloPennies, amountOfPennies;
moduloPennies = moduloNickels % 1;
//eprintf("about to find pennies in number: \n");
// If moduloPennies is less than 5, do nothing
if (moduloPennies < 1)
{
    //do nothing;
}
// If modulo is greater than 5, divide sum by 1 and round a value and print total sum
else
{
    amountOfPennies = round(moduloPennies / 1);
    int totalSum = amountOfQuarters + amountOfDimes + amountOfNickels + amountOfPennies;
    printf("%i\n", totalSum);
}
}

CS50 check provides me this output

:) cash exists
:) cash compiles
:( input of 0.41 yields output of 4
expected "4\n", not "41\n"
:) input of 0.01 yields output of 1
:( input of 0.15 yields output of 2
expected "2\n", not "15\n"
:( input of 1.6 yields output of 7
expected "7\n", not "160\n"
:( input of 23 yields output of 92
expected "92\n", not "2300\n"
:( input of 4.2 yields output of 18
expected "18\n", not "420\n"
:) rejects a negative input like -.1
:) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""

I read many similar questions here and on Reddit as well. Please, help me to find out the problem.

This line

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

Solves this problem

:) input of 0.01 yields output of 1

But I know that it's not right to do like so.

0

Since sumInt / 25 is an integer division, round has no effect (other than converting the number to floating point).

What concerns me are the //do nothing; lines. This means the variables are never initialised, and might contain anything that was at that memory location before.

Later, tool valgrind is introduced, one of its functions is finding usage of uninitialised variables. It would probably complain at a few places when called like valgrind ./cash (it's called much like debug50).

I would do things a bit differently, keep only one variable with the remaining cash, and one for the coins so far, and change their content.

4
  • Thanks for the input. It caused an error when I tried to add logic to if statement. Inability to initialize a variable when boolean operation in if statement is false. That's why I left all my if statements empty.
    – Mikhail R
    Oct 30 '18 at 11:03
  • Not sure what you did there, but you shouldn't have these if statements in the first place.
    – Blauelf
    Oct 30 '18 at 12:03
  • I think you talking about this cs50.stackexchange.com/questions/30420/… But I really want to use modulo based solution...
    – Mikhail R
    Oct 30 '18 at 12:29
  • I prefer not using that many variables in both approaches. And the modulo based approach needs exactly one condition, one to handle invalid input. No further if required. It doesn't hurt if you add 0 coins of a type.
    – Blauelf
    Oct 30 '18 at 12:41
0

The error messages by check50 are caused by this line:

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

Here you print the input, converted to cents.

However, the specifications told you very clearly that your program should only print the total number of coins needed. And that is what check50 is checking, and what result in those :( lines ...

4
  • This is not a problem. This line was left for the reason. It has no impact on the final check from cs50.
    – Mikhail R
    Oct 31 '18 at 7:06
  • It does. For instance, for test case "0.41" check50 complains that your output is "41" in stead of "4". Your program starts by converting "0.41" to [41], and then it prints "41". And that is exactly what check50 is complaining about! And the same goes for the error messages on "0.15", "1.6, "23" and "4.2" ... Oct 31 '18 at 7:21
  • This is an output of cs50 check after. :) cash exists :) cash compiles :( input of 0.41 yields output of 4 expected "4\n", not "" :( input of 0.01 yields output of 1 expected "1\n", not "" :( input of 0.15 yields output of 2 expected "2\n", not "" :( input of 1.6 yields output of 7 expected "7\n", not "" :( input of 23 yields output of 92 expected "92\n", not "" :( input of 4.2 yields output of 18 expected "18\n", not "" :) rejects a negative input like -.1 :) rejects a non-numeric input of "foo" :) rejects a non-numeric input of ""
    – Mikhail R
    Oct 31 '18 at 7:33
  • Ah, that is something entirely different. If you want us to help you understand this error message and repair your program, you should post that as a separate question. Oct 31 '18 at 7:41
0

Yes, my solution was not elegant. Too much code duplication.

I searched for a mathematical solution and here is my code now.

[code removed per Academic Honesty guidelines]

4
  • That's not an answer to your question, it does not search for fixes of your existing but broken solution, it's mostly a completely different solution, not using / and %.
    – Blauelf
    Oct 30 '18 at 16:04
  • Please see an update and comment.
    – Mikhail R
    Oct 31 '18 at 13:42
  • You shouldn't use an answer to clarify your original question (or to show us your solution code, as that is not allowed per the Academic Honesty guidelines).
    – curiouskiwi
    Oct 31 '18 at 22:36
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review Nov 6 '18 at 3:52

You must log in to answer this question.

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