In: Computer Science
In C#
When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’.
Existing Code:
using System;
public class Student
{
public int credit;
public String firstname, lastname, gender, residency, edate;
public void input()
{
Console.WriteLine("\nWelcome to the Continental University Registration System!");
Console.WriteLine("\nEnter data about a student");
Console.Write("First Name: ");
firstname = Console.ReadLine();
Console.Write("Last Name: ");
lastname = Console.ReadLine();
Console.Write("Gender (M/F): ");
gender = Console.ReadLine();
Console.Write("Residency (I/O): ");
residency = Console.ReadLine();
Console.Write("Credits Taking: ");
credit = Convert.ToInt32(Console.ReadLine());
Console.Write("Entrance Date: ");
edate = Console.ReadLine();
}
}
Code:
using System;
public class Student
{
public int credit;
public String firstname, lastname, gender, residency, edate;
public void input()
{
Console.WriteLine("\nWelcome to the Continental University
Registration System!");
Console.WriteLine("\nEnter data about a student");
Console.Write("First Name: ");
firstname = Console.ReadLine();
Console.Write("Last Name: ");
lastname = Console.ReadLine();
do
{
Console.Write("Gender (M/F): ");
gender = Console.ReadLine();
}while(!(String.Equals(gender, "M") || String.Equals(gender,
"F")));
do
{
Console.Write("Residency (I/O): ");
residency = Console.ReadLine();
}while(!(String.Equals(residency, "I") || String.Equals(residency,
"O")));
Console.Write("Credits Taking: ");
credit = Convert.ToInt32(Console.ReadLine());
Console.Write("Entrance Date: ");
edate = Console.ReadLine();
}
}
Screenshot: