Home > Enterprise >  C# Cannot find a way to loop back to one if statement
C# Cannot find a way to loop back to one if statement

Time:03-09

how would i go back to option 1 when option 2 is completed(Regitration is completed then go to the login part of the code).

if (options == 1)
            {
                Console.WriteLine("Please enter username and password");
                Console.Write("Username: ");
                string username = Console.ReadLine();
                Console.Write("Password: ");
                string password = Console.ReadLine();

                if (Login.login(username, password)) Console.WriteLine(username   " Succesfully logged in");
                else if (!Login.login(username, password)) Console.WriteLine("Login failed");
            }
            // Register user
            else if (options == 2)
            {
                Console.WriteLine("Please create a userbame and password");
                Console.Write("Create Userame: ");
                string newUsername = Console.ReadLine();
                Console.Write("Create a Password: ");
                string newPassword = Console.ReadLine();
                string toWrite = newUsername   "|"   newPassword   "\n";
                File.AppendAllText(fileName, toWrite);

                   
                


            }

CodePudding user response:

try this

do
    {

    if (options == 1)
        {

            ..... your code
    

        options = 0;
        }
        // Register user
        else if (options == 2)
        {
            ... your code
    

       options = 1;
        }
        
} while (options > 0);

CodePudding user response:

Here is a slightly different version:

while (options < 3)
            {


                if (options == 1)
                {
                    Console.Clear();
                    Console.WriteLine("Please enter username and password");
                    Console.Write("Username: ");
                    string username = Console.ReadLine();
                    Console.Write("Password: ");
                    string password = Console.ReadLine();

                    if (Login.login(username, password))
                    {
                        Console.WriteLine(username   " Succesfully logged in");
                        options = 3;
                    }
                    else if (!Login.login(username, password))
                    {
                        Console.WriteLine("Login failed");
                        options = 3;
                    }
                }
                // Register user
                else if (options == 2)
                {
                    Console.WriteLine("Please create a userbame and password");
                    Console.Write("Create Userame: ");
                    string newUsername = Console.ReadLine();
                    Console.Write("Create a Password: ");
                    string newPassword = Console.ReadLine();
                    string toWrite = newUsername   "|"   newPassword   "\n";
                    //File.AppendAllText(fileName, toWrite);
                    options = options - 1;




                }

            }
  •  Tags:  
  • c#
  • Related