Question: Now, using your code from Chapter 7 Case Study 1 , modify your program so every data entry statement uses a TryParse ( ) method

Now, using your code from Chapter 7 Case Study 1, modify your program so every data entry statement uses a TryParse() method to ensure that each piece of data is the correct type.
I used the code below and still failed the test for Task #01: All data entry statements use a TryParse() method. I was wondering if someone could make sure TryParse() method is in every place it should be.
using System;
using static System.Console;
using System.Globalization;
class GreenvilleRevenue
{
static void Main()
{
// Variables
const int ENTRANCE_FEE =25;
const int MIN_CONTESTANTS =0;
const int MAX_CONTESTANTS =30;
int numThisYear;
int numLastYear;
int revenue;
string[] names = new string[MAX_CONTESTANTS];
char[] talents = new char[MAX_CONTESTANTS];
char[] talentCodes ={'S','D','M','O'};
string[] talentCodesStrings ={ "Singing", "Dancing", "Musical instrument", "Other" };
int[] counts ={0,0,0,0}; ;
// Get Data and process data
numLastYear = GetContestantNumber("last", MIN_CONTESTANTS, MAX_CONTESTANTS);
numThisYear = GetContestantNumber("this", MIN_CONTESTANTS, MAX_CONTESTANTS);
revenue = numThisYear * ENTRANCE_FEE;
WriteLine("Last year's competition had {0} contestants, and this year's has {1} contestants",
numLastYear, numThisYear);
WriteLine("Revenue expected this year is {0}", revenue.ToString("C", CultureInfo.GetCultureInfo("en-US")));
DisplayRelationship(numThisYear, numLastYear);
GetContestantData(numThisYear, names, talents, talentCodes, talentCodesStrings, counts);
GetLists(numThisYear, talentCodes, talentCodesStrings, names, talents, counts);
}
//Contestant Number
public static int GetContestantNumber(string when, int min, int max){
int num;
Write("Enter number of contestants {0} year >>", when);
while(!int.TryParse(ReadLine(), out num)){
Write("Invalid Input. Enter number of contestants {0} year >>", when);
}
while (num < min || num > max)
{
WriteLine("Number must be between {0} and {1}", min, max);
Write("Enter number of contestants {0} year >>", when);
while(!int.TryParse(ReadLine(), out num)){
Write("Invalid Input. Enter number of contestants {0} year >>", when);
}
}
return num;
}
public static void DisplayRelationship(int numThisYear, int numLastYear){
if (numThisYear >2* numLastYear)
WriteLine("The competition is more than twice as big this year!");
else
if (numThisYear > numLastYear)
WriteLine("The competition is bigger than ever!");
else
if (numThisYear < numLastYear)
WriteLine("A tighter race this year! Come out and cast your vote!");
}
public static void GetContestantData(int numThisYear, string[] names, char[] talents, char[] talentCodes, string[] talentCodesStrings, int[] counts){
int x =0;
bool isValid;
while (x < numThisYear)
{
Write("Enter contestant name >>");
names[x]= ReadLine();
WriteLine("Talent codes are:");
for (int y =0; y < talentCodes.Length; ++y)
WriteLine("{0}{1}", talentCodes[y], talentCodesStrings[y]);
Write(" Enter talent code >>");
while(!char.TryParse(ReadLine(),out talents[x])){
Write("Invalid Input.Enter talent code >>");
}
isValid = false;
while (!isValid)
{
for (int z =0; z < talentCodes.Length; ++z)
{
if (talents[x]== talentCodes[z])
{
isValid = true;
++counts[z];
}
}
if (!isValid)
{
WriteLine("{0} is not a valid code", talents[x]);
Write(" Enter talent code >>");
while(!char.TryParse(ReadLine(),out talents[x])){
Write("Invalid Input.Enter talent code >>");
}
}
}
++x;
}
}
public static void GetLists(int numThisYear, char[] talentCodes, string[] talentCodesStr

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!