Hot answers tagged

3 votes
Accepted

Can I declare a variable *within* a do-while loop?

Yes and no. Yes, it is possible, and perfectly permissible to declare height inside the do/while loop, but there's a problem. If you only wanted to use height inside the loop, it's fine. But, the ...
  • 64.6k
2 votes
Accepted

Pset4 Recover not working

It's a scope issue. You have declared outptr inside of a pair of curly braces - the curly braces that contain the code for the if statement's code block. As soon as the code exits that set of curly ...
  • 64.6k
2 votes
Accepted

Pset3 Breakout: Question on variable scope in while loop

You're on the right track, but there's an additional issue going on. Think of scope this way: a variable only exists within the curly braces that immediately surround it. So, when you declare them ...
  • 64.6k
2 votes

do while mistake , please give right code

Your problem is a scope issue. The way you declare n, it ceases to exist after you leave the loop's body. Which means when you enter the while(n<=0), it no longer is. To prevent this, place a int ...
  • 20.7k
2 votes
Accepted

Initials: Not printing second initial

The only problem is that your second IF condition is outside the For loop: if (name[i] == ' ') { printf("%c\n", name[i + 1]); so that the blank space is never reached, the solution would be to ...
  • 5,181
1 vote

Can I declare a variable *within* a do-while loop?

Yes. you can declare a variable inside any loop(includes do while loop.lol).But remember the scope of that variable is restricted inside the loop. That is you can't use that variable outside the loop.
1 vote
Accepted

pset5 speller Hash table not appearing in the debug50 stack/heap

Look very carefully at all of the areas on the right side of the screen. Did you notice that the area that shows the vars and their content is called local variables? It isn't that the vars you ...
  • 64.6k
1 vote
Accepted

CS50 Credit - again a scope problem

Nope, not a scope problem. It's an infinite loop problem. This is because the code has an infinite loop. Look carefully at the code: //Calculate the sum of digits while (tempc > 0) ...
  • 64.6k
1 vote
Accepted

Scope problem - not print the test results

You are saying return 0 after your first loop which terminates the program. return 0 is used to terminate the program at at that line and will not continue to the next lines..
  • 711
1 vote

Declaring a variable in loop or outside

You can do what is shown in both of your examples, but you shouldn't. The first is a very bad practice, while the second depends on how it is used. In the first case, it's called creating a shadow ...
  • 64.6k
1 vote

Pset3 Breakout: Question on variable scope in while loop

Whoops, sorry guys. Just realized that for the latter situation, when the code goes through the If statement then loops back to the top of the while statement, the velocities get assigned back to ...

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