1

I'm working on PSET1's Water problem. Here's my code:

#include <stdio.h>

int main(void)
{
int m, b;
printf("minutes: ");
scanf("%d", &m);
b = m * 12;
printf("bottles: %d\n", b);
}

Check50 returns the following:

:) water.c exists
:) water.c compiles
:) 1 minute equals 12 bottles
:) 2 minutes equals 24 bottles
:) 5 minutes equals 60 bottles
:) 10 minutes equals 120 bottles
:( rejects "foo" minutes
   \ expected prompt for input, not exit code of 0
:( rejects "" minutes
   \ expected output, not a prompt for input
:( rejects "123abc" minutes
   \ expected prompt for input, not exit code of 0

I've tried everything that I can think of, but I can't seem to crack it. Does anyone have any ideas? Thanks!

3

One option would be to simply use the GetInt(); function in the cs50.h library since it already reprompts the user for input if they give bogus values.

Or implement the feature yourself and use a while loop and continue prompting the user until they give a valid integer.

I should also refer you to this forum post: Check if input is integer

3
  • 1
    Thanks, I realized that it was failing when given the "foo" value, which led me to think about switching to GetInt. It passed the test now! Aug 1 '16 at 0:25
  • 1
    You're welcome. I was actually surprised that you made the decision to use scanf() in such an early pset. It's nothing impossible, but it is a harder brahman to tackle what with all the error checking and dynamic memory allocation that GetInt() does for you. Aug 1 '16 at 0:34
  • 1
    Stephen, be sure to accept this answer so that the forum knows it's no longer an unanswered question! Aug 1 '16 at 0:59

You must log in to answer this question.

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