Delisha Cogdell

Delisha Cogdell

  • NA
  • 7
  • 1.5k

How to display a specific message when wrong input entered?

Feb 1 2018 2:56 PM
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.
  1. // if no height entered, display "No Height was entered". ?????  
  2. // calculate avg height of students.-done  
  3. // Teacher will enter height in inches for students one by one.-done  
  4. // when no more height, using -1 to stop data entry-done  
  5. // Display total height, number of students and average height-done  
  6. class Program  
  7. {  
  8. static void Main(string[] args)  
  9. {  
  10. int height;  
  11. int totalHeight = 0;  
  12. int numStudents = 0; //counter variable needed  
  13. double avgHeight; //get average of height  
  14. Console.WriteLine("Enter the height of the student in inches or -1 to stop.");  
  15. height = int.Parse(Console.ReadLine());  
  16. while (height != -1)  
  17. {  
  18. totalHeight = totalHeight + height;  
  19. numStudents = numStudents + 1;  
  20. Console.WriteLine("Enter the height of the student in inches or -1 to stop.");  
  21. height = int.Parse(Console.ReadLine());  
  22. }  
  23. Console.WriteLine("Total height: {0}", totalHeight);  
  24. Console.WriteLine("Total number of students: {0}", numStudents);  
  25. if (numStudents > 0)  
  26. {  
  27. avgHeight = totalHeight / numStudents;  
  28. }  
  29. else  
  30. {  
  31. avgHeight = 0;  
  32. }  
  33. Console.WriteLine("Average Height for students: {0}.", avgHeight);  
  34. }  
  35. }  
  36. }  

Attachment: Lab4Q3DCogdell.zip

Answers (2)