Hot answers tagged

2

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 outside of the while loop, the variables exist and persist inside of all of main. When you declare them inside the while loop's curly braces, they only exist ...


2

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 height var would cease to exist when the code exits the loop, so you couldn't use it later. It's a matter of variable scope. See my answer at Pset4 Recover not ...


2

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 n; before your loop, and just n = get_int(... within. Make sure to not have int n = get_int(... within the loop as well, as you would have two variables of ...


2

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 introduce it into the loop. On the other hand the statement: first_letter = name[0]; I would take it out of the loop, since you know what the index is, you do ...


1

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 mentioned don't exist, they're just not displayed. They are all global vars, not local. If you want to see them, you can type in their names in the area titled "...


1

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) { digits = tempc / 10; count++; } The condition that stops the loop is when tempc is not greater than 0. But, tempc never ...


1

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


1

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

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 braces, the var outptr ceases to exist. If you had declared it before starting the do loop, it would persist through the rest of the program because the scope ...


1

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 their original values, i.e. they won't go negative, while for the former situation allows the velocity to go negative.


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