'C# Visual Studio Console does not display output, no error message is given
I have a program that reads a text file with student information and is supposed to compute average grades and present them in a table. I have only modified method PopulateStudents() in class studentS and MainMethod() in GradesUI. The rest of the work was provided by the prompt. Initially I was getting an index out of bounds error but after some fiddling I'm not getting an error but I am also not getting any results showing on the console. Any help would be appreciated:
class GradesUI
{
StudentS myStudentS;
public void MainMethod()
{
//instance an object of StudentUI..
myStudentS = new StudentS();
//Call StudentUI->PopulateStudents..
string path = @"C:\Users\PathgoesHere.txt";
bool returnValue = myStudentS.PopulateStudents;
//Verify if the file was created correctly by checking the return value.
if (returnValue == true)
{
DisplayInfo();
}
else
Console.WriteLine("There was a problem loading the file"); //else display error message.
}
void DisplayInfo()
{
Console.WriteLine("Student id\tLast Name\tAverage \tGrade");
for (int index = 0; index < myStudentS.ListLength; index++)
{
Console.WriteLine(" {0} \t {1} \t {2} \t {3}", myStudentS.StudentID(index), myStudentS.StudentLastName(index), myStudentS.StudentAverage(index), myStudentS.StudentGrade(index));
}
}
}
}
class StudentS
{
List<Student> theStudentList;
public bool PopulateStudents(string path)
{
theStudentList = new List<Student>();
bool flag = false;
int index2 = 3;
try
{
List<string[]> strArrayList = new List<string[]>();
using (StreamReader streamReader = new StreamReader(path))
{
string str;
while ((str = streamReader.ReadLine()) != null)
{
string[] strArray = str.Split(',');
strArrayList.Add(strArray);
}
}
for (int index1 = 0; index1 < strArrayList.Count; ++index1)
{
string[] strArray = strArrayList[index1];
Student student = new Student(strArray[0], strArray[1], strArray[2]);
while (index2 < strArray.Length)
{
student.EnterGrade(int.Parse(strArray[index2]), int.Parse(strArray[index2 + 1]));
index2 += 2;
}
student.CalGrade();
theStudentList.Add(student);
}
}
catch (Exception e)
{
flag = true;
Console.WriteLine(e);
}
return flag;
}
public int ListLength
{
get { return theStudentList.Count; }
}
public string StudentID(int index)
{
return theStudentList.ElementAt(index).ID;
}
public string StudentLastName(int index)
{
return theStudentList.ElementAt(index).NameLast;
}
public string StudentGrade(int index)
{
return theStudentList.ElementAt(index).LetterGrade;
}
public float StudentAverage(int index)
{
return theStudentList.ElementAt(index).Average;
}
}
}
class Student {
//data member
string nameFirst;
string nameLast;
string studentID;
List<int> earned;
List<int> possible;
float average;
string letterGrade;
public Student() //default constructor
{
studentID = null;
earned = new List<int>();
possible = new List<int>();
}
public Student(string id) //overloaded constructor
{
studentID = id;
nameFirst = null;
nameLast = null;
earned = new List<int>();
possible = new List<int>();
}
public Student(string id, string first, string last) //overloaded constructor
{
nameFirst = first;
nameLast = last;
studentID = id;
earned = new List<int>();
possible = new List<int>();
}
public void EnterGrade(int earnedValue, int possValue)
{
earned.Add(earnedValue);
possible.Add(possValue);
}
public float Average
{
get { return average; }
}
public string LetterGrade
{
get { return letterGrade; }
}
public string ID
{
get { return studentID; }
}
//accessor mutators //properties
public string NameFirst
{
get //accessor - getters
{
return nameFirst;
}
set //mutator - setter
{
nameFirst = value;
}
}
public string NameLast
{
get //accessor - getters
{
return nameLast;
}
set //mutator - setter
{
nameLast = value;
}
}
public void CalGrade()
{
int totalEarned = 0;
int totalPossible = 0;
foreach (int item in earned)
totalEarned += item;
foreach (int item in possible)
totalPossible += item;
average = (float)totalEarned / totalPossible; //you could think that this is stored in class Level but not.
average = (float)Math.Round(average, 2);
if (average >= .90 )
letterGrade = "A";
if (average >= .80 && average < .90)
letterGrade = "B";
if (average >= .70 && average < .80)
letterGrade = "C";
if (average >= .60 && average < .70)
letterGrade = "D";
if (average < .60)
letterGrade = "U";
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
