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 trial

Carl Klein
54 PointsHow do I repeat a loop after creating an error message prompt (that terminates the program)?
I've created a power factor calculation with a limit of 10. If I input 11 it terminates and prints out the error message I created.
Essentially, I'm trying to then go back to the top of the loop after the error message prompt.
For instance, if I enter 4, it will calculate 4 via my power factor instruction. If I enter 11, it terminates and will state: "You are wrong, try again." At this point I would like to create a new prompt but starting back at the top of the loop so I can continue as long as I stay within the 1-10 parameters.
Ideas anyone?
3 Answers

Patrick Cooney
12,216 PointsI think the best way to do this is, as you mentioned, to switch to a while
loop. it should be
while (x > 11 || x < 1){
//ask for a number and spit out error
}
if (x >= 1 || x <= 10){
//your logic code here
}
give that a try.
Edit: fixed the if statement code

Patrick Cooney
12,216 PointsI assume you wrote this as a class or a function? If so you can have an external class call it from within its own set of loops to provide this control. That is if I'm understanding correctly what you're asking.

Carl Klein
54 PointsI used an if/else if statement with an embedded for loop (I think). I probably should have used a while loop but wasn't sure how to restart the code after the error prompt message I created terminates the code. I can get the error message prompt if I enter 11 or higher (as I should) but can't get it to go back to the top to start the code over again. I'm trying to get it to keep asking me to enter the right numbers between 1 and 10 until it gets the right one.
For example:
{ int x; int y = 11; int z;
NSLog(@"Enter a number between 1 and 10");
scanf("%i", &x);
{
if (x < y)
{
NSLog(@"x x^2 x^3");
for (z = 1; z <= x; z++)
NSLog(@"%d %d %d", z, z*z, z*z*z);
}
else if (x > y)
NSLog(@"You are wrong, try again!");
}
return 0;
}
Carl Klein
54 PointsCarl Klein
54 PointsPatrick,
Your fix worked! I did have to remove the else in the else if though. :) Once I took out the else, it worked perfectly!
Thank you for your help!
Patrick Cooney
12,216 PointsPatrick Cooney
12,216 PointsSorry! Brain fart. I swear I knew else ifs only work when you have an original if statement!