0

Right now I'm just playing about and trying to add various things to Water, just to get a hang of them. It's probably to over-engineered to get a pass or anything, but oh well.

I'm trying to add a loop to Water, so that if an integer less then or equal to 0 is given, it will give a pithy little remark and let the user input another number. enter image description here

Right now, if I remove the loop parts, everything works fine. I tried it with a do-while loop at first, and that kinda worked, except the program kept repeating "Bottles X, Water Y" over and over and I had to close my browser window to get it to stop.

Also, this is over-engineered so if anyone has any hints about what I could be doing to make things like this shorter in future, it would be appreciated. This is what it looks like without the loop parts.

enter image description here

2 Answers 2

1

You'd need a loop only if you want to re-query the user. If minutes does not change within the loop, the result will always be the same.

Also, while (minutes < 0, minutes == 0); is an empty loop, the ; is its body. And minutes < 0, minutes == 0 is the same as minutes == 0. The comma opererator evaluates its left side, ignores the result, and evaluates to the value of its right side. Did you mean while (minutes < 0 || minutes == 0), or even simpler while (minutes <= 0)?

7
  • This helped a bit, and I did indeed mean to use || (or just <). However, I do want to re-query the user if minutes is anything but a positive integer. Using your advice I've got the program functioning, but when anything less then 0 in given, the program repeats whatever comment is set to print endlessly until I ctrl+c to end the program. Oct 16, 2017 at 13:21
  • A do..while might be a good idea (basically a while loop run at least once), or you'd have to add the additional input in the invalid paths, like if (minutes <= 0) { printf("Invalid input (must be positive!). How many minutes, again?"); minutes = get_int(); } else { printf("Minutes = .... The insults directed at the misbehaving user would always go inside the loop.
    – Blauelf
    Oct 16, 2017 at 13:30
  • Do..While loop works better, but I need some way for the program to do the insult just once, then return to the start of the program. As it is now, I have something that functions, but if say, a 0 is inputted, the terminal endlessly puts back "Gross" until I ctrl+c the program to end. Oct 16, 2017 at 13:47
  • I've figured out how to get back to start using goto and jump_point, which doesn't work perfectly but does do most of what I wanted. So I'll mark this as answered and perhaps come back to it when I learn a little more. Thanks for the help. Oct 16, 2017 at 14:18
  • Just re-read the problem specification. Looks like you're supposed to print Minutes: before the get_int(), or at least that's how they do it. Maybe the checker is flexible enough and just wants to read the number.
    – Blauelf
    Oct 16, 2017 at 14:27
0

As an aside, and if it helps anyone else out, I figured out how to properly implement a do..while loop.

enter image description here

I haven't figured out how to get to the stage where it responds with an insult then goes back to start, but I'm far further along then when I started. Thanks again go to Blauelf.

1
  • If you want an insult, it could be like int minutes = get_int("Minutes: "); while (minutes <= 0) { minutes = get_int("Bakamono. Enter a positive number for the minutes: "); }
    – Blauelf
    Oct 17, 2017 at 7:41

You must log in to answer this question.

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