Results 1 to 8 of 8

Thread: VB 2010 Assignment

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    19

    VB 2010 Assignment

    I've been told this type of post has been answered before but Ive searched and couldn't find anything.
    Can anyone help me out or point me in the direction of a post that could. Thanks

    Read a text file containing a single student's grades. Each line simply contains a letter grade. You can assume that each course is 3 credits
    Task: Compute the student's GPA
    Grade table is as follows
    A 4.0
    A- 3.67
    B+ 3.33
    B 3.0
    B- 2.67
    C+ 2.33
    C 2.0
    C- 1.67
    D 1.0
    F 0

    GPA is computed by obtaining the total quality points and dividing by total credits
    Quality Points are calculated by multiplying the points for the grade by the number of credits

    the output should read "student's GPa is xx.xx" usising two decimal places
    The GUI is a Button and an output label

    txtfile

    A

    A-

    C

    D

    C-

    B

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    19

    Re: VB 2010 Assignment

    I have an issue adding values in the code for the textfile

  3. #3
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    Re: VB 2010 Assignment

    The reason you are having trouble adding numbers you read in is because they come in as a string data type. That is where the convertion comes into play. You have different conversions such as cdbl( string ), cint( string ), cbool( string )...

    One thing you could do is read in a line and use the split function to split each line on the space, assuming that there is always a space between the Grade and number, then assign that to an array.


    if you do Imports System.IO at the top, you can leave the system.io off of the first line in the code below:dim i as integer = 0

    Code:
    dim tempGradeArray() as string
    dim letterArray(1) as string 'these may both need to be 0, play around with it a bit.
    dim scoreArray(1) as double
    
    using gradeReader as New system.IO.StreamReader("C:\gradesfolder\textfile.txt")
        do until gradeReader.EndOfStream()   
            tempGradeArray = split(gradeReader.ReadLine()," ",2)
            letterArray(i) = tempGradeArray (0)
            scoreArray(i) = cdbl(tempGradeArray (0))
            redim Preserve letterArray(i + 1) ' redim allows you to make an array bigger or smaller
            redim Preserve scoreArray(i + 1 ) ' preserve saves the data in the array, if you don't do this, then you loose the array info
            i + = 1
        Loop
    end Using ' this closes the file even if an error occurs during this section. Once control leaves the using block what ever resources it was 
                   ' using will be disposed of.
    this is not exact, I typed it in here verses into a compiler, so the syntax may be off a bit, but it gives you an idea.
    Now you will have two arrays holding what you want, and you can process the data how ever you wish.
    Last edited by Adam Stallcup; Jul 26th, 2013 at 03:58 PM.

  4. #4
    Addicted Member
    Join Date
    Jan 2013
    Posts
    242

    Re: VB 2010 Assignment

    for the output you would do this:
    dim D as double
    dim stud_Grade as string = ""

    D = qualityPoints / totalPoints
    stud_Grade = "student's GPa is " & format(D, "00.00") ' the output is a string for the format function. 0 forces it to put something in that place, whereas # would only put something in that place value if it had a number to put in it.

    format(1234.56 , "0000.000") gives 1234.560
    format(1234.5 , "0000.###") gives 1234.5

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: VB 2010 Assignment

    Create a function where you pass in the letter grade (Name it something meaningful like GetQualityPoints). In that function create a Select Case statement. If the Case is "A" you return 4.0 x the course's hours. If the Case is "A-" you return 3.67 * the course's hours. etc.

    Keep a running total of the number of credit hours.

    Keep a running total of the quality points returned from the function.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    19

    Re: VB 2010 Assignment

    I keep getting a error trying to create a case in a private function

  7. #7
    Addicted Member MetalInquisitor's Avatar
    Join Date
    Sep 2012
    Posts
    143

    Re: VB 2010 Assignment

    Are people supposed to guess what that error is? You need to understand that unless you give the details, nobody will be able to help.
    So, what is the exact error? What do you expect to happen? What is actually happening? What do you have so far?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2013
    Posts
    19

    Re: VB 2010 Assignment

    this is the code i have, im having trouble getting my stream reader to load and my answer to populate the textbox

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            Dim fileName As String = IO.File.ReadAllText("Grades.txt")
            Dim GPA As Double = 0
            Dim i As Integer = 0
            If IO.File.Exists(fileName) Then
                Dim sr As New IO.StreamReader(fileName)
                Dim line As String
                Dim finalscore As Double = 0
                Dim count As Integer = 0
    
                Using reader As = StreamReader = New StreamReader(fileName)
                    While Not reader.EndOfStream
    
                    End While
    
                    line = reader.ReadLine()
                End Using
    
    
    
                If line.Trim() <> String.Empty Then
                    finalscore += calculate(line)
                    count += 1
                End If
    
                GPA = finalscore / (count * 3)
                TextBox1.Text = "student's GPa is " & Math.Round(GPA, 2).ToString()
    
    
            End If
    
    
    
        End Sub
    
        Private Function calculate(ByVal grade As String) As Double
            Dim value As Double = 0
    
            If grade = "A" Then
                value = 4.0 * 3
    
            ElseIf grade = "A-" Then
                value = 3.67 * 3
    
            ElseIf grade = "B+" Then
                value = 3.33 * 3
    
            ElseIf grade = "B" Then
                value = 3 * 3
    
            ElseIf grade = "B-" Then
                value = 2.67 * 3
    
            ElseIf grade = "C+" Then
                value = 2.33 * 3
    
            ElseIf grade = "C" Then
                value = 2.0 * 3
    
            ElseIf grade = "C-" Then
                value = 1.67 * 3
    
            ElseIf grade = "D" Then
                value = 1.0 * 3
    
            ElseIf grade = "F" Then
                value = 0
    
            End If
    
            Return value
    
    
        End Function
    End Class
    Last edited by onlivecoder; Jul 27th, 2013 at 11:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width