Home > Software design >  Unbreakable loop
Unbreakable loop

Time:11-16

I am trying to break a loop by only inputing a white space but everytime I do this it keeps looping idk why is that. Is it probably by a logic problem or what is it because id what is going on/

Console.WriteLine("Please create a file with .doc .txt .pxt etc");
            string fileName = Console.ReadLine();
            writer1 = new StreamWriter($"{fileName}");
            while (true)
            {
                while (choiceParsered == false && choiceRange == false) 
                {

                    Console.WriteLine("1) write a file");
                    Console.WriteLine(" 2. Copy a file  ");
                    Console.WriteLine(" 3. Exit ");

                    choose = Console.ReadLine();
                    choiceParsered = int.TryParse(choose, out num);

                }
                if (choose == "1")
                {
                    string writing = "m";


                    while (true)
                    {
                       
                            Console.WriteLine("What do you want to drive on the file? write a blank space to finish");
                            writing = Console.ReadLine();
                            writer1.WriteLine(writing);
                        
                        if (writing == " " || writing == "" )
                        {
                            break;
                        }
                        
                    }

                }

CodePudding user response:

I assume you mean that it doesn't break out of the first while(true) loop. In that case it is because the break you have only breaks out of the second while(true) loop. You'll need to either use a goto statement or some other logic. I think also you would want to do writing == null instead of writing == "".

CodePudding user response:

The problem is caused by this line

choiceParsered = int.TryParse(choose, out num);

Here you get true if the user types a valid input. Then, when you type "1" the variable choose is assigned and the code enters the second loop.
If you type a space or press enter the second loop exits without problems.
But then you never reenter the first loop because the variable choiceParsered is true and, thus, the code goes to test again choose and this is still "1" so the code enters again the second loop and never ends

To fix you need to move the test for the choices inside the first loop AND set the choiceParsered again to false before exiting the input loop.

while (choiceParsered == false && choiceRange == false) 
{

    Console.WriteLine(" 1. write a file");
    Console.WriteLine(" 2. Copy a file  ");
    Console.WriteLine(" 3. Exit ");

    choose = Console.ReadLine();
    choiceParsered = int.TryParse(choose, out num);
    if(choiceParsered)
    {
         if (choose == "1")
         {
             string writing = "m";
             while (true)
             {
                 Console.WriteLine("What do you want to drive on the file? write a blank space to finish");
                 writing = Console.ReadLine();
                 writer1.WriteLine(writing);

                 if (writing == " " || writing == "")
                 {
                     choiceParsered = false;
                     break;
                 }
             }
        }
    }

But there is still the problem of the outer infinite loop. This will never ends if you cannot put a break to terminate it or set a condition to end if

Console.WriteLine("Please create a file with .doc .txt .pxt etc");
string fileName = Console.ReadLine();
writer1 = new StreamWriter($"{fileName}");
bool exitProgram = false;
while (!exitProgram)
{
    ....
   
    if (choose == "3")
       exitProgram = true;
}

Also you never close the StreamWriter. This is a very serious bug that leaves the file open and without a proper flush to end the writing.
But you can have the using statement to close the file for you, so replace the line that opens the file with

using writer1 = new StreamWriter($"{fileName}");
  • Related