You are here

Data input validation

Forums: 

I am trying to get SB to complain about the wrong kind of data being input.

I am writing an assessment for my High School introductory programming class and am not super familiar with SB.

I want to modify this so that it will not accept anything but numbers in the .ReadNumber(). Currently it will continue through the program if the user enters the enter key. I want it to complain. Also, how do i get it to only accept numeric characters in the TextWindow.Read() At the moment if I enter a % in the middle of a name L%iz, it will accept it as valid.
Any suggestions? Thanks in advance for your help. Liz

'setting up the loop for going through the 3 year levels
For c = 1 To 3 Step 1

'setting up the loop for going through the 3 top students
For i=1 To 3 Step 1

'reading in student name the input from the keyboard
TextWindow.WriteLine("Enter student name")
student=TextWindow.Read()
'reading in student excellence credits the input from the keyboard
TextWindow.WriteLine("How many credits at Excellence - enter numbers only")
excellence_credits= TextWindow.ReadNumber()
'reading in student merit credits the input from the keyboard
TextWindow.WriteLine("How many credits at Merit - enter numbers only")
merit_credits= TextWindow.ReadNumber()
'reading in student achieved credits the input from the keyboard
TextWindow.WriteLine("How many credits at Achieved - enter numbers only")
achieved_credits= TextWindow.ReadNumber()

'calculating the result and assigning it to variable named result
result= (excellence_credits*3)+(merit_credits*2)+(achieved_credits*1)

'checking the students result with the highest result so far
'if students result it higher than the previous highest result, that student's result is
'reassigned to be the top_result and their name becomes the top_student
If (result > top_result) Then
top_student= student
top_result = result
EndIf

'ending the input for the 3 students
EndFor

'printing out the year levels top student and result
TextWindow.WriteLine("The top student for this year level is " + top_student)
TextWindow.WriteLine("Their score is " + top_result)
TextWindow.WriteLine("")

'reinitialising the top_result score so it is ready for next year level
top_result = 0
'ending the year level, going onto the next one
Endfor

Hi Liz,

Glad to hear you are doing programming at high school. The BASIC language is a great way to start learning programming. There are lots of interesting programming languages around and lots of different implementations of BASIC. The SmallBASIC project (this web site) began over ten years ago. The code you have submitted is for Microsoft "Small Basic" which started about 2 years ago (I think). If you would like to switch to SmallBASIC I'd be happy to help. Your program looks like this in SmallBASIC:

'setting up the loop for going through the 3 year levels
For c = 1 To 3 Step 1

'setting up the loop for going through the 3 top students
For i=1 To 3 Step 1

'reading in student name the input from the keyboard
print "Enter student name"
input student

'reading in student excellence credits the input from the keyboard
print "How many credits at Excellence - enter numbers only"
input excellence_credits

'reading in student merit credits the input from the keyboard
print "How many credits at Merit - enter numbers only"
input merit_credits

'reading in student achieved credits the input from the keyboard
print "How many credits at Achieved - enter numbers only"
input achieved_credits

'calculating the result and assigning it to variable named result
result= (excellence_credits*3)+(merit_credits*2)+(achieved_credits*1)

'checking the students result with the highest result so far
'if students result it higher than the previous highest result, that student's result is
'reassigned to be the top_result and their name becomes the top_student
If (result > top_result) Then
top_student= student
top_result = result
EndIf

'ending the input for the 3 students
next i

'printing out the year levels top student and result
print "The top student for this year level is " + top_student
print "Their score is " + top_result

'reinitialising the top_result score so it is ready for next year level
top_result = 0
'ending the year level, going onto the next one

next c

You could replace the "input" statement with a custom FUNC (function) which takes care of accepting and validating the user input. This could also provide a way for the user to exit your program.

I wondered. So I am NOT at the Microsoft Small Basic forum?? I wonder where I go for that? When I found this forum, it did my head in for a minute about whether we were doing Small Basic or SmallBasic. Seems like the naming is too close for it to be different.

We are coding in the Microsoft Small Basic at School. Seemed easiest for a taster for programming.

Thanks for your assistance. Do you know if the Microsoft Small Basic allows you to create a customer FUNC? or is it smallBASIC.
Liz

Hi Liz,

In SmallBASIC, "FUNC" is used to define a custom function (as apposed to a built-in function). "SUB" is similar - it defines a sub-routine but unlike FUNC it does not return a value. Also, you can declare one or more SUB/FUNC arguments to be "BYREF" (by-reference). Changes to BYREF arguments inside the SUB/FUNC also effect the value of variable passed when calling the routine. You can define local variables inside the SUB/FUNC using the "local" keyword.

I imagine "Small Basic" would have similar concepts since this is very fundamental to programming in general.