locked
Alternative to goto. RRS feed

  • Question

  • Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("Would you like to:");
                Console.WriteLine("  1.Read in a data file?");
                Console.WriteLine("\t\t    2.Enter data manually?");
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write("\nSelect option 1 or 2 to proceed: ");
                error: int choice = int.Parse(Console.ReadLine());
                List<int[]> arrays = new List<int[]>(); 

                if (choice == 1)
                {
                    Console.Write("Enter file name and, if necessary, it's location: "); // Enter location of file (if the file is not in the debug file enter the EXACT location of said file).
                    string filepath = Console.ReadLine();
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("String\t\t\tH(1)\tH(2)\tH(3)\tApEn(1)\tApEn(2)\tApEn(3)");
                    System.IO.StreamReader s = new System.IO.StreamReader(filepath);
                    List<string> store = new List<string>();
                    while (!s.EndOfStream)
                    {
                        string currentline = s.ReadLine(); 
                        store.Add(currentline);
                    }
                    s.Close();
                    for (int n = 0; n < store.Count; n++)
                    {
                        int[] bit_string = new int [store[n].Length];
                        for (int j = 0; j < (store[n].Length); ++j)
                        {
                            bit_string[j] = int.Parse(new string(store[n][j],1));
                        }

                        arrays.Add(bit_string);
                        Calc(bit_string);    
                    }
                    Console.ReadLine();
                }
                if (choice == 2)
                {
                    Console.Write("Enter data: ");
                    string a = Console.ReadLine();
                    int[] bit_string = new int[a.Length];

                    for (int n = 0; n < a.Length; n++)
                    {
                        bit_string[n] = int.Parse(new string(a[n], 1));
                    }
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("String\t\t\tH(1)\tH(2)\tH(3)\tApEn(1)\tApEn(2)\tApEn(3)");
                    Calc(bit_string);
                    Console.ReadLine();
                }
                if (choice != 1 && choice != 2)
                {
                    Console.Write("Invalid choice. Please select option 1 or 2: ");
                    goto error;
                }  

    I apologise about the preceding reams of code. In this case the use of goto works extremely well. I would like to replace it though due to the general taboo placed upon the use of goto. Could anyone give me an alternative which would produce the same effect?

               
    Friday, November 23, 2012 8:26 PM

Answers

  • Greetings DAPOS1993.

    The way to replace the goto using the MNC (minimum necessary change) is something like the following.

    // ** Original code. **
    error: int choice = int.Parse(Console.ReadLine());
    
    // etc. etc.
    
    if(choice != 1 && choice != 2)
    {
       goto error;
    }
    
    
    
    
    // ** New code. **
    int choice = 0;
    while(choice != 1 && choice != 2)
    {
       choice = int.Parse(Console.ReadLine());
       
       // etc. etc.
    
    }
    • Proposed as answer by JMCF125 Sunday, November 25, 2012 3:45 PM
    • Marked as answer by DAPOS1993 Tuesday, November 27, 2012 10:36 PM
    Saturday, November 24, 2012 10:56 PM

All replies

  • I'd prefer to check the value first.

    while (2 + 3 == 5)
    {
        string choiceS = Console.ReadLine();
        int choice = 0;
    
        if (int.TryParse(choiceS, out choice) && (choice == 1 || choice ==2))
        {
             // your code...
        }
        else
        {
            // error handling...
            continue;
        }
    }



    João Miguel

    • Proposed as answer by JMCF125 Friday, November 23, 2012 9:38 PM
    Friday, November 23, 2012 8:37 PM
  • I would put your code for user input or reading from file into subroutines, then change your main method to the following:
     
      static void Main(string[] args)
      {
       bool looping = true;
     
       while (looping)
       {
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.Write("Would you like to:");
        Console.WriteLine("  1.Read in a data file?");
        Console.WriteLine("\t\t    2.Enter data manually?");
        Console.ForegroundColor = ConsoleColor.DarkGreen;
        Console.Write("\nSelect option 1 or 2 to proceed: ");
     
        switch (Console.ReadLine())
        {
         case "1":
          ReadFromFile();
          break;
         case "2":
          ReadFromUser();
          break;
         default:
          looping = false;
          break;
        }
     
        Console.WriteLine("Press Enter");
        Console.ReadLine();
       }
      }

    --
    Mike
    • Proposed as answer by JMCF125 Friday, November 23, 2012 9:38 PM
    Friday, November 23, 2012 9:02 PM
  • The rest of my code is working fine and I don't feel much need to change it. I'll play around with your suggestions for sure though. Thanks for the help. But, could anyone give me a way just to replace the goto statement? Keeping everything else the same. I've tried using a conditional loop around the if statements but I'm not too sure what the condition would be. Appreciate the help lads.
    Saturday, November 24, 2012 10:29 AM
  • But, could anyone give me a way just to replace the goto statement? Keeping everything else the same.  
    That's not possible (even if it is, it's not worth it), you want to go to somwhere without goto nor loops? Any alternative would likely be as bad as goto itself. Use our suggestions (mark them as answers and helpful), I can't see a better way to do it.

    João Miguel

    Saturday, November 24, 2012 12:22 PM
  • Creating the two subroutines was a suggestion to make the code look cleaner, as I tend to hate really long "main" methods that cannot be viewed in one screen view. 
     
    You may put your original code into the Switch/Case statements if you wish.  But as JMCF125 said, you cannot restructure your code without restructuring your code.

    --
    Mike
    Saturday, November 24, 2012 1:16 PM
  • [...], you cannot restructure your code without restructuring your code.
    That one was amusing, +1.

    João Miguel

    Saturday, November 24, 2012 6:04 PM
  • "The rest of my code is working fine"

    Not really.  You should always validate UI input as soon as it becomes available.  If you did, a simple break statement  would replace your goto.
    Saturday, November 24, 2012 7:12 PM
  • I enjoy pointing out conflicting requirements to people.  :-)

    --
    Mike
    Saturday, November 24, 2012 7:28 PM
  • Greetings DAPOS1993.

    The way to replace the goto using the MNC (minimum necessary change) is something like the following.

    // ** Original code. **
    error: int choice = int.Parse(Console.ReadLine());
    
    // etc. etc.
    
    if(choice != 1 && choice != 2)
    {
       goto error;
    }
    
    
    
    
    // ** New code. **
    int choice = 0;
    while(choice != 1 && choice != 2)
    {
       choice = int.Parse(Console.ReadLine());
       
       // etc. etc.
    
    }
    • Proposed as answer by JMCF125 Sunday, November 25, 2012 3:45 PM
    • Marked as answer by DAPOS1993 Tuesday, November 27, 2012 10:36 PM
    Saturday, November 24, 2012 10:56 PM
  • The above suggestion was brilliant. Thanks!
    Sunday, November 25, 2012 5:22 PM
  • Mark them as answers please.

    João Miguel

    Sunday, November 25, 2012 6:52 PM