Skip to main content
4 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 ...
Cliff B's user avatar
  • 69k
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 ...
Cliff B's user avatar
  • 69k
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 ...
Blauelf's user avatar
  • 21k
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 ...
MARS's user avatar
  • 5,179
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 ...
Cliff B's user avatar
  • 69k
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) ...
Cliff B's user avatar
  • 69k
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..
Ojou Nii's user avatar
  • 731
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 ...
Cliff B's user avatar
  • 69k
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.
Basil Kuriakose's user avatar

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