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 trializaan khalid
549 Pointscan you not create a do..while loop and set the condition to "entry = "quit""?
i'm not sure if do..while are a thing in C# but if it is wouldn't it be easier
2 Answers
Steven Parker
231,248 PointsYes, C# does have a "do...while" but I don't know if it would be applicable to the situation you have in mind.
Can you provide the complete code, or a time index and link to a video, so we can take a look?
izaan khalid
549 Pointsi'm not sure if this will work but i was thinking about something like this:
do{
//prompt the user for the minutes excersiced
Console.Write("Enter how many minutes you excerisiced or Type 'quit' to quit: ");
string entry = Console.ReadLine();
int minutes = int.Parse(entry);
runningTotal = runningTotal + minutes;
//Add minutes excersices to the screen
//Display total inutes excersiced to the screen
Console.WriteLine("You've excersiced " + runningTotal + " minutes");
//repeat until the user quits
}while(entry != "quit");
Fredrik Rönnehag
2,342 PointsYou are declaring the string entry inside the loop, so the while statement wont find the entry variable declared. You also parse that same entry string to an int with int minutes = int.Parse(entry);
This will cause an error to the compiler since you write "quit" in the middle of the do-loop. And you can't parse "quit" to an int.
What you can do is doing a try-catch statement, so after entry it will try the parse. If it comes back with an error it will catch the error and continue the code below.
However, the way I would type is just a while(true) and an if statement after the entry = Console.ReadLine(); checking for if(entry == "quit") break;
int runningTotal = 0;
string entry = "";
do
{
//prompt the user for the minutes excersiced
Console.Write("Enter how many minutes you excerisiced or Type 'quit' to quit: ");
entry = Console.ReadLine();
try
{
int minutes = int.Parse(entry);
runningTotal = runningTotal + minutes;
Console.WriteLine("You've excersiced " + runningTotal + " minutes");
}
catch { }
//Add minutes excersices to the screen
//Display total inutes excersiced to the screen
//repeat until the user quits
} while (entry != "quit");
// Using While and IF-statement //
int runningTotal = 0;
string entry = "";
while (true)
{
//prompt the user for the minutes excersiced
Console.Write("Enter how many minutes you excerisiced or Type 'quit' to quit: ");
entry = Console.ReadLine().ToLower(); // Sets the input to lower to avoid the user from typing Quit to cause a crash.
if( entry == "quit")
{
Console.WriteLine("You quit");
break;
}
else
{
int minutes = int.Parse(entry);
runningTotal = runningTotal + minutes;
Console.WriteLine("You've excersiced " + runningTotal + " minutes");
}
// However, this code will still break if the user types any other word than "quit". I will leave it up to you to solve that one ;)
´´´
Steven Parker
231,248 PointsSteven Parker
231,248 PointsNow that I see the code, at first glance it seems that you would not want to parse the input before you test it to see if the user entered "quit". So perhaps a "do....while" is not the best choice for this situation because the test only happens after the code inside runs.
For this situation, an unconditional loop ("
while(true)
") combined with an explicit "break" when "quit" is entered would probably make the most sense.