Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

password wrong still sends you to the next page

I started coding in c# 2 days ago for school so I’m very new to it, I ran into a problem with my code and I don’t know how to fix it. Once the person trying to login has 3 failed attempts it indeed shows the text that tells the user that they failed 3 times and that the app will close itself, but instead of closing the app sends them to the next "page" with the text menu on it. I have tried multiple things and asked friends in school but no one really knows what’s wrong with the code.

If someone who is more experienced knows I’d really appreciatie it, if there is anything else wrong with my code or if I made a mistake please let me know, I’d like to learn as much as possible!

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my code:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;

namespace Gimpies
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // strings inlognaam + password
            string inlognaam = "inkoop";
            string password = "Gimpies_Inkoop";

            // attempts counter
            for (int i = 0; i < 3; i++)
            {
                // strings gebruikersnaam + wachtwoord
                Console.WriteLine("U heeft nog " + (3 - i) + " pogingen ");
                Console.WriteLine("Voor gebruikersnaam in:");
                string gebruikersnaam = Console.ReadLine();
                Console.WriteLine("Voer wachtwoord in:");
                string wachtwoord = Console.ReadLine();


                // If else login / password
                if (inlognaam == gebruikersnaam && password == wachtwoord)
                {                                      
                    Console.WriteLine("Succes");    
                    Console.WriteLine("Druk een knop in om door tegaan");
                    Console.ReadLine();
                    goto AfterLoop;
                    
                }
                else
                    Console.WriteLine("Gebruikersnaam of wachtwoord is onjuist probeer opnieuw.");
                                                             
            }

            Console.WriteLine("U heeft geen poginen meer, de applicatie sluit af na 5 seconden");
            Thread.Sleep(5000);

        AfterLoop:
            Console.Clear();
            Console.WriteLine("Menu");
            Console.ReadKey();
        }
    }
}

we have tried adding in the break; command instead of Thread.sleep but that ruined the loop ofcourse, and we have also messed around with the ReadKey but that didnt’t help aswell

>Solution :

As stated above, the fault is the goto, since the AfterLoop is still going to execute even if you dont call it

I recommend avoid using goto at all and just make a new method AfterLoop();

However, if you still wanna use it for some reason, add Environment.Exit(0); right after the thread.sleep to close the app after the delay.

Cheers!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading