When I run the check on my greedy program, all outputs pass the test except for the negative value. At first I used the code as it is below (negative value asks for input again). This gives me the error: "expected output, not a prompt for input"

When I change the it to only reject 0 but not negative numbers (by changing while(moneyRet <= 0) to while(moneyRet == 0) it gives another error: "expected prompt for input, not exit code of 0".

My code:

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

int main(void){
    float moneyRet;
    int cents;
    int coins[4] = {25, 10, 5, 1};
    int coinsNeeded = 0;
    int tempCoinsNeeded = 0;
    int i;

    printf("How much change needs to be returned: ");
        moneyRet = GetFloat();
    }while(moneyRet <= 0);

    cents = roundf(moneyRet * 100);

    for(i = 0; i < 4; i = i + 1)
        tempCoinsNeeded = floorf(cents / coins[i]);
        coinsNeeded = coinsNeeded + tempCoinsNeeded;
        cents = cents - (tempCoinsNeeded * coins[i]);

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

Part of your problem is understanding exactly what the errors mean. In this case, "Expected output" means that check50 is expecting the program to write out a message. "A prompt for input" means that the computer is waiting for input, not actually printing text that asks for input.

So, with this in mind, consider what the program is doing when an invalid number is entered. Does the program ask for input again or does it just sit there waiting for input?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks. Placed the question for input into the loop as well and it works now. :) Oct 27 '16 at 9:33

You must log in to answer this question.

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