Hot answers tagged

4 votes

Do do/while loops only take one line?

The syntax for do-while loop goes like this do { //do something here } while(test_condition); It is better to use braces {} every time you use loops. Although loops work even without their use, ...
user avatar
  • 3,316
3 votes

Do do/while loops only take one line?

This error is because of the missing braces '{}'. Languages like python compile the code based on the indenting. However, C requires braces around do-while to compile correctly. Your code doesn't ...
user avatar
  • 85
3 votes
Accepted

What's wrong with my do while loop syntax?

You have to explicitly state the comparison each time: you have while (pyramidHeight >= 0 && <= 23); and it should be while(pyramidHeight <= 0 && pyramidHeight >=23); ...
user avatar
  • 1,317
3 votes

How to Convert Integers to Spaces/Hashes?

If you use printf(" ") and printf("#") they will print one after another, when you need to go to the next line, just printf("\n")
user avatar
2 votes

pset4 Lives Unintentional infinite loop

An alternative method to do the same would be to avoid deletion and addition of new objects altogether. You can simply reduce the lives by one and set the location of the ball to the center of the ...
user avatar
  • 161
2 votes
Accepted

How to Convert Integers to Spaces/Hashes?

There are many ways to solve this problem(mario.c). I don't want to spoil your work by providing an exact answer, but for your question how you translate this "number" that I can printf into an actual ...
user avatar
  • 3,316
2 votes
Accepted

Why is my do..while loop executing only once?

you're defining repeater twice, once outside of the do..while loop and the other one is inside. the one defined inside shadows the one defined outside in the scope of the body of do. the one defined ...
user avatar
  • 17.4k
2 votes
Accepted

Infinite looping in hacker 1 - water

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, ...
user avatar
2 votes

Infinite looping in hacker 1 - water

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, ...
user avatar
  • 1,677
2 votes

What's wrong with my do while loop syntax?

while (pyramidHeight >= 0 && <= 23); As you've seen, that syntax doesn't work. You need to include the variable in both conditions. An example while (value < 0 || value > 10) will ...
user avatar
  • 18.4k
2 votes
Accepted

Pset2 vigenere how to combine key loop with message loop

I created a loop for the key as follows I can see you did not do anything wrong with that. I know I can't make the key loop inside the message loop since it will run key loop * message loop ...
user avatar
  • 17.4k
2 votes
Accepted

Trying to Increment Characters in a String

One way would be to interpret characters 'a' to 'z' as digits in a number with base 26, start at "aaaa" (representing 0), and then adding 1 to the last string. Add or subtract 'a' for conversion ...
user avatar
  • 20.7k
2 votes
Accepted

pset2 caesar, argv[1]

It's always the problems that hide in plain sight that get us! ;-) isdigit() is working exactly as expected. If a character is a digit, it returns a non-zero (not necessarily 1, try printing the ...
user avatar
  • 63.5k
1 vote
Accepted

In Check50 I get a Segmentation Fault, and trying myself gives me an endless loop

I don't understand why you are getting to check, or appear to be getting an infinite loop. When I ran your posted code, it immediately generated a seg fault. The problem is exposed here: for (int c =...
user avatar
  • 63.5k
1 vote
Accepted

Mario pyramid loop arithmetic question

Your math is correct, but your understanding of the variables in the for loop isn't. On the first run, rows = 0, not 1. Look at the for loop setup code: for (rows = 0; rows < height; rows++) ...
user avatar
  • 63.5k
1 vote
Accepted

Will the loop break in case the condition returns some value to the caller method in C?

"Will the loop break?" Technically speaking, no, it won't break. By using a return true statement, the entire function will immediately terminate and return a value of true to the calling code (or if ...
user avatar
  • 63.5k
1 vote

Will the loop break in case the condition returns some value to the caller method in C?

Yes, It will. By return keyword your program will be out of current function's scope. So it will be out of that loop.
user avatar
  • 158
1 vote

PS1 greedy question regarding formatting of do/while conditions

Do while behaves slightly different than while. You managed to find the exact difference :). Do while executes the code inside once and then checks your condition. That means in all cases the Do ...
user avatar
1 vote
Accepted

Vigenere - how to apply local variable in key to secret message

Your thoughts about why you're getting the error are on track. As declared in your code, the variable x only exists in the for loop where it is declared. Outside of the for loop, it is out of scope, ...
user avatar
  • 63.5k
1 vote

Understanding continue statement in while loop in (pset6)

Ok well... that was quite the oversight... realized immediately after posting the question. if dprintf is successfull continue never gets called. which is what should happen.
user avatar
  • 159
1 vote
Accepted

do while loop not looping

This loop will run exactly once every time. Look at your test. It has to be true for the loop to execute more than once, but that will never happen. What number can be less than zero AND greater ...
user avatar
  • 63.5k
1 vote

Comparing chars

Your problem is much more basic and simple. Look at these lines: char x; // a char variable char *y; // a pointer to a string y = "a"; // loads a string into memory at y if ...
user avatar
  • 63.5k
1 vote
Accepted

Breakout.c: GLabel Looping

You could call the initScoreboard() function to create the new GLabel, like this: GLabel final = initScoreboard(window); setFont(final, "SansSerif-40"); setColor(final, "GREEN"); if (bricks) { ...
user avatar
  • 18.4k
1 vote
Accepted

my while loop is not working

You have a couple of issues in this code that are mostly about structure. First, the semicolon after the while condition serves as a statement terminator and basically means "While this condition is ...
user avatar
  • 63.5k
1 vote

This webpage has a redirect loop [pset7]

Look in your config.php file (it is in "includes" directory). 'Loop redirect' means that your site redirect the user to another (maybe the same, but still a redirect) page, and the new page redirect ...
user avatar
1 vote
Accepted

Mario counter and output problem

First of all, notice that inside a for loop you can use other variables as well, not only counter. E.g.: for(counter = 1; counter <= height; counter++) { int row_number = counter; int ...
user avatar
  • 1,140
1 vote
Accepted

Loops in greedy.c give unusual outputs

This is a good example of what happens when you don't initialize local variables. If the input is .25, your second while loop never executes and dimes will not be initialized at the time that coins ...
user avatar
  • 2,136
1 vote

Infinite loop in speller.c check function

while (cursor is not NULL) //here is where the infinite loop begins { if( cursor->word = new_node->word) //this is using strcmp { return true make cursor = cursor->next; } } ...
user avatar
  • 17.4k
1 vote
Accepted

For loop doesn't start

In your code, the test condition for the for loop should go like this for (int i = 0; i < strlen(k); i++) because when you will pass the string to the function, then it will be of non-zero ...
user avatar
  • 3,316
1 vote
Accepted

pset4 Lives Unintentional infinite loop

The key solution to this is to reuse the variable that's named ball and declared in main() (before the loop). What you're basically doing here is that you're creating a new variable with the same ...
user avatar
  • 17.4k

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