Hot answers tagged

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


2

Like I edited in my own problem and comment, I solved it. #include <stdio.h> #include <cs50.h> int main(void) { int bottle = 12; int num_bottles; printf("minutes: \n"); int minutes = GetInt(); if (minutes > 0) { num_bottles = bottle * minutes; printf("bottles: %d\n", num_bottles); } else { num_bottles = bottle * ...


2

I like that you don't want to use CS50's tools and use tools that you are going to have available everywhere outside of CS50. I was reviewing your code. I tried to solve it with a fflush(stdin) line, since I had that working for me on a Windows program in the past, but it did not work in this case. However, I found this very useful post and built an answer ...


2

Yes, don't use scanf. You have a library that helps you avoid using scanf. The reason is probably related to the fact that scanf still keeps the string you input in its buffer and it won't empty it, nor will it do anything useful with it. scanf is a function that will only work as expected when it reads what you expected (which means it's not really good ...


1

Not really a good solution. What happens if someone enters bad input twice? You need a solution that will keep prompting for valid input until it is entered. Time to discover what a while loop is. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


1

what you are seeing is a side effect of how get_int works. If you have something like: int num; do { printf("Number: "); num = get_int(); } while (num < 0); And run it, you will see this behavior: Number: -1 Number: foo Retry 2 But if you change your do while to this: do { num = get_int("Number: "); } while num < 0; Then you will ...


1

You should review the class material and google references for printf. Specifically, to print the value of a variable, you need a place marker in the print format string and the var name in the printf statement. Example: ... int count = get_int("Give me an integer: "); printf("You said %i \n", count); Note the placeholder %i that says 'put the integer ...


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 ...


1

The do while loop tells the program to do these things in order: Do something - The code you put between the curly braces {statement(s)} will run at least once. 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. If the condition is true the loop ...


1

Your code is actually doing what you told it to, but you told it to print out the number of bottles before it exited the for-loop. A simple fix is to put the printf outside the for-loop. If you still have issues/questions, comment below.


1

You lack the main function, all console programs must have a main function: int main(void) { some code } The error is surely because the compiler thinks that you are going to define or declare a function, the do-while loop is the most appropriate for this case. EDIT All statements must end in semicolons (;) int minutes = get_int() should be: int ...


1

A real answer is further down Take note to how I formatted your code according to CS50's style guide. Well formatted code is important if you expect others (or even yourself at a later date) to read your code. I also took myself the liberty to rename your variables to something more descriptive, because descriptive variable names is also important. How is ...


1

Try taking away the semicolon after int main (void). From there you will have to go through and fix your other errors, but those should be easier to fix. Just run make and then go through each error one by one. I hope this helps! If this answers your question, please click the check to accept:)


1

in 2017 the GetInt() function is depreciated water.c:8:9: warning: 'GetInt' is deprecated [-Wdeprecated-declarations] now use get_int(); It works for:- Positive Integer Negative Integer Zero otherwise user is prompted to "retry". note: view the Walkthrough video.


1

%f is a format specifier wrong for an integer, the right thing would be %i, or %d. Since NumberOfBottles is an integer, the variable is unused, hence the second error, but in this case will be corrected when we use the correct formatting for an integer. EDIT: The most obvious things are we overlook, excuse my cluelessness. The error persists because we ...


Only top voted, non community-wiki answers of a minimum length are eligible