0

everything is working fine until the condition of remainder <= 5 is met (else if) then it just executes the first part only (if).

#include <stdio.h>
#include <math.h>
#include <cs50.h>
int main(void)

{

    float n;
    do
    {
        // prompt user for change
        n = get_float("change owed: ");
    }
    while(n<0);
    int cents = round(n * 100);
    // perform division
    // computes quotient
    int quotient = cents / 25;

    // computes remainder
    int remainder = cents % 25;

    printf("Quotient = %d\n", quotient);

    printf("Remainder = %d\n", remainder);

    if (remainder >= 10)
    {
        int quotient2 = remainder / 10;
        int remainder2 = remainder % 10;
        printf("Quotient2 = %d\n", quotient2);
        printf("Remainder2 = %d\n", remainder2);
        if (remainder2 >= 5)
        {
            int quotient4 = remainder2 / 5;
            int remainder4 = remainder2 % 5;
            printf("Quotient4 = %d\n", quotient4);
            printf("Remainder4 = %d\n", remainder4);
            int addition, subtraction, multiplication, division,               modulus;
            addition = quotient + quotient4 + remainder4;
            printf("number of coins: %d\n", addition);
        }
    else if (remainder >=5)
    {
        int quotient3 = remainder / 5;
        int remainder3 = remainder % 5;
        printf("Quotient3 = %d\n", quotient3);
        printf("Remainder3 = %d\n", remainder3);
    } 
    else
    {
        printf("%d\n", remainder);
    }
}

}
1
  • problem solved. thanks. one bracket was misplaced. – SELKERDANY Apr 3 '20 at 14:25
0

Your curly brackets don't line up with the indentation of your code. It looks like you misplaced one to the end of your code.

11
  • Hi T, thank you very much for your input. I figured that the problem may be in alignment but I checked all brackets and they seem ok. as for the one at the end, it closes the very first one on the top of the code and no error is reported in it level. – SELKERDANY Apr 2 '20 at 11:19
  • i checked your edits, the thing is, whenever i remove the bracket at the end, the sandbox asks me to return it although i can see it is redundant. – SELKERDANY Apr 2 '20 at 11:25
  • i just tried it again and the program refuses that i delete the last bracket or replace it with the one before last. it insists that a bracket is needed to match the first one (in level 6). don't know what to do. – SELKERDANY Apr 2 '20 at 11:31
  • One of the last two brackets is for the if (remainder >= 10). Does that if statement have a proper close bracket? – TLH Apr 2 '20 at 11:32
  • Ohhhhhhhhhhh, i see what you mean. the bracket for this one is shifted. i will fix and see what happens although the if (remainder >=10) is actually executed. the problem is in else if. – SELKERDANY Apr 2 '20 at 11:40

You must log in to answer this question.

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