14,265,813 members

# How do I compute the total score and average for each line (representing scores of a student). And also compute the total and average for each subject.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NonyeluCOMP2240A9
{
class Program
{
static void Main(string[] args)
{
string line, subject;
int s;
int t, average;
StreamWriter report = new StreamWriter("scoresReport.txt");

{
String[] strings = line.Split();
if(strings.Length == 3)
{
subject = strings[0];
s = int.Parse(strings[1]);
t = int.Parse(strings[2]);
average = s + t;
Console.WriteLine("The average of {0} is {1:c}", subject, average/5);
report.WriteLine("The average of {0} is {1:c}", subject, average/5);
}
}
f.Close();
report.Close();
}
}
}

What I have tried:

So far, I've tried computing, average = s + t / 5;
Console.WriteLine("Total cost of {0} is {1:c}", subject, average/5);

I also tried average = s + t;, without dividing by 5 and that didn'twork either. When I run the program, I get

"Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at NonyeluCOMP2240A9.Program.Main(String[] args) in c:\users\ify\documents\visual studio 2015\Projects\NonyeluCOMP2240A9\NonyeluCOMP2240A9\Program.cs:line 26
Press any key to continue . . .

The problem is below so you can understand it.

2. Create a text file, sourceScores.txt, which contains the following lines:

90 80 100
62 78 90
78 98 65
45 56 78
88 65 85

make sure you save this file in the \bin\debug subfolder of your project folder.

3. In the Main method, write code to read in data from the above file and compute the total score and average for each line (representing scores of a student). And also compute the total and average for each subject. Write the original scores and the totals and averages to a text file, scoresReport.txt. Your text file generate from your program may look like this:

90 80 100 237 90
62 78 90 230 77
78 98 65 241 80
45 56 78 179 60
88 65 85 237 79
362 377 418
72 75 84
Tags: C#