-
Nov 12th, 2014, 06:49 PM
#1
Thread Starter
Junior Member
Help! Display numbers and grade from tests for each person.
I got these errors on my program and I need help fixing them.
Directions:
NOTE: Student First Name + a space + Student Last Name will be 18 characters maximum
Computer Problem:
To write a program, named P08.vb, that has the Program Name and Your Name, as comment lines, at the top of the source code.
The program displays a list of names of all students, their score on each test, their average score (rounded to a whole number), and the letter grade. The grade will depend on the rounded average score as follows:
Average Score Letter Grade
0 - 59 "F"
60 - 69 "D"
70 - 79 "C"
80 - 89 "B"
90 - 100 "A"
Array and Function Requirements:
An array, named Score(), of integers, to read and process five test scores.
A function, named Sum(), which computes and returns the sum of the scores as an integer.
A function, named Average(), which computes and returns the average score, rounded
to an integer.
A function, named Grade(), which computes and returns the letter grade, based on the
average score.
Suggested Header: Function Grade(ByVal averageScore As Integer) As Char
Display Your Name and Assignment# at the top of the output screen.
My code:
' Name: Anna DeAngelis
' Program: p08.vb
Imports System.IO
Imports RahimLibrary
Module P07
Const AUTHOR As String = "Anna DeAngelis Assignment 8"
'-------------------- Subroutine: Main()---------------------------------
Sub Main()
Dim Tokens As StringTokenizer
Dim Delimiter As Char() = {" ", ",", ";", ":"}
Dim Diskfile As String = "P08.DAT"
Dim Name As String
Dim k, scoreSum, avgScore As Integer
Dim Grade As Char
Console.WriteLine(vbNewLine & AUTHOR)
If Not File.Exists(Diskfile) Then
Console.WriteLine("File: " & Diskfile & " does not exist")
Utility.BlankLine(2) : Exit Sub : End If
FileOpen(1, Diskfile, OpenMode.Input)
While Not EOF(1)
Tokens = New StringTokenizer(LineInput(1), Delimiter)
Name = Tokens.NextToken() & " " & Tokens.NextToken()
Name = Name.PadRight(13)
Console.Write(vbNewLine & Name)
For k = 0 To 4
Score(k) = Tokens.NextToken()
Console.Write(Format(Score(k), "0").PadLeft(4))
Next
ScoreSum = Sum(Score)
avgScore = Average(ScoreSum, 5)
Console.WriteLine(" Average =" & Format(avgScore, "0").PadLeft(4))
End While
FileClose(1)
End Sub ' Main()
'-------------------- Function: Sum()---------------------------------
Function Sum(ByVal P As Integer()) As Integer
Dim scoreSum = 0, k As Integer
For k = 0 To P.GetUpperBound(0) : scoreSum += P(k) : Next
Return scoreSum
End Function 'Sum()
'-------------------- Function: Average() --------------------------------
Function Average(ByVal scoreSum As Integer, _
ByVal count As Integer) As Integer
Dim averageScore As Double
averageScore = scoreSum/count
averageScore = Utility.Round(averageScore, 0)
Return Convert.ToInt32(averageScore)
End Function 'Average()
'-------------------- Function: Grade() --------------------------------
Function Grade (ByVal Grade As Char, _
ByVal Score As Integer) As Char
If (Score < 60) Then : Console.WriteLine("F")
ElseIf (Score 60 To 69) Then : Console.WriteLine("D")
ElseIf (Score 70 To 79) Then : Console.WriteLine("C")
ElseIf (Score 80 To 89) Then : Console.WriteLine("B")
ElseIf (Score 90 To 100) Then : Console.WriteLine("A")
End If
Return Grade
End Function 'Grade()
End Module 'P08
-
Nov 12th, 2014, 11:49 PM
#2
Re: Help! Display numbers and grade from tests for each person.
Hi,
If we ignore all the issues that relate to the Namespace “RahimLibrary”, since none of us know what this is, there are quite a few issues there that you need to deal with. First of all, Turn Option Strict On Now, and never turn it off. It will actually cause you a few additional errors due to the current Type Conversion errors in your code but it will help you to identify and correct these errors as you write your code in the future. So, a few things to look at are:-
1) A Char Array should contain Char Characters and if you want to initialise a Char Array when its declared then it should be done like so:-
vb.net Code:
Dim Delimiter As Char() = {" "c, ","c, ";"c, ":"c}
2) You apply information to the elements of a Score Array, i.e:-
vb.net Code:
Score(k) = Tokens.NextToken()
But you have NOT declared the Score Array? You need to declare this array just before the Start of your For Loop.
3) You create function with the Signature of:-
vb.net Code:
Function Grade(ByVal Grade As Char, ByVal Score As Integer) As Char
You must remember that you cannot declare a Parameter Name which is the Same as its Defining Function Name. Therefore the parameter name “Grade” is invalid.
In this case you actually want to Return the Grade from the Score and not Pass the Grade to the function in the first place.
4) In the Grade Function you have the conditional statement:-
vb.net Code:
If (Score < 60) Then : Console.WriteLine("F")
ElseIf (Score 60 To 69) Then : Console.WriteLine("D")
ElseIf (Score 70 To 79) Then : Console.WriteLine("C")
ElseIf (Score 80 To 89) Then : Console.WriteLine("B")
ElseIf (Score 90 To 100) Then : Console.WriteLine("A")
End If
Ignoring the Colons for the moment, which you should not really use, the first If statement is correct but where are all the other Equality and Conditional Operators after that? That “If” statement should follow along the lines of:-
vb.net Code:
If (Score < 60) Then
Console.WriteLine("F")
ElseIf Score >= 60 AndAlso Score <= 69 Then
Console.WriteLine("D")
'ElseIf etc.....
End If
Saying that, you actually need to Return the Grade from the Call to the Grade Function so you need to declare a variable of Type Char in the Function to hold the Grade, calculate the actual Grade with the If statement and assign that value to the Grade variable and then Return that Grade to the calling routine for Display rather than Displaying it in this function.
My final comment at this stage would be your use of “FileOpen” and a While Loop to read your file. You may be required to do that as part of your work but I would recommend using simpler techniques to do this, just one of which is:-
vb.net Code:
For Each studentRecord In File.ReadAllLines(Diskfile)
'Your Code Here
Next
Hope that helps.
Cheers,
Ian
-
Nov 13th, 2014, 11:03 AM
#3
Thread Starter
Junior Member
Re: Help! Display numbers and grade from tests for each person.
Here is the errors I am getting after I added some things to my code.
Here is my code:
' Name: Anna DeAngelis
' Program: p08.vb
Imports System.IO
Imports RahimLibrary
Module P07
Const AUTHOR As String = "Anna DeAngelis Assignment 8"
'-------------------- Subroutine: Main()---------------------------------
Sub Main()
Dim Tokens As StringTokenizer
Dim Delimiter As Char() = {" ", ",", ";", ":"}
Dim Diskfile As String = "P08.DAT"
Dim Name As String
Dim Score(4) As Integer
Dim k, scoreSum, avgScore As Integer
Console.WriteLine(vbNewLine & AUTHOR)
If Not File.Exists(Diskfile) Then
Console.WriteLine("File: " & Diskfile & " does not exist")
Utility.BlankLine(2) : Exit Sub : End If
FileOpen(1, Diskfile, OpenMode.Input)
While Not EOF(1)
Tokens = New StringTokenizer(LineInput(1), Delimiter)
Name = Tokens.NextToken() & " " & Tokens.NextToken()
Name = Name.PadRight(18)
Console.Write(vbNewLine & Name)
For k = 0 To 4
Score(k) = Tokens.NextToken()
Console.Write(Format(Score(k), "0").PadLeft(4))
Next
ScoreSum = Sum(Score)
avgScore = Average(ScoreSum, 5)
Console.WriteLine(" Average =" & Format(avgScore, "0").PadLeft(4))
End While
FileClose(1)
End Sub ' Main()
'-------------------- Function: Sum()---------------------------------
Function Sum(ByVal P As Integer()) As Integer
Dim scoreSum = 0, k As Integer
For k = 0 To P.GetUpperBound(0) : scoreSum += P(k) : Next
Return scoreSum
End Function 'Sum()
'-------------------- Function: Average() --------------------------------
Function Average(ByVal scoreSum As Integer, _
ByVal count As Integer) As Integer
Dim averageScore As Double
averageScore = scoreSum/count
averageScore = Utility.Round(averageScore, 0)
Return Convert.ToInt32(averageScore)
End Function 'Average()
'-------------------- Function: Grade() --------------------------------
Function Grade (ByVal averageScore As Integer) As Char
If (averageScore < 60) Then
Console.WriteLine("F")
ElseIf (averageScore 60 To 69) Then
Console.WriteLine ("D")
ElseIf (averageScore 70 To 79) Then
Console.WriteLine("C")
ElseIf (averageScore 80 To 89) Then
Console.WriteLine("B")
ElseIf (averageScore 90 To 100) Then
Console.WriteLine("A")
End If
Return Grade
End Function 'Grade()
End Module 'P08
-
Nov 13th, 2014, 11:11 AM
#4
Re: Help! Display numbers and grade from tests for each person.
Hi,
Then you need to read Post#2 again!
Cheers,
Ian
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|