Hot answers tagged

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 ...
  • 17.5k
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, ...
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, ...
  • 1,677
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 ...
  • 17.5k
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 ...
  • 66.2k
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 ...
  • 20.8k
1 vote
Accepted

Check function pset5 speller

Let's take the easy one first. Think about how check_buffer is populated. The for loop copies every char from word. The problem is that this does NOT copy the end of string marker, \0 to the end of ...
  • 66.2k
1 vote

pset1 cash less, problem using for loops

Normal practice is to use a for loop when the number of passes is well defined, either by a hard coded value or by a value contained in a variable. When the number of passes is based on a condition or ...
  • 66.2k
1 vote
Accepted

pset1 cash less, problem using for loops

You need a semicolon immediately after each for loop. Otherwise, all the statements until the next semicolon are considered the body of that for loop. The compiler will complain if you put a semicolon ...
  • 165
1 vote

Pset Cash CS 50 - need help

I can see a few bugs here - the one causing the infinite loop is your while loop. The condition is while (cents >= 25) but the value of cents never changes/decreases, so this condition will be ...
  • 11
1 vote

Key looping in Vigenere

From Final Steps in the spec: Remember also that every time you encipher a character, you need to move to the next letter of k, the keyword (and wrap around to the beginning of the keyword if ...
1 vote
Accepted

pset4 recover single image

A loop is definitely the way to go, not if statements. Now, have you looked at the size of the file(s) produced? I suspect that the first file created is much larger than it should be - a ...
  • 66.2k
1 vote
Accepted

PSet 1, Trying to add a loop to Water

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, ...
  • 20.8k
1 vote
Accepted

PSET1 Credit infinite loop when checking length

The ^ operator is the Bitwise XOR operator in C as well as many languages that derive their syntax from C (including C++, Python and Java). I assume you want the power operator. There is no power ...
1 vote
Accepted

Pset 4: Stuck in a loop again (all green pixels)

Your problem is with your fseek()s. You should fseek() back to beginning of the current line each time you read all the pixels of this line. You should do this n times for each line. You should ...
  • 7,366
1 vote
Accepted

PSET3 Binary Search Function Infinite Loop

Seems you just misprinted. Change end = middle + 1 to start = middle + 1 and it will work just fine.
  • 1,139
1 vote

Pset1 Greedy; problem with do-while loop

Your while loop does not end up EDIT: (Thanks to SuperNovaCoder for your intervention) The problem is that equality must be achieved ie the conditions must be >= EDIT1: while(cent_amount>=0) ...
  • 5,201
1 vote

Problem in the do..while loop in Mario.c

The declaration of your main function needs to be as so: int main(void) { ...code... } Notice the positions of curly braces relative to the declaration and the lack of semi-colons.
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 ...
  • 66.2k
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.
  • 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 ...
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 =...
  • 66.2k
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++) ...
  • 66.2k
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, ...
  • 66.2k
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.
  • 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 ...
  • 66.2k
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 ...
  • 66.2k
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) { ...
  • 18.5k
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 ...
  • 66.2k
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 ...

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