TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
// if no height entered, display "No Height was entered". ?????
{
// calculate avg height of students.-done
// Teacher will enter height in inches for students one by one.-done
// when no more height, using -1 to stop data entry-done
// Display total height, number of students and average height-done
class
Program
{
static
void
Main(
string
[] args)
{
int
height;
int
totalHeight = 0;
int
numStudents = 0;
//counter variable needed
double
avgHeight;
//get average of height
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);
}
}
}
Attachment:
Lab4Q3DCogdell.zip
Reply
Answers (
2
)
How retrieve data from one data table by using another data
The webclient Uploadfile Methods Hangs