Answer to Question #311506 in C# for Dave

Question #311506

Modify the following program to check for reasonable inputs (or reject un-reasonable ones). For example, a negative GPA or test score, or a GPA of 10 or a test score of more than 100.


using System;


namespace Admit

{

  class Program

  {

    public static void Main(string[] args)

    {

      float avg;

      int score;

      Console.WriteLine("Grade point average: ");

      avg = float.Parse(Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture); //decimal mark is "."

      Console.WriteLine("Admission test score: ");

      score = int.Parse(Console.ReadLine());

      if ((avg >= 3) && (score >= 60) || (avg < 3) && (score >= 80))

        Console.Write("Accept");

      else

        Console.Write("Reject");

      Console.ReadKey(true);

    }

  }

}



1
Expert's answer
2022-03-14T13:04:22-0400
using System;


namespace Admit
{
    class Program
    {
        public static void Main(string[] args)
        {
            float avg = float.MinValue;
            int score = int.MinValue;


            while (avg < 0 || avg >= 10)
            {
                Console.WriteLine("Grade point average: ");
                avg = float.Parse(Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture); //decimal mark is "."
                if(avg < 0 || avg >= 10)
                    Console.WriteLine("Incorrect input");
            }
            while(score < 0 || score > 100)
            {
                Console.WriteLine("Admission test score: ");
                score = int.Parse(Console.ReadLine());
                if (score < 0 || score > 100)
                    Console.WriteLine("Incorrect input");
            }


            if ((avg >= 3) && (score >= 60) || (avg < 3) && (score >= 80))
                Console.Write("Accept");
            else
                Console.Write("Reject");
            Console.ReadKey(true);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS