Question: This is my program on calculating averages in c#. The program works completly fine, i just am stumped on how to implement error messages if
This is my program on calculating averages in c#. The program works completly fine, i just am stumped on how to implement error messages if the user types anything but what the sentinel value and department codes. I also need an error message for if the user enters an invalid mark. Can someone help me out?
char deptCode; double CompScienceMarkVal = 0.0; double CSAvg; double MathMarkVal = 0.0; double MathAvg; double BusinessMarkVal = 0.0; double BusinessAvg; int CompScienceMarkCount = 0; int MathMarkCount = 0; int BusinessMarkCount = 0; double SumCS = 0.0; double SumM = 0.0; double SumB = 0.0;
//Program asks for Department code input, followed by a mark.
Console.WriteLine("Enter a department code: 'C' or 'c' for Computer Science, 'M' or 'm' for Math, 'B' or 'b' for Business Admin, or enter 'Q' or 'q' to quit =>"); deptCode = Convert.ToChar(Console.ReadLine()); //Parent loop to enter marks for department codes, when the input is not the sentinel value while (char.ToUpper(deptCode) != 'Q') { //Condition of the the Computer Science Department code, which starts the loop while (char.ToUpper(deptCode) == 'C') { //Prompts user to input a mark value, the program then has a running total of each computer science mark into one sum. Console.WriteLine("Enter the Computer Science mark value =>"); CompScienceMarkVal = Convert.ToDouble(Console.ReadLine()); SumCS += CompScienceMarkVal; //Counts how many marks in total for the computer science department CompScienceMarkCount ++;
//Prompts user to input a new department code, or sentinel value to exit the loop Console.WriteLine ("Enter a department code: 'C' or 'c' for Computer Science, 'M' or 'm' for Math, 'B' or 'b' for Business Admin, or enter 'Q' or 'q' to quit =>"); deptCode = Convert.ToChar(Console.ReadLine()); } while (char.ToUpper(deptCode) == 'M') { //Prompts user to input a mark value, the program then has a running total of each computer science mark into one sum. Console.WriteLine("Enter the Math mark value =>"); MathMarkVal = Convert.ToDouble(Console.ReadLine()); SumM += MathMarkVal; //Counts how many marks in total for the math department MathMarkCount ++; //Prompts user to input a new department code, or sentinel value to exit the loop Console.WriteLine ("Enter a department code: 'C' or 'c' for Computer Science, 'M' or 'm' for Math, 'B' or 'b' for Business Admin, or enter 'Q' or 'q' to quit =>"); deptCode = Convert.ToChar(Console.ReadLine()); } while (char.ToUpper(deptCode) == 'B') { //Prompts user to input a mark value, the program then has a running total of each Business Admin mark into one sum. Console.WriteLine("Enter the Business Administration mark value =>"); BusinessMarkVal = Convert.ToDouble(Console.ReadLine()); SumB += BusinessMarkVal; //Counts how many marks in total for the business department BusinessMarkCount ++; //Prompts user to input a new department code, or sentinel value to exit the loop Console.WriteLine ("Enter a department code: 'C' or 'c' for Computer Science, 'M' or 'm' for Math, 'B' or 'b' for Business Admin, or enter 'Q' or 'q' to quit =>"); deptCode = Convert.ToChar(Console.ReadLine()); } } //Exit loop via sentinel value to iniatiate calculation of each average if (char.ToUpper(deptCode) == 'Q') { //Program calculates the average using the sum of the mark values from the departments individually, then divides them by the individual mark counts. CSAvg = SumCS / CompScienceMarkCount; MathAvg = SumM / MathMarkCount; BusinessAvg = SumB/ BusinessMarkCount; //Outputs the averages for each department to the user Console.WriteLine("The average for Computer Science students is {0}", CSAvg); Console.WriteLine("The average for Mathematics students is {0}", MathAvg); Console.WriteLine("The average for Business Administration students is {0}", BusinessAvg); Console.ReadLine(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
