My vbs script hell

Status
Not open for further replies.

mikesx4911

In Runtime
Messages
356
Location
chicago, IL
Well this week my class got thrown into the deep end and its either sink or swim and I'm confused as hell still I do not get most of this. Here is what I have so far

Objective
In this lab, students will complete the following objectives:

Create a VBScript Program using NotePad++.
Access a two-dimensional array
Use a Nested For Loop to display array contents
Use Do/While Loops for Error-Handling
Use CStr( ), CInt( ) and chr( ) functions


Task 1: Open IP_Array_start.vbs in NotePad++

Download the file IP_Array_start.zip from Doc Sharing in eCollege. Extract the file IP_Array_start.vbs and open it in NotePad++.

Modify the Programmer Header as needed. and Save As the file with the new name of IP_Array.vbs.

The line dim ipAddress(5,3) declares a 6x4 2-dimensional array. The 5 and 3 give the maximum index value. Since indices always start at 0, this is 6x4 array.

The lines that follow initilize the array locations with IP addresses. The first index (0..5) represents the rooms 100 through 105. The second index (0..3) represent the 4 computers in each room.

The IP address of the 3rd computer in room 104 can be found in the array element or component ipAddress(4,2). This value is “192.168.10.45”. Look at the array carefully to determine the meaning of the index values.

Now that you have an initialized 2-dimensional array, it will be your job to query the array for the IP address for any computer (1-4) in any room (100-105).







Task 2: Define Program Variables and Query Array for IP Addresses


Let's start by defining our program variables. We need two sets of variables: string variables : roomStr and compStr and number variables: room and computer. We string variables are needed for input and the number variables are used as indices for the ipAddress array.



Prompt the user for the room number (100-105). We need to make sure that the user doesn't enter a value less than 100 or greater than 105. We will use a Do/Loop/While loop to error-handle this value. Look at the Pseudocode shown below and write the required VBScript statement that implement the Pseudocode task. Real code that is correct as written will be in bold.

Do
Display Prompt “Please Enter the Room Number (100-105) ...... “
Get StdIn Console Input and assign string value to roomStr
room = CInt(roomStr)

If room < 100 OR room > 105
Use StdOut to Beep Speaker twice with chr(7)
Display ErrMsg “Error, 100 to 105 Only!!!”
endif
Loop While room < 100 OR room > 105

Now we prompt the user for the computer number (1-4). We need to make sure that the user doesn't enter a value less than 1 or greater than 4. We will again use a Do/Loop/While loop to error-handle this value. Look at the Pseudocode shown below and write the required VBScript statement that implement this Pseudocode task. Real code that is correct as written will be in bold.

Do
Display Prompt “Please Enter the Computer Number (1-4) ...... “
Get StdIn Console Input and assign string value to compStr
computer = CInt(compStr)

If computer < 1 OR computer > 4
Use StdOut to Beep Speaker twice with chr(7)
Display ErrMsg “Error, 1 to 4 Only!!!”
endif
Loop While computer < 1 OR computer > 4



Now that we have a valid room number 100-105 and a valid computer number 1-4, let's display the IP address for the specified room and computer as follows:

Display the message “ The IP Address in Room ### for computer # is ###.###.##.##:


Note: For room 102, Computer 2, the IP Address is found at ipAddress(2,1). How do you convert room 102 to index 2 and Computer 2 to index 1?


This is my script so far :

' VBScript: IP_Array_start.vbs
' Written by: Michael Ikezoe
' Date: Today's Date
' Class: COMP230
' Professor: Professor Name
' ===================================
' This initialize a 2-dimension array
' of IP Address. The first index +100
' is the room# and the second index+1
' is the computer# in the room.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"

' Define Script Variables
roomStr=""
compStr=""
room=0
computer=0

Do

Loop While room <100 Or room > 105


Here is what I have so far

' VBScript: IP_Array_start.vbs
' Written by: Michael Ikezoe
' Date: Today's Date
' Class: COMP230
' Professor: Professor Name
' ===================================
' This initialize a 2-dimension array
' of IP Address. The first index +100
' is the room# and the second index+1
' is the computer# in the room.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"

' Define Script Variables
roomStr=""
compStr=""
room=105
computer=3

Do


Loop While room <100 Or room > 105


My first question is when defining a variable above should it be if I want to use room 105 computer 3 IP:192.168.10.54

' Define Script Variables
roomStr=" ipAddress(5,3)"
compStr="192.168.10.54"
room=105
computer=3

I'm a little confused as to wth this really means, whats the difference betwee the str and the bottom part that just says room and computer.
 
Note: For room 102, Computer 2, the IP Address is found at ipAddress(2,1). How do you convert room 102 to index 2 and Computer 2 to index 1?

The above line is the key. You need to do some simple math on the room number and the computer number the user enters to convert those values into the appropriate index values in order to retrieve and display the IP address for that computer from the array. The indexes for the room numbers are 0-5 and the indexes for the computers are 0-4.

The string variables contain the text that you want to display while the numeric values are used for the indexes. Most computer languages cannot directly display a numeric value so those values usually need to be converted to string (text) to be able to be printed or displayed on the screen.
 
ok this is what I have, how does this look?

' VBScript: IP_Array.vbs
' Written by:
' Date: 1/22/2012
' Class:
' Professor:
' ===================================
' This initialize a 2-dimension array
' of IP Address. The first index +100
' is the room# and the second index+1
' is the computer# in the room.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"

' Define program variables
roomStr=""
compStr=""
room=0
computer=0

Do
WScript.Echo "Please Enter the Room Number (100-105) ...... "
roomStr = WScript.StdIn.ReadLine
If IsNumeric(roomStr) Then
If (roomStr >= 100) And (roomStr <= 105) Then Exit Do
End If

WScript.Echo Chr(7)
WScript.Echo Chr(7)
WScript.Echo "Error, 100 to 105 Only!!!"
Loop

Do
WScript.Echo "Please Enter the Computer Number (1-4) ......"
compStr = Wscript.StdIn.ReadLine
If IsNumeric(compStr) Then
If (compStr >= 1) And (compStr <=4) Then Exit Do
End If

WScript.Echo Chr(7)
WScript.Echo Chr(7)
Wscript.Echo "Error, 1 to 4 Only!!!"
Loop

'Display All IP Address Y/N?
Do
WScript.StdOut.Write ("Do you wish to Display all of the IP Address (Y/N)......")
ans = WScript.StdIn.Readline()
If ans = "N" or ans = "n" then
WScript.StdOut.Write chr (7)
WScript.StdOut.Write chr (7)
Wscript.StdErr.Write "Error,Y,y,N,n Response Only!!!"
End if

While ans = "N" or ans = "n"

If ans = "Y" or ans = "y" then
For room = 0 to 5
For computer = 0 to 3
WScript.Echo "The IP Address in Room" + room+100
'WScript.Echo "for Computer" (computer+1)
'WScript.Echo "is" + IPAddress(Room, computer)
 
Your For loop at the end would list ALL IP addresses in the array, if that's what you want to do, why prompt the user for room # and computer #?

Check your math. You are adding 100 to the room number. If the inputted room number was 101, what would be the result of adding 100 to it? Also, if the computer number inputted was 4, what would be the result of adding 1 to it? Remember that the array indexes are 0 thru 5 for the room numbers and 0 thru 3 for the computers. Your math needs to convert room # such as 104 into the appropriate index into the array.
 
Status
Not open for further replies.
Back
Top Bottom