0

Hei, I'm currently stuck on this Greedy.c project. it says that my while loop is empty, which it's not? Can someone give me a hint towards, what is wrong with my code? Thank you.

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

int main(void)
{

    // Declare variables
    int amount = 0;
    float dollars;

    // Get amount of dollars owed
    do{
        printf("How much money is owed: \n");
        dollars = GetFloat();
    } while (dollars < 0); {
        printf("Input is invalid!\n");
    }
  // Convert to cents
    float convert_to_cents = 100 * dollars;
    int cents = round(convert_to_cents);
    printf("%d cents\n", cents);

    // While loops, check for coins
    while (cents > 24); {
        amount + 1;
        cents = cents % 25;
    }
    while (cents > 9); {
        amount++;
    }
    while (cents > 4); {
        amount++;
    }
    while (cents > 0); {
        amount++;
    }

    // Print total amount of coins used

    printf("The total amount of coins used is: %d\n",amount);
}

Edit: So i've fixed the while loops, and it's looking to work as it should.

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

int main(void)
{

    // Declare variables
    int amount = 0;
    float dollars;

    // Get amount of dollars owed
    do{
        printf("How much change is owed: \n");
        dollars = GetFloat();
    } while (dollars < 0);

    if (dollars <= 0 || ""){
        printf("Input is invalid\n");
    }


  // Convert to cents
    float convert_to_cents = 100 * dollars;
    int cents = round(convert_to_cents);
    printf("%d cents\n", cents);

    // While loops, checking for coins

    while (cents > 24) {
        amount++;
        cents = cents - 25;
    }
    while (cents > 9) {
        amount++;
        cents = cents - 10;
    }
    while (cents > 4) {
        amount++;
        cents = cents - 5;
    }
    while (cents > 0) {
        amount++;
        cents = cents - 1;
    }

    // Print total amount of coins used

    printf("The total amount of coins used is: %d\n",amount);
}
1

The do-while loop shouldn't have curly braces after the while part. Remove them along with the printf statement. Also your while loops shouldn't have a semicolon after the while part. That's why the compiler complains they are empty. Examples:

do
{
    // your code here
} while (condition);
while (condition)
{
    // your code here
}
| improve this answer | |
  • I think I've gotten it right this time :) – JakobH Aug 1 '15 at 10:05
  • Since you have the do-while loop to check for input < 0 you don't need the if for the input again. Also to pass the check50 your outputs should be exactly as described, not as it feels better to you. – ChrisG Aug 1 '15 at 10:48

You must log in to answer this question.

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