Reputation: 27
Is this possible? Can it be done by using a function of some sort. All i can find is examples on how to make an output span the width of a text box within a visual basic windows form app.
My code is below.
Sub Main()
Dim usrnum(1), usramount, userchoice, count, result2 As Integer
Dim totalstars(1), question1, userchoice2, nchrt, result1 As String
Do Until question1 = "y"
userchoice = 0
Do Until userchoice = 1 Or userchoice = 2
'menu - text tries to be as specific as possible to prevent an incorrect input from the user
WriteLine("------------------- Bar chart creator! ---------------------")
WriteLine(" ")
WriteLine(" ")
WriteLine("===============================================================")
WriteLine("************************** MENU ***************************")
WriteLine("===============================================================")
WriteLine(" ")
ForegroundColor = ConsoleColor.Cyan
WriteLine(" 1 - To begin ")
ForegroundColor = ConsoleColor.Yellow
WriteLine(" 2 - To exit ")
WriteLine(" ")
WriteLine(" ")
WriteLine(" ")
WriteLine(" ")
'reads in data from the user from the menu
Try
userchoice = ReadLine()
Clear()
Catch ex As Exception
'if this produces an error, for example a string is entered by the user then the following message will be issued to the user
WriteLine("That value is unknown!")
WriteLine("Please enter an Integer only")
End Try
If userchoice = 1 Then
WriteLine("What would you like to name your bar chart as?")
nchrt = ReadLine()
Clear()
WriteLine("Enter the amount of values you want to enter.")
Try
usramount = ReadLine()
Catch ex As Exception
'if this produces an error, for example a string is entered by the user then the following message will be issued to the user
WriteLine("That value is unknown!")
WriteLine("Please enter an Integer only: ")
End Try
Clear()
WriteLine("Values for the chart " & nchrt)
WriteLine("")
WriteLine("Please note! Each * Is equal To 100. Once a value reaches over 1000 the maxium amount Of * shown Is 10")
WriteLine("")
ReDim usrnum(usramount) 're-assign the users input as the value for the arraysize
ReDim totalstars(usramount)
count = 0
'the count allows the user to see what how many values they have entered so far and what number they are currently on
While count < usramount
For i As Integer = 0 To usrnum.Length - 1
'allows the users numbers to be recorded into the array
WriteLine("Please enter value " & count & " And <press> enter")
Try
usrnum(i) = ReadLine()
count = count + 1
Catch ex As Exception
'if this produces an error, for example a string is entered by the user then the following message will be issued to the user
WriteLine("That value is unknown!")
WriteLine("Please enter an Integer only.")
ReadLine()
End Try
Clear()
Next i
End While
'Calulates how many * to display with an IF statement
For i As Integer = 0 To totalstars.Length - 1
If usrnum(i) > 1000 Then
totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ")
'This was where my function was supposed to work but I can't get it to work the way I wanted it to
'These commented out if statements would have checked to see if the usrnum(i) current value was the biggest in relation to the output of the function max()
'If the current value of usrnum(i) was equal to the biggest value output by max() then extra spacing would be added between the stars so that they would span the width of the console window
'Else
'If usrnum(i) > 1000 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 900 And usrnum(i) < 1000 Then
totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ")
'Else
'If usrnum(i) >= 900 And usrnum(i) < 1000 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 800 And usrnum(i) < 900 Then
totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ")
'Else
'If usrnum(i) >= 800 And usrnum(i) < 900 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 700 And usrnum(i) < 800 Then
totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * ")
'Else
'If usrnum(i) >= 700 And usrnum(i) < 800 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 600 And usrnum(i) < 700 Then
totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * ")
'Else
'If usrnum(i) >= 600 And usrnum(i) < 700 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 500 And usrnum(i) < 600 Then
totalstars(i) = (" * " & " * " & " * " & " * " & " * ")
'Else
'If usrnum(i) >= 500 And usrnum(i) < 600 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 400 And usrnum(i) < 500 Then
totalstars(i) = (" * " & " * " & " * " & " * ")
'Else
'If usrnum(i) >= 400 And usrnum(i) < 500 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * " & " * ")
Else
If usrnum(i) >= 300 And usrnum(i) < 400 Then
totalstars(i) = (" * " & " * " & " * ")
'Else
'If usrnum(i) >= 300 And usrnum(i) < 400 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " & " * ")
Else
If usrnum(i) >= 200 And usrnum(i) < 300 Then
totalstars(i) = (" * " & " * ")
'Else
'If usrnum(i) >= 200 And usrnum(i) < 300 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * " & " * " )
Else
If usrnum(i) >= 100 And usrnum(i) < 200 Then
totalstars(i) = " * "
'Else
'If usrnum(i) >= 300 And usrnum(i) < 400 And usrnum(i) = Max(usrnum(usramount)) Then
'totalstars(i) = (" * ")
Else
If usrnum(i) >= 0 And usrnum(i) < 100 Then
totalstars(i) = " "
ElseIf usrnum(i) < 0 Then
totalstars(i) = " "
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
'End If
' End If
'End If
'End If
'End If
'End If
Next i
Clear()
'displays results of the bar chart to the user.
Do Until userchoice2 = "y" Or userchoice2 = "n"
'gives user an extra choice for displaying the bar chart
WriteLine("would you like your bar chart sorted into desending order?")
WriteLine("Y = yes")
WriteLine("N = no ")
userchoice2 = ReadLine()
If userchoice2 = "y" Then
Clear()
'sorts the stars and numbers out so they display in desending order, looks neater also for the user to read
Array.Sort(usrnum)
Array.Reverse(usrnum)
Array.Sort(totalstars)
Array.Reverse(totalstars)
ForegroundColor = ConsoleColor.Magenta
WriteLine("")
WriteLine("Values for the chart " & nchrt & " in decending order")
WriteLine("")
For i As Integer = 0 To usrnum.Length - 1
BackgroundColor = ConsoleColor.DarkMagenta
ForegroundColor = ConsoleColor.White
result1 = (totalstars(i))
result2 = (usrnum(i))
result2.ToString()
WriteLine(result1 & result2)
WriteLine("")
Next i
BackgroundColor = ConsoleColor.DarkBlue
ForegroundColor = ConsoleColor.White
WriteLine("")
WriteLine("Please note! Each * Is equal To 100.
Once a value reaches over 1000 the maxium amount Of stars shown Is 10 ")
Else
'displays results as they were typed in and does not sort them in decending order
If userchoice2 = "n" Then
Clear()
ForegroundColor = ConsoleColor.Magenta
WriteLine("")
WriteLine("Values for the chart " & nchrt)
WriteLine("")
For i As Integer = 0 To usrnum.Length - 1
BackgroundColor = ConsoleColor.Cyan
ForegroundColor = ConsoleColor.Black
result1 = (totalstars(i))
result2 = (usrnum(i))
result2.ToString()
WriteLine(result1 & result2)
WriteLine("")
Next i
BackgroundColor = ConsoleColor.DarkBlue
ForegroundColor = ConsoleColor.White
WriteLine("")
WriteLine("Please note! Each * Is equal To 100.
Once a value reaches over 1000 the maxium amount Of stars shown Is 10 ")
Else
If userchoice2 <> "n" Or userchoice2 <> "y" Then
WriteLine("Sorry that choice is not reconised, please enter a choice of y or n.")
End If
End If
End If
Loop
Else
Clear()
If userchoice < 1 Or userchoice > 2 Then
WriteLine("That value is unknown!")
WriteLine("Please enter a choice of 1 or 2 only. ")
WriteLine("Press enter to return to the main menu.")
ReadLine()
Clear()
Else
If userchoice = 2 Then
WriteLine("you sure you want to exit?")
End If
End If
End If
Loop
Do Until question1 = "y" Or question1 = "n"
'exit or return to main menu
ForegroundColor = ConsoleColor.Magenta
WriteLine(" ")
WriteLine(" ")
WriteLine(" ")
WriteLine("Would you Like To Exit the program?")
WriteLine("")
WriteLine("y = Exit")
WriteLine("n = return to the main menu")
Try
question1 = ReadLine()
Clear()
Catch ex As Exception
'if this produces an error, for example a string is entered by the user then the following message will be issued to the user
WriteLine("That value is unknown!")
WriteLine("Please enter a choice of y or n")
End Try
If question1 <> "y" Or question1 <> "n" Then
WriteLine("That value is unknown!")
WriteLine("Please enter a choice of y or n")
ReadLine()
Clear()
End If
Loop
Loop
End Sub
'function that is meant to calculate the largest value in the given usrnum() array however I can't get it to work at the moment :(
Public Function Max(ByVal size As Integer) As Integer
Dim usrnum(0) As Integer
usrnum(0) = size
Dim lstsize As Integer = usrnum.Length - 1 'how many items are in the list
ReDim usrnum(lstsize)
Dim i As Integer = 0 'counter to enable all the values within the array to be compared
Dim maxium As Integer
maxium = 0 'gives a value for all the values within the array to be compared to
Do While (i <= lstsize - 1) 'do while still within the size of the array - prevents it from checking values which do not exsist
If usrnum(i) > maxium Then
maxium = usrnum(i) ' if the current value is bigger than the current maxium then the new bigger value replaces the maxium
End If
i += 1 'enables the counter to work
Loop
Return maxium 'returns the final value of maxium
End Function
End Module
Upvotes: 2
Views: 108
Reputation: 38130
You can query the Console.WindowWidth
property, and adjust your output to fit.
Upvotes: 2