I am a newbie in C# Programming. I have no prior experience. I am taking a online introduction class in Visual C# How to Program. I don't know how to make my program display a "No Height was Entered." when the end-user types a letter instead of an number when the program asks the question-Console.WriteLine("Enter the height of the student in inches or -1 to stop.");
Here is the question I'm trying to solve: Write a program that calculates the average height of a class of students. The teacher will enter the height of each student in inches one by one. When the teacher has no more height to enter, she will enter -1 to stop data entry. Display the total height, the number of students in the class and the average height of the students. If no height was entered, display “No height was entered.”.
Here is the work I've done in MS Visual Studio 2017. Please keep in mind this is the beginning of an Introduction course so Advanced concepts will go over my head write now. The test is to keep this code as basic as possible. so far I have learned about While loops, Conditional operators, logical operators, Negation, counters, and increment/decrement operators.
I would greatly appreciate any help you can provide.
-
- {
-
-
-
- class Program
- {
- static void Main(string[] args)
- {
- int height;
- int totalHeight = 0;
- int numStudents = 0;
- double avgHeight;
- Console.WriteLine("Enter the height of the student in inches or -1 to stop.");
- height = int.Parse(Console.ReadLine());
- while (height != -1)
- {
- totalHeight = totalHeight + height;
- numStudents = numStudents + 1;
- Console.WriteLine("Enter the height of the student in inches or -1 to stop.");
- height = int.Parse(Console.ReadLine());
- }
- Console.WriteLine("Total height: {0}", totalHeight);
- Console.WriteLine("Total number of students: {0}", numStudents);
- if (numStudents > 0)
- {
- avgHeight = totalHeight / numStudents;
- }
- else
- {
- avgHeight = 0;
- }
- Console.WriteLine("Average Height for students: {0}.", avgHeight);
- }
- }
- }