Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

izaan khalid
izaan khalid
549 Points

can 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
Steven Parker
220,937 Points

Yes, 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?

Steven Parker
Steven Parker
220,937 Points

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

izaan khalid
izaan khalid
549 Points

i'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
Fredrik Rönnehag
2,342 Points

You 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 ;)
´´´