Click here to Skip to main content
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.

Yetco asked:

Open original thread
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

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

while((line = f.ReadLine()) != null)
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);

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:

Math reading writing
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:

Math reading writing total averages
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#


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100