Part of method not running - c#

static void getGrades()
{
Console.WriteLine("How many grade level classes are you taking?");
int standardNumber = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("How many honors level classes are you taking?");
int honorsNumber = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("How many AP level classes are you taking?");
int apNumber = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("Enter your letter grades when prompted.");
Console.WriteLine("=======================================");
for (int a = 1; a == standardNumber; a++)
{
int num = 1;
Console.WriteLine("Enter letter grade for honors class {0}:", num);
switch (num)
{
case 1:
string class1 = Console.ReadLine();
break;
case 2:
string class2 = Console.ReadLine();
break;
case 3:
string class3 = Console.ReadLine();
break;
case 4:
string class4 = Console.ReadLine();
break;
default:
break;
}
Console.WriteLine();
}
}
I'm trying to call this method to collect number of classes and grade letters from a mix of 4 classes. When I call the method, the first part (below) executes fine.
Console.WriteLine("How many grade level classes are you taking?");
int standardNumber = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("How many honors level classes are you taking?");
int honorsNumber = int.Parse(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("How many AP level classes are you taking?");
int apNumber = int.Parse(Console.ReadLine());
However, the rest of the code (below) does not execute.
Console.WriteLine();
Console.WriteLine("Enter your letter grades when prompted.");
Console.WriteLine("=======================================");
for (int a = 1; a == standardNumber; a++)
{
int num = 1;
Console.WriteLine("Enter letter grade for honors class {0}:", num);
switch (num)
{
case 1:
string class1 = Console.ReadLine();
break;
case 2:
string class2 = Console.ReadLine();
break;
case 3:
string class3 = Console.ReadLine();
break;
case 4:
string class4 = Console.ReadLine();
break;
default:
break;
}
Console.WriteLine();
}
}
Does anyone know why this might be happening. Thanks in advance

According to your logic, if you enter the input standardNumber as 1 only it will run these lines,
for (int a = 1; a == standardNumber; a++)
{
int num = 1;
}
btw, You dont need a for loop for that.
EDIT:
You need to store the inputs in a collection and do a loop on that
static void Main(string[] args)
{
List<int> inputs = new List<int>();
Console.WriteLine("How many grade level classes are you taking?");
int standardNumber = int.Parse(Console.ReadLine());
inputs.Add(standardNumber);
Console.WriteLine();
Console.WriteLine("How many honors level classes are you taking?");
int honorsNumber = int.Parse(Console.ReadLine());
inputs.Add(honorsNumber);
Console.WriteLine();
Console.WriteLine("How many AP level classes are you taking?");
int apNumber = int.Parse(Console.ReadLine());
inputs.Add(apNumber);
Console.WriteLine();
Console.WriteLine("Enter your letter grades when prompted.");
Console.WriteLine("=======================================");
for (int i = 0; i < inputs.Count; i++)
{
Console.WriteLine("Enter letter grade for honors class {0}:", inputs[i]);
switch (i)
{
case 1:
string class1 = Console.ReadLine();
break;
case 2:
string class2 = Console.ReadLine();
break;
case 3:
string class3 = Console.ReadLine();
break;
case 4:
string class4 = Console.ReadLine();
break;
default:
break;
}
Console.WriteLine();
}
Console.Read();
}

Your method contains these two lines in a row:
int apNumber = int.Parse(Console.ReadLine());
Console.WriteLine();
There are two ways that it's possible for the first line to execute, but the second line not to:
The method call Console.ReadLine() never returns because the user never presses "enter".
Either the method Console.ReadLine or the method int.Parse throws an exception.
My guess is that int.Parse is throwing an exception.
A more theoretical answer:
If I'm not mistaken, when the code that I quoted runs, one of the following must happen, always, no matter what:
The first line continues executing forever.
The first line throws an exception.
The thread is terminated before the second line executes.
The first line returns and the second line executes.
If the last thing doesn't happen, one of the other things must be happening.

Related

C# I want to use either a switch or if statement to select a sorting algorithm

