0

I am a complete newbie to CS50 and programming. I entered in my basic code and ran the program and CS50check and it was all good. The I tried many times to include this do while loop, reviewed notes, walkthrough's, etc. and I just don't get it or see it.

Here is the revised code but it still doesn't work after many variations. i just don't see the problem.

#include <cs50.h> 
#include <stdio.h> 
int main(void) 
{ 
    do 
    { 
        printf("How long was your shower in minutes "); 
        int minutes = get_int(); 
        int bottles = minutes * 12; 
        printf("You used %i bottles of water for your shower\n", bottles); 
    } 
    while (minutes > 1); 
}
1

The do while loop tells the program to do these things in order:

  1. Do something - The code you put between the curly braces {statement(s)} will run at least once.
  2. The while condition between the parenthesis (condition) will then test to see if the condition is true. In your case the condition is true while minutes < 1.
  3. If the condition is true the loop will return to step one and perform the statement(s) again. If it is false it will break out of the loop and carry on to the next line.

What you want to do is get the number of minutes from the user and then check the input, while the input is invalid you want to get the number of minutes from the user again.

So you may have to ask the user for input a number of times depending on their response. This will be the part of your code contained in the while loop because you want to keep getting user input and checking it until it is valid.

| improve this answer | |
  • I want the user to input the number of minutes for a shower. The value has to be a positive number. It prompts a retry if its a letter value but the I still can't get the loop to prompt retry after a negative value is entered. So as I understand it my code should look like this: #include <cs50.h> #include <stdio.h> int main(void) { do { printf("How long was your shower in minutes "); int minutes = get_int(); int bottles = minutes * 12; printf("You used %i bottles of water for your shower\n", bottles); } while (minutes > 1); } I don't see the problem – Warren Muzak May 17 '17 at 22:53
  • Im sorry, i seemed to have messed up this post too. – Warren Muzak May 17 '17 at 23:43
  • If you look at your loop it will attempt to calculate the number of bottles and print how much bottles were used. You don’t want this inside your loop because it will perform this every time the loop executes, including those times where the user gives invalid input. If you want to ask the user something different on the second iteration of the loop like “retry” if the user input is invalid you can use an ‘if’ statement to accomplish this. I will leave it to you to work it how to implement that. – Ryan McCauley May 18 '17 at 10:23
  • Also if you indent code you write on these forums with at least four spaces it will be formatted as code and look a lot cleaner for people to read. – Ryan McCauley May 18 '17 at 10:24
  • Thank you for the help but I just can't connect the dots. I think I need to review everything including what ( and { mean to understand how to use this properly. I am enjoying this and I want to "see" what everyone else does – Warren Muzak May 18 '17 at 18:43

You must log in to answer this question.

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