0
int main(void)
{
// declare number as number of coins
int number = 0;
// declare x as the change owed
float x;
// declare y for converting x into format calculatable
int y;
// prompt the user to enter the change owed
printf("Hi! How much change is owed?\n");
do
{
    x = GetFloat ();

}
while (x < 0);
 // more code
[edit:  additional unrelated code removed for Honor Code compliance ]
}

May someone help me as how to reject a negative input, like -.1 in my case? Thanks.

1

Ummmm... what do you mean? You are already doing exactly that! The only change that you need to make is to move the printf inside the loop.

do
{
// prompt the user to enter the change owed
printf("Hi! How much change is owed?\n");

    x = GetFloat ();
}
while (x < 0);

This will automatically go back and repeat if x is negative.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

3
  • Thank you very much for your help. I just got stuck at this and repeatedly got the message of failure to reject negative value at check50. Thanks a lot again. I will accept this answer and remove it from the unanswered pool. – Alan Jul 4 '15 at 7:37
  • Hmmm, sounds like you might have something different going on. If you still get problems with check50 on this, either repost with the check50 output or better still, ask someone live on one of the several discussion forums. (I hang out on CS50 slack). – Cliff B Jul 4 '15 at 7:39
  • I was experiencing a similar problem to the OP, where inputting a negative number would leave the program hanging. This answer (moving the printf statement into the do/while loop) solved it for me. Thanks Cliff! – deliciousfudge Jan 11 '16 at 0:53

You must log in to answer this question.

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