I am writing a program that will take user input and run it through whichever type of sort they choose. If I try to use a switch I cannot figure out how to add arguments to a switch or if I use an if statement how do I implement that with the user's input?
Here is the code and thank you all for your help.
using System;
namespace ASortAboveTheRest
{
internal class Program
{
static void Main(string[] args)
{
MainMenu();
}
static void MainMenu()
{
Console.Clear();
Console.WriteLine("Choose a sort algorithm to perform on the Array");
Console.WriteLine("");
Console.WriteLine("Option 1: Heap Sort");
Console.WriteLine("Option 2: Bubble Sort");
Console.WriteLine("Option 3: Shell Sort");
Console.WriteLine("Please type: 1, 2, or 3");
string myOption;
myOption = Console.ReadLine();
int[] arr = new int[10];
int i;
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
...
}
}
}
The following code should do the trick:
using System;
namespace ASortAboveTheRest {
internal class Program
{
static void Main(string[] args)
{
MainMenu();
}
static void MainMenu()
{
Console.Clear();
Console.WriteLine("Choose a sort algorithm to perform on the Array");
Console.WriteLine("");
Console.WriteLine("Option 1: Heap Sort");
Console.WriteLine("Option 2: Bubble Sort");
Console.WriteLine("Option 3: Shell Sort");
Console.WriteLine("Please type: 1, 2, or 3");
// Take Option input as an INT
int myOption;
myOption = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[10];
int i;
Console.Write("Input 10 elements in the array :\n");
for (i = 0; i < 10; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElements in array are: ");
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
//Use switch case after taking array input from the user
switch(myOption) {
case 1:
//Call Heap Sort Function and pass your array
break;
case 2:
// Call Bubble Sort Function and pass your array
break;
case 3:
//Call Shell Sort Function and pass your array
break;
default:
break;
}
}
}
}
You shall:
read the option as a string and then try to parse it, handling the mistake of conversion
also handle cases when a user enters a number that doesn't match your options
rule of thumb: never use 'if' you can use 'switch', never used 'switch' if the mapping can be specified as a dictionary
string optionTyped = Console.ReadLine();
int sortOption = Convert.ToInt32(Console.ReadLine());
var sortFunctions = new Dictionary<int, Func<int[], int[]>>
{
{ 1, HeapSort },
{ 2, BubbleSort },
{ 3, ShellSort }
};
while(!Int32.TryParse(optionTyped, out sortOption)
or (!sortFunctions.Keys.Contains(sortOption)))
{
Console.WriteLine("Not a valid option, try again.");
optionTyped = Console.ReadLine();
}
Func<int[], int[]> sort = sortFunctions[sortOption]
...
int[] resultArray = sort(inputArray)

Not enter in a switch case in C#

I can't enter in the case 2 of the switch statement and I don't know why. If I remove the do while loop the code runs perfectly. It's about something with the memory of the structure array? Here is the code:
class Notebook {
struct Student
{
public String id;
public String name;
public void showInfo(Student x) {
Console.WriteLine("\t ID: " + x.id);
Console.WriteLine("\t Name: " + x.name);
}
}
static void Main(string[] args){
bool display = true;
int studentNum = int.Parse(Console.ReadLine());
Student[] students = new Student[studentNum];
do {
Console.Clear();
Console.WriteLine("1.- Insert register");
Console.WriteLine("2.- Show register");
Console.WriteLine("3.- Exit");
String opc = Console.ReadLine();
switch (opc) {
case "1":
Console.Clear();
for(int i = 0; i < students.Length; ++i){
Console.WriteLine("Name of the student " + (i+1));
students[i].name = Console.ReadLine();
Console.WriteLine("ID of the student " + (i+1));
students[i].id = Console.ReadLine();
}
break;
case "2":
Console.Clear();
for(int i = 0; i < students.Length; ++i){
students[i].showInfo(students[i]);
}
break;
case "3":
Console.Clear();
Console.WriteLine("bye");
display = false;
break;
}
}while(display);
}
}
I think that is "something" in the memory of opc string that avoids the case 2.
Your problem is the Console.Clear statement that you run at start of do while loop. Comment that line and you will see that your code is going to case "2".
Its going to case "2" even in your original code, but console is every time being cleared at start of do while loop and so you don't see the statements written by case "2" logic.
There is no memory problem as you suspected.
The do while loop should have Console.Clear commented as in code below.
do {
//Console.Clear();
Console.WriteLine("1.- Insert register");
Console.WriteLine("2.- Show register");
Console.WriteLine("3.- Exit");
add a Console.ReadLine(); before break case "2".
case "2":
Console.Clear();
for (int i = 0; i < students.Length; ++i)
{
students[i].showInfo(students[i]);
}
Console.ReadLine();
break;
You write students informations and Call Console.Clear() after that

Collecting multiple Variables from one loop

I'm Working on a project for college and have sat here trying to figure out a solution to this problem for a solid 3 hours now. the problem is:
Scenario:
You want to calculate a student’s GPA (Grade Point Average) for a number of classes taken by the student during a single semester.
Inputs:
The student’s name.
Class names for the classes taken by the student.
Class letter grade for the classes taken by the student.
Class credit hours for the classes taken by the student.
Processing:
Accept and process classes until the user indicates they are finished.
Accumulate the number of credit hours taken by the student.
Calculate the total number of “points” earned by the student as:
a. For each class calculate the points by multiplying the credit hours for that class times the numeric equivalent of the letter grade. (A=4.0, B=3.0, C=2.0, D=1.0, F=0)
b. Total all points for all classes.
Calculate the GPA as the total number of “points” divided by the total credit hours.
Output:
Display a nicely worded message that includes the student’s name and the GPA (decimal point number with 2 decimal places) achieved by the student that semester.
What I currently have is:
static void Main(string[] args)
{
String StudentName;
//Error Trapping
try
{
Console.WriteLine("Welcome to The GPA Calculator");
Console.WriteLine("Hit any key to continue...");
Console.ReadKey();
Console.Write("Please enter your name: ");
StudentName = Convert.ToString(Console.ReadLine());
InputGradeInfo();
}
//Error Repsonse
catch (System.Exception e)
{
Console.WriteLine("An error has Occurred");
Console.WriteLine("The error was: {0}" , e.Message);
//Belittle the User
Console.WriteLine("Good Job, you Broke it.");
Console.ReadLine();
}
}
public static double InputGradeInfo()
{
String ClassName;
Char LetterGrade;
Double LetterGradeValue;
Double CreditHours;
Double ValueOfClass;
Console.Write("Please enter the class title: ");
ClassName = Convert.ToString(Console.ReadLine());
Console.Write("Please enter the total credits this class is worth: ");
CreditHours = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter the grade letter recived: ");
LetterGrade = Convert.ToChar(Console.ReadLine());
switch (LetterGrade)
{
case 'a': LetterGradeValue = 4;
break;
case 'A': LetterGradeValue = 4;
break;
case 'b': LetterGradeValue = 3;
break;
case 'B': LetterGradeValue = 3;
break;
case 'c': LetterGradeValue = 2;
break;
case 'C': LetterGradeValue = 2;
break;
case 'd': LetterGradeValue = 1;
break;
case 'D': LetterGradeValue = 1;
break;
case 'f': LetterGradeValue = 0;
break;
case 'F': LetterGradeValue = 0;
break;
default: LetterGradeValue = 0;
break;
}
ValueOfClass = CalculateClass(LetterGradeValue, CreditHours);
return ValueOfClass;
}
public static double CalculateClass(double LetterGrade, double CreditHours)
{
Double CreditTotal;
CreditTotal = CreditHours * LetterGrade;
return CreditTotal;
}
The Problem arises for me as to how one would loop info collection, save it to different variable every time and then breaking the loop using user input at the end. We haven't learned about arrays yet so that's off the table. After I have that looped collection down calculating the total GPA and displaying wouldn't be difficult.
Also I haven't learned about created classes yet so I can't use those either
static void Main(string[] args)
{
String StudentName;
//Error Trapping
try
{
Console.WriteLine("Welcome to The GPA Calculator");
Console.WriteLine("Hit any key to continue...");
Console.ReadKey();
Console.Write("Please enter your name: ");
StudentName = Convert.ToString(Console.ReadLine());
List<double> gradeInfoList = new List<double>();
List<double> creditList = new List<double>();
bool brakeLoop = false;
while (!brakeLoop)
{
gradeInfoList.Add(InputGradeInfo(creditList));
Console.WriteLine("Do you want to continue(y/n): ");
brakeLoop = Console.ReadLine() != "y";
}
Console.WriteLine(StudentName + " GPA is: " + gradeInfoList.Sum() / creditList.Sum());
}
//Error Repsonse
catch (System.Exception e)
{
Console.WriteLine("An error has Occurred");
Console.WriteLine("The error was: {0}", e.Message);
//Belittle the User
Console.WriteLine("Good Job, you Broke it.");
Console.ReadLine();
}
}
public static double InputGradeInfo(List<double> creditList)
{
String ClassName;
Char LetterGrade;
Double LetterGradeValue;
Double CreditHours;
Double ValueOfClass;
Console.Write("Please enter the class title: ");
ClassName = Convert.ToString(Console.ReadLine());
Console.Write("Please enter the total credits this class is worth: ");
CreditHours = Convert.ToDouble(Console.ReadLine());
creditList.Add(CreditHours);
Console.Write("Please enter the grade letter recived: ");
LetterGrade = Convert.ToChar(Console.ReadLine().ToUpper());
switch (LetterGrade)
{
case 'A': LetterGradeValue = 4;
break;
case 'B': LetterGradeValue = 3;
break;
case 'C': LetterGradeValue = 2;
break;
case 'D': LetterGradeValue = 1;
break;
case 'F': LetterGradeValue = 0;
break;
default: LetterGradeValue = 0;
break;
}
ValueOfClass = CalculateClass(LetterGradeValue, CreditHours);
return ValueOfClass;
}
public static double CalculateClass(double LetterGrade, double CreditHours)
{
Double CreditTotal;
CreditTotal = CreditHours * LetterGrade;
return CreditTotal;
}
Here you probably want this. You need one while loop to take all the classes, the loop brakes if you say that you don't want to continue or put another input. You make the gradeInfoList with gradeInfoList.Sum() function.
Also your variables should start with small letter, StudentName->studentName !
EDIT:
gpa List is collection which stores all your values which comes from InputGradeInfo().
What Sum() function is doing:
double sum = 0;
foreach(double d in gpa)
{
sum= sum + d; //(or sum+= d;)
}
In other words loop all the elements in gradeInfoList collection and make the sum of them.
About the while loop-> this loop will executes till the condition in the brackets is broken. In this case you can add many classes till you click 'y' at the end. If you click something else from 'y' you will break the loop.
I add another list creditList which you will add as parameter to the InputGradeInfo. In this list you will store every credit per class. At the end you will have gradeInfoList .Sum()/creditList.Sum() and this will give you what you want.

c# switch loop, adding up total for each case

I have 2 codes that I want to combine into 1 and am having a lot of trouble doing it. The code should ask for the group number then their donation amount and loop back until they press 0. Once they press 0 it should show the total for all groups. Here are the 2 different codes
code 1
using System;
public class TotalPurchase
{
public static void Main()
{
double donation;
double total = 0;
string inputString;
const double QUIT = 0;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString = Console.ReadLine();
donation = Convert.ToDouble(inputString);
while(donation != QUIT)
{
total += donation;
Console.WriteLine("Enter next donation amount, or " +
QUIT + " to quit ");
inputString = Console.ReadLine();
donation = Convert.ToDouble(inputString);
}
Console.WriteLine("Your total is {0}", total.ToString("C"));
}
}
code 2
using System;
namespace donate
{
class donate
{
public static void Main()
{
begin:
string group;
int myint;
Console.WriteLine("Please enter group number (4, 5, or 6)");
Console.WriteLine("(0 to quit): ");
group = Console.ReadLine();
myint = Int32.Parse(group);
switch (myint)
{
case 0:
Console.WriteLine("Bye.");
break;
case 4:
case 5:
case 6:
double donation;
string inputString;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString = Console.ReadLine();
donation = Convert.ToDouble(inputString);
goto begin;
default:
Console.WriteLine("Incorrect grade number.", myint);
goto begin;
}
}
}
}
So basically I want to find the total for each group using the 2nd code.
Any help would be greatly appreciated.
Don't use goto like that. Just use another while loop like you did in the first code segement.
It would be much more concise to say:
if (myInt > 3 && myInt < 7) { ... }
instead of using that switch statement.
Basically your code for summing up the donation amount does the trick, so just stick that inside a similar loop that handles what the group number is. If you do this you're going to want to use a different input to signify the end of donations vs the end of input altogether. It would be confusing if the application said "Enter 0 to quit input", followed by "Enter 0 to quit donation input".
You are very close with Code 2. If you put Code 2 inside while loop it works!
The only code I have written for you here is declared myint outside the while loop and initialised it to a non-zero value.
double total = 0;
int myint = -1;
while (myint != 0)
{
string group;
Console.WriteLine("Please enter group number (4, 5, or 6)");
Console.WriteLine("(0 to quit): ");
group = Console.ReadLine();
myint = Int32.Parse(group);
switch (myint)
{
case 0:
Console.WriteLine("Bye.");
break;
case 4:
case 5:
case 6:
double donation;
string inputString;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString = Console.ReadLine();
donation = Convert.ToDouble(inputString);
total += donation;
break;
default:
Console.WriteLine("Incorrect grade number.", myint);
break;
}
}
Console.WriteLine("Your total is {0}", total.ToString("C"));

C# How do you count the number of inputs to find the average in a switch loop?

Here is my loop that asks for the group number then the donation. I'm wondering how to count the number of donations to find the average for each group.
using System;
public class TotalPurchase
{
public static void Main()
{
double total4 = 0;
double total5 = 0;
double total6 = 0;
int myint = -1;
while (myint != 0)
{
string group;
Console.WriteLine("Please enter group number (4, 5, or 6)");
Console.WriteLine("(0 to quit): ");
group = Console.ReadLine();
myint = Int32.Parse(group);
switch (myint)
{
case 0:
break;
case 4:
double donation4;
string inputString4;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString4 = Console.ReadLine();
donation4 = Convert.ToDouble(inputString4);
total4 += donation4;
break;
case 5:
double donation5;
string inputString5;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString5 = Console.ReadLine();
donation5 = Convert.ToDouble(inputString5);
total5 += donation5;
break;
case 6:
double donation6;
string inputString6;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString6 = Console.ReadLine();
donation6 = Convert.ToDouble(inputString6);
total6 += donation6;
break;
default:
Console.WriteLine("Incorrect grade number.", myint);
break;
}
}
Console.WriteLine("Grade 4 total is {0}", total4.ToString("C"));
Console.WriteLine("Grade 5 total is {0}", total5.ToString("C"));
Console.WriteLine("Grade 6 total is {0}", total6.ToString("C"));
}
}
Any help would be appreciated.
Not sure if I fully understand your question -- but you could just add a simple counter for each group:
int donations4 = 0;
int donations5 = 0;
int donations6 = 0;
And then increment that counter in each of your switch cases, ex:
switch(myInt)
{
case 4:
...
donations4++;
break;
case 5:
...
donations5++;
break;
case 6:
...
donations6++;
break;
}
Then when you're done - simply do the math to find the average.
Although this is probably the simplest way, a better way would be to treat each group as its own object, and have the object internally track the # of donations, as well as the sum and average.
-- Dan
using System;
public class TotalPurchase
{
public static void Main()
{
double total4 = 0;
double total5 = 0;
double total6 = 0;
int numberOfInputForTotal4 = 0;
int numberOfInputForTotal5 = 0;
int numberOfInputForTotal6 = 0;
int myint = -1;
while (myint != 0)
{
string group;
Console.WriteLine("Please enter group number (4, 5, or 6)");
Console.WriteLine("(0 to quit): ");
group = Console.ReadLine();
myint = Int32.Parse(group);
switch (myint)
{
case 0:
break;
case 4:
double donation4;
string inputString4;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString4 = Console.ReadLine();
donation4 = Convert.ToDouble(inputString4);
total4 += donation4;
numberOfInputForTotal4++;
break;
case 5:
double donation5;
string inputString5;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString5 = Console.ReadLine();
donation5 = Convert.ToDouble(inputString5);
total5 += donation5;
numberOfInputForTotal5++;
break;
case 6:
double donation6;
string inputString6;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString6 = Console.ReadLine();
donation6 = Convert.ToDouble(inputString6);
total6 += donation6;
numberOfInputForTotal6++;
break;
default:
Console.WriteLine("Incorrect grade number.", myint);
break;
}
}
Console.WriteLine("Grade 4 total is {0}", total4.ToString("C"));
Console.WriteLine("Grade 5 total is {0}", total5.ToString("C"));
Console.WriteLine("Grade 6 total is {0}", total6.ToString("C"));
Console.WriteLine("Grade 4 average is {0}", (total4 / numberOfInputForTotal4).ToString("C"));
Console.WriteLine("Grade 5 average is {0}", (total5 / numberOfInputForTotal5).ToString("C"));
Console.WriteLine("Grade 6 average is {0}", (total6 / numberOfInputForTotal6).ToString("C"));
}
}
As you can see, there are 3 extra variables (one for each group) that can be used to figure out the number of inputs provided. Using that you can divide the total for each group by the number of input in each group separately.
Just declare count for each group as well as total and increment in the case statement:
case 4:
double donation4;
string inputString4;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString4 = Console.ReadLine();
donation4 = Convert.ToDouble(inputString4);
total4 += donation4;
count4++; // HERE!!!!
break;
Alternatively, you can use List<int> which will calculate your average as well:
List<int> list4 = new List<int>();
and
case 4:
double donation4;
string inputString4;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString4 = Console.ReadLine();
donation4 = Convert.ToDouble(inputString4);
list4.Add(donation4);
break;
and
Console.WriteLine(list4.Average());
Just keep track of the count yourself with another variable. count4, count5, etc.
For bonus points in your homework assignment:
1) Sanitize your group number input - ie check to see if the user typed in a valid number.
2) Don't call the variable myInt. Call it groupNum, or something that describes the function, not the implementation of the variable.
3) Use an array for donation totals and counts
ie,
int[] donationCount= new int[MAX_GROUP+1]; // figure out yourself why the +1
int[] donationTotal= new int[MAX_GROUP+1];
// initialize donationCount and donationTotal here
then in your loop (don't even need switch):
++donationCount[groupNum];
donationTotal[groupNum] += donationAmount; // did you notice that you moved the reading of donationAmount out of the switch?
I would go with changing your doubles to List and using the Sum() and Average() methods on your Lists at the end. Your code would look like this after this change.
using System;
using System.Collections.Generic;
using System.Linq;
public class TotalPurchase
{
public static void Main()
{
List<double> total4 = new List<double>();
List<double> total5 = new List<double>();
List<double> total6 = new List<double>();
int myint = -1;
while (myint != 0)
{
string group;
Console.WriteLine("Please enter group number (4, 5, or 6)");
Console.WriteLine("(0 to quit): ");
group = Console.ReadLine();
myint = Int32.Parse(group);
switch (myint)
{
case 0:
break;
case 4:
double donation4;
string inputString4;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString4 = Console.ReadLine();
donation4 = Convert.ToDouble(inputString4);
total4.Add(donation4);
break;
case 5:
double donation5;
string inputString5;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString5 = Console.ReadLine();
donation5 = Convert.ToDouble(inputString5);
total5.Add(donation5);
break;
case 6:
double donation6;
string inputString6;
Console.WriteLine("Please enter the amount of the contribution: ");
inputString6 = Console.ReadLine();
donation6 = Convert.ToDouble(inputString6);
total6.Add(donation6);
break;
default:
Console.WriteLine("Incorrect grade number.", myint);
break;
}
}
if(total4.Count > 0)
Console.WriteLine("Grade 4 total is {0}; Average {1}", total4.Sum().ToString("C"), total4.Average().ToString("C"));
if(total5.Count >0)
Console.WriteLine("Grade 5 total is {0}; Average {1}", total5.Sum().ToString("C"), total5.Average().ToString("C"));
if (total6.Count > 0)
Console.WriteLine("Grade 6 total is {0}; Average {1}", total6.Sum().ToString("C"), total6.Average().ToString("C"));
}
}

Categories