VBScript Problem

Permabanned
Joined
21 Nov 2010
Posts
2,315
Location
Newton Aycliffe
Ok so im having some problems trying to get my code working at current, im restricted in what i can use so i have to stick to the outline in this code (No arrays).

Its a script that accepts numbers till a negative number is input but im struggling getting the values input from the user, in a string format to an integer, because i need to sum and average them later.

This is what im working off right now.

Option Explicit

dim userinput,overall, numberofnumbers

Do

WScript.StdOut.Write "Input a number: "
userinput = WScript.StdIn.ReadLine

overall = overall + userinput

numberofnumbers = numberofnumbers + 1

loop until (userinput < 0)

overall = CInt(overall)

WScript.StdOut.Writeline "Amount of numbers entered: " & numberofnumbers

WScript.StdOut.Writeline "Sum of numbers entered: " & overall
WScript.StdOut.Writeline "Average of numbers: " & (overall/numberofnumbers)

If anyone can think of a better way let me know, but as i said im restricted to certain variables and commands currently.
 
Last edited:
I hate VBS compared to PowerShell these days, if you could write it in PowerShell then I would tbh! :)

Your Write commands don't work for me, gives me a handle error, so I had to use Wscript.echo instead.

My code checks if the number is numeric, and then if it's above 0 it processes it, otherwise it keeps asking until a number is entered which is below 0.

Code:
Option Explicit

dim userinput, overall, numberofnumbers, avgnumber

numberofnumbers = 0
userinput = 0
overall = 0
avgnumber = 0

Do while userinput >= 0

	userinput = InputBox("Input a Number")
	
	if IsNumeric(userinput) then
		
		if userinput >= 0 then
		
			numberofnumbers = numberofnumbers + 1
			overall = overall + userinput
			
		End if
		
	else
	
		userinput = 0
	
	End if

loop

avgnumber = overall / numberofnumbers

WScript.echo "Amount of numbers entered : " & numberofnumbers
WScript.echo "Sum of numbers entered : " & overall
WScript.echo "Average number : " & avgnumber
 
Thats exactly how i planned to do it, but its throwing up mismatch errors when inputting the negative number. Does the above work for you?

Sadly it must be VBS

Edit: nevermind youve done it with inputbox and i was trying to run in console, will report back in a sec :P
 
Tested as working here dude, if i put in a text string that is not a number it simply ignores it. I got around that by checking if the input is a number or not, if it wasn't I set the userinput variable back to 0 so it would keep the loop going.
 
Yep your correct works perfectly, some reason i thought i needed to display the numbers entered back, when infact i didnt, and thus got the fact i needed to use a string when i did not in my head!

Thanks.
 
Back
Top Bottom