Click here to Skip to main content
14,269,736 members
Rate this:
Please Sign up or sign in to vote.
See more:
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);
}
}
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:

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
Posted
Updated 20-Apr-16 17:33pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Okay, for the first line of your input file, "Math reading writing", your strings array will contain "Math", "reading" and "writing".

Now, when you are trying to parse strings[1] to an integer, you are actually trying to parse "reading" to an integer. That is why you are getting error.

Deal with the first line of the input file and output file separately outside the while loop.
   
Comments
Yetco 21-Apr-16 0:04am
   
I tried adding Math, reading, and writing to the array but I'm still getting errors. I don't understand
Garth J Lancaster 21-Apr-16 0:13am
   
no, either
a) delete the 'heading' line from the file, or
b) do something like this

int linesInCount = 0;
while((line = f.ReadLine()) != null)
{
// If First Line From File, Skip
if (!linesInCount == 0)
{
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);
} // if strings.Length == 3
} // if linesInCount
linesInCount += 1; // or ++, whatever your poison
} // while
debashishPaul 21-Apr-16 0:15am
   
Look, it must be your assignment... so, I shouldn't give you a direct solution... I am giving you a guideline as you are almost there...

Don't add them into array... read the first line, append " total averages" to it, write it to the output file...

start processing the numbers in the loop from the second line of the input file...
Yetco 21-Apr-16 10:30am
   
I've tried, but I'm still not getting it. I want to figure it out on my own, but I can't

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




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