Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialArmando Torrez
3,238 PointsCompiler Issues
In this lesson, I tried running the code regarding popcorn sizes and their values; however upon running the code, the popcornPrice value gives me the value of 0, initially. When I run through the code line by line using Step-in tool, the value changes to the correct one. Please, help me understand why.
Armando Torrez
3,238 Pointsenum popcornSizes {
kidsPopcorn = 1,
smallPopcorn = 2,
mediumPopcorn = 3,
largePopcorn = 4,
tubPopcorn = 5,
};
float popcornPrice;
int popcornSize = tubPopcorn;
switch (popcornSize) {
case kidsPopcorn:
popcornPrice = 1.5;
break;
case smallPopcorn:
popcornPrice = 3;
break;
case mediumPopcorn:
popcornPrice = 4.25;
break;
case largePopcorn:
popcornPrice = 5.25;
break;
case tubPopcorn:
popcornPrice = 6;
break;
default: NSLog(@"No valid size entered");
break;
}
return 0;
}
popcornPrice = 0 popcornSize = 0
When I use the Step-in tool to go over the lines of code, popcornPrice = 6 popcornSize = 5
As it should. But only after I have gone through each functional line of code.
4 Answers
Martin Wildfeuer
Courses Plus Student 11,071 PointsOh, it seems you have accidentally set a breakpoint. this will stop the app from executing whenever it reaches this point. This is for debugging porposes. Is there a blue rectangle on the line number of this line? If so, you can either drag and drop it out of the editor, remove it in the breakpoint manager or disable breakpoints as a whole.
Martin Wildfeuer
Courses Plus Student 11,071 PointsI am not sure if I understand your question correctly, but this is expected behavior. This code is executed in a serial manner. Only after the switch statement is finished, your variables are set correctly.
float popcornPrice;
int popcornSize = tubPopcorn;
At this point popcornPrice
is 0, the default value when creating a float without assigning a value.
popcornSize
, however, is 5 at this point, not zero.
Now the switch statement is executed, and it goes through every single case statement, unless it matches this case. In case of tubPopcorn
, it is the last case that matches, so the code in the body of this case is executed. Only now the popcornPrice
is assigned a float value of 6.
Not sure what return 0
and the extra }
are, I consider this a copy/paste artefact, otherwise it would not compile.
Hope this helps :)
Armando Torrez
3,238 PointsThe portions of the code do not run until I review them step by step. I'm noticing that there is a green banner over: int popcornSize = kidsPopcorn; saying Thread 1 Breakpoint 2.1
Perhaps that has to do with it?
Armando Torrez
3,238 PointsCorrection:
int popcornSize = tubPopcorn;
Armando Torrez
3,238 PointsOh, yeah. I removed the first breakpoint and now it's working. Thank you!
Martin Wildfeuer
Courses Plus Student 11,071 PointsMartin Wildfeuer
Courses Plus Student 11,071 PointsHey there! Please add your code to the question, it's impossible to go through this otherwise. Thanks!