Program ends after user input is invalid

Abdulhamid

How would make the program state that the users input is invalid and then close after enter is pressed? I have the first error that states that the value the user inputted is wrong, giving them another try, but if the user types the same/invalid number it will repeat the last thing. How can I make it limit to one more try and then if the user doesn't give a valid entry it would right and error? The code is below:

        string First;
        string Last;
        First = "Cristiano";
        Last = " Ronaldo";
        Console.Write("Please enter student name <First Last>: ");
        Console.WriteLine(First + Last);

        Console.WriteLine(" ");

        Console.WriteLine("*************NOTE**********************************************");
        Console.WriteLine("*** Be sure to include decimal point for scores.            ***");
        Console.WriteLine("***     !!!All score should range from 0.00 to 100.00 !!    ***");
        Console.WriteLine("***                                                         ***");
        Console.WriteLine("*** For example : 80.50                                     ***");
        Console.WriteLine("***************************************************************");

        Console.WriteLine(" ");

        double Exam_1 = -1;
        double Exam_2;
        double Exam_3;
        double Assignment_1;
        double Assignment_2;

        Console.Write("Please enter score for Exam 1 <Example: 100.0>: ");
        Exam_1 = Convert.ToDouble(Console.ReadLine());

        while (Exam_1 < 0.0 || Exam_1 > 100.0)
        {
            Console.Write("Exam score cannot be less than 0. or greater than 100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
            Exam_1 = Convert.ToDouble(Console.ReadLine());
        }

        Console.Write("Please enter score for Exam 2 <Example: 0.0>: ");
        Exam_2 = Convert.ToDouble(Console.ReadLine());

        while (Exam_2 < 0.0 || Exam_2 > 100.0)
        {
            Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 2 <Example: 95.0>:");
            Exam_2 = Convert.ToDouble(Console.ReadLine());
        }

        Console.Write("Please enter score for Exam 3 <Example: 60.8>: ");
        Exam_3 = Convert.ToDouble(Console.ReadLine());

        while (Exam_3 < 0.0 || Exam_3 > 100.0)
        {
            Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 3 <Example: 95.0>:");
            Exam_3 = Convert.ToDouble(Console.ReadLine());
        }

        Console.WriteLine(" ");

        Console.Write("Please enter score for Assignment 1 <Example: 100.0>: ");
        Assignment_1 = Convert.ToDouble(Console.ReadLine());

        while (Assignment_1 < 0.0 || Exam_2 > 100.0)
        {
            Console.Write("Assignment score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 1 <Example: 95.0>:");
            Assignment_1 = Convert.ToDouble(Console.ReadLine());
        }

        Console.Write("Please enter score for Assignment 2 <Example: 23.46>: ");
        Assignment_2 = Convert.ToDouble(Console.ReadLine());

        while (Assignment_2 < 0.0 || Assignment_2 > 100.0)
        {
            Console.Write("Assignment score can not be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 2 <Example: 56.0>: ");
            Assignment_2 = Convert.ToDouble(Console.ReadLine());
        }

        Console.WriteLine(" ");

        Console.WriteLine(" -------------- OUTPUT ---------------");

        Console.WriteLine(" ");

        Console.Write("Student: ");
        Console.WriteLine(First + Last);

        Console.WriteLine(" ");



















        Console.Write("Press any key to continue . . . ");
        Console.ReadLine();
    }
}

}

James Dev

Try this code

    static void Main(string[] args)
    {
        string First;
        string Last;
        First = "Cristiano";
        Last = " Ronaldo";
        Console.Write("Please enter student name <First Last>: ");
        Console.WriteLine(First + Last);

        Console.WriteLine(" ");

        Console.WriteLine("*************NOTE**********************************************");
        Console.WriteLine("*** Be sure to include decimal point for scores.            ***");
        Console.WriteLine("***     !!!All score should range from 0.00 to 100.00 !!    ***");
        Console.WriteLine("***                                                         ***");
        Console.WriteLine("*** For example : 80.50                                     ***");
        Console.WriteLine("***************************************************************");

        Console.WriteLine(" ");

        double Exam_1 = -1;
        double Exam_2;
        double Exam_3;
        double Assignment_1;
        double Assignment_2;

        Console.Write("Please enter score for Exam 1 <Example: 100.0>: ");
        Exam_1 = Convert.ToDouble(Console.ReadLine());
        var exitProgram = false;
        var errorCount = 0;

        while (Exam_1 < 0.0 || Exam_1 > 100.0)
        {
            Console.Write("Exam score cannot be less than 0. or greater than 100.0. Please re-enter the score for Exam 1 <Example: 95.0>:");
            Exam_1 = Convert.ToDouble(Console.ReadLine());
            ++errorCount;
            ErrorCount(errorCount);
        }

        Console.Write("Please enter score for Exam 2 <Example: 0.0>: ");
        Exam_2 = Convert.ToDouble(Console.ReadLine());
        errorCount = 0;
        while (Exam_2 < 0.0 || Exam_2 > 100.0)
        {
            Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 2 <Example: 95.0>:");
            Exam_2 = Convert.ToDouble(Console.ReadLine());
            ++errorCount;
            ErrorCount(errorCount);
        }

        Console.Write("Please enter score for Exam 3 <Example: 60.8>: ");
        Exam_3 = Convert.ToDouble(Console.ReadLine());
        errorCount = 0;
        while (Exam_3 < 0.0 || Exam_3 > 100.0)
        {
            Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 3 <Example: 95.0>:");
            Exam_3 = Convert.ToDouble(Console.ReadLine());
            ++errorCount;
            ErrorCount(errorCount);
        }

        Console.WriteLine(" ");

        Console.Write("Please enter score for Assignment 1 <Example: 100.0>: ");
        Assignment_1 = Convert.ToDouble(Console.ReadLine());
        errorCount = 0;
        while (Assignment_1 < 0.0 || Exam_2 > 100.0)
        {
            Console.Write("Assignment score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 1 <Example: 95.0>:");
            Assignment_1 = Convert.ToDouble(Console.ReadLine());
            ++errorCount;
            ErrorCount(errorCount);
        }

        Console.Write("Please enter score for Assignment 2 <Example: 23.46>: ");
        Assignment_2 = Convert.ToDouble(Console.ReadLine());
        errorCount = 0;
        while (Assignment_2 < 0.0 || Assignment_2 > 100.0)
        {
            Console.Write("Assignment score can not be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 2 <Example: 56.0>: ");
            Assignment_2 = Convert.ToDouble(Console.ReadLine());
            ++errorCount;
            ErrorCount(errorCount);
        }

        Console.WriteLine(" ");

        Console.WriteLine(" -------------- OUTPUT ---------------");

        Console.WriteLine(" ");

        Console.Write("Student: ");
        Console.WriteLine(First + Last);

        Console.WriteLine(" ");
        Console.Write("Press any key to continue . . . ");
        Console.ReadLine();
    }

    public static void ErrorCount(int errorCount)
    {
        if (errorCount > 0)
        {
            Console.Write("Error count too much ! . . . ");
            Console.Write("Press any key to exit . . . ");
            Console.ReadKey();
            Environment.Exit(0);
        }

    }

Bear in mind it doesn't detect if the user enters an alpha numeric number or special characters but you get the idea.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How should I handle invalid user input?

From Java

How can I make sure the User can input again after entering something invalid?

From Dev

C How to return screen to normal after curses program ends

From Dev

Python: wait for user input, and if no input after 10 minutes, continue with program

From Dev

how to re-display user prompt after invalid input is given in c#?

From Dev

Catch an exception for invalid user input in swift

From Dev

Do memory leaks have any effect after the program ends?

From Dev

User input to repeat program in Java

From Dev

Program is hanging after input

From Dev

Program blocks after input

From Dev

Keeping matplotlib figures open after program ends

From Dev

Asks user to input floating-point values. If invalid inputs more than twice, quit the program using python

From Dev

How can I make sure the User can input again after entering something invalid?

From Dev

Re-prompt user after invalid input in Java

From Dev

Start a program after waiting for user input

From Dev

Network reply after program ends

From Dev

Processing user input in a python program

From Dev

Ending a python program with user input

From Dev

Program is hanging after input

From Dev

Java program not reading user input

From Dev

Keeping matplotlib figures open after program ends

From Dev

How to Make it so that the User ends this program?

From Dev

Python program just ends after a loop

From Dev

matrices program asking for user input

From Dev

What is the proper way to prevent a program from exiting after a user enters wrong input in Java?

From Dev

I am using node.js and stdin, but the program ends before the user can input their binary to be evaluated

From Dev

Taking in a user input to load a program

From Dev

Modification of Bubblesort program with user input

From Dev

How can i terminate the program when the user inputs an invalid input?

Related Related

  1. 1

    How should I handle invalid user input?

  2. 2

    How can I make sure the User can input again after entering something invalid?

  3. 3

    C How to return screen to normal after curses program ends

  4. 4

    Python: wait for user input, and if no input after 10 minutes, continue with program

  5. 5

    how to re-display user prompt after invalid input is given in c#?

  6. 6

    Catch an exception for invalid user input in swift

  7. 7

    Do memory leaks have any effect after the program ends?

  8. 8

    User input to repeat program in Java

  9. 9

    Program is hanging after input

  10. 10

    Program blocks after input

  11. 11

    Keeping matplotlib figures open after program ends

  12. 12

    Asks user to input floating-point values. If invalid inputs more than twice, quit the program using python

  13. 13

    How can I make sure the User can input again after entering something invalid?

  14. 14

    Re-prompt user after invalid input in Java

  15. 15

    Start a program after waiting for user input

  16. 16

    Network reply after program ends

  17. 17

    Processing user input in a python program

  18. 18

    Ending a python program with user input

  19. 19

    Program is hanging after input

  20. 20

    Java program not reading user input

  21. 21

    Keeping matplotlib figures open after program ends

  22. 22

    How to Make it so that the User ends this program?

  23. 23

    Python program just ends after a loop

  24. 24

    matrices program asking for user input

  25. 25

    What is the proper way to prevent a program from exiting after a user enters wrong input in Java?

  26. 26

    I am using node.js and stdin, but the program ends before the user can input their binary to be evaluated

  27. 27

    Taking in a user input to load a program

  28. 28

    Modification of Bubblesort program with user input

  29. 29

    How can i terminate the program when the user inputs an invalid input?

HotTag

Archive