Hot answers tagged

4

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, but using braces provide readability to your code and avoid such confusions. So you should do this instead of what you mentioned. do { n = GetInt(); ...


3

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 allow the compiler to reach the while statement. Also, C needs braces around a set of instructions you wish to execute inside a conditional statement or loop, if ...


3

You have to explicitly state the comparison each time: you have while (pyramidHeight >= 0 && <= 23); and it should be while(pyramidHeight <= 0 && pyramidHeight >=23); Remember C needs you to explicitly state everything! Also, it's probably good form to initialize your pyramidHeight variable to 0 to start with, just for good ...


3

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


2

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 screen. Also you should take into consideration the width of the ball in your "if" condition as that is the correct way according to the cs50 team's ...


2

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 number of spaces and hash signs, may be this could help. If variable no_spaces holds the number of spaces, then int i; for (i = 0 ; i <= height+1 ; ++i) {...


2

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 inside also goes out of scope as the closing brace of the do is reached. the one while (repeater > 0) sees is actually the one declared outside which ...


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


2

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 run the loop if the user enters any value that isn't between 0-10. You want the while condition to be the values that cause the loop to be repeated.


2

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 times. you're also right about this. we only need to encrypt the message once, don't we? so probably we should loop over it once, just like we did with caesar ...


2

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 between character and "digit" value. Instead of storing the string as your state, you could also store the values, and convert to the string for passing to crypt ...


2

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 return value as an int) if true and returns 0 if false. There's no need to use this function in any special way as was suggested. The problem here appears to be a ...


1

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 = fgetc(dict); c != EOF; c = fgetc(dict)) { if(c != '\n') { int chara = toupper(c) - 'A'; if(character->children[chara] == NULL) { The ...


1

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++) rows is initially set to 0 for the first pass. rows++ only increments when the loop has completed the first pass and returns to the beginning. At that point, ...


1

"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 in main, it will immediately terminate the program). My guess is that this is exactly what you want to do. Understand that a break performs a different ...


1

Yes, It will. By return keyword your program will be out of current function's scope. So it will be out of that loop.


1

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 While will run at least once even if condition is not met. As such you put 0.50 the first Do while(for the 25) runs twice until the condition fails and then the ...


1

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, so it no longer exists. You need to declare a local variable that you can use as an index for the key. You could redeclare x, but that would be a bad practice, ...


1

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.


1

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 than 23 AT THE SAME TIME? ;-) If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)


1

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 (x == y){ // compares a char and a string y is a pointer to a string, not a character. You are attempting to compare a char to a pointer. There are two ...


1

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) { setColor(final, "RED"); setLabel(final, "Sorry, you lost!"); } else setLabel(final, "You win! You cleared all bricks!"); setLocation(final, (WIDTH - ...


1

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 true, do nothing until it is false and then continue to the next line of code." In fact, if you entered a negative number, it should just sit there forever, ...


1

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 again... and the new page redirect again... and the new. You understand. In the config file you find a script that redirects the user to the login page. Check ...


1

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 hash_quant = counter + 1; ... } Even further, you don't need to store the quantity counter + 1 in an extra variable. You could simply use it as is, in any ...


1

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 is calculated. As a result, you get "undefined behavior," which in C could mean throwing an error, retrieving a random piece of information from memory, ...


1

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; } } What if cursor->word is NOT equal to new_node->word? cursor will never become cursor->next and thus it's never becoming NULL, right?


1

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 length(in most cases), say any positive integer. Now when you compare this with the condition, i > strlen(k) at first time, then it is actually 0 > positive ...


1

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 name as the original variable (i.e., ball) within the body of the if statement. This variable shadows the original one and goes out of scope as soon as you exit ...


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