• Status: Solved
• Priority: Medium
• Security: Public
• Views: 635

# Input validation in Qbasic

Dear Programmers

Could you please supply me with a piece of code in QBasic to answer the following problem:

Declare an array N which contains 30 cells of integer type and prompt the user to read in 30 integers between 0 and hundred.

Validity of input between 0 and 100 is important.
0
cybernursery
1 Solution

Commented:
Homework?
0

Author Commented:
50 DIM N(30)
100 For I= 1 to 30
200 Input M(I)
300 If M(I)>=30 AND M(I)<=100 THEN 500
400 Print "Out of Range"
500 Next I

Well this is my piece of code, is it correct?
0

Commented:
Hi,

I haven't used QBasic for many years, however your code seeems more of a GS Basic type. I'd try:

Option Base 1
DIM N(30) as Integer
For I= 1 to 30
Input M
If M>0 Or M<100 THEN
Print "Out of Range"
Else
N(i)=M
End If
Next I

Enjoy

0

Commented:
GW Basic, not GS
0

Commented:
> between 0 and 100 is important

IMO that means 0 is invalid as is 100.

So,,, I'd say any two digit number is valid.
Better to be positive, than to be censor

Any one digit number is also OK

0 isn't really a number, you can't hold it or add it to anything to generate something creatively new

(or can you?)
0

Commented:
>>     "300 If M(I)>=30 AND M(I)<=100 THEN 500"

This is really simple enough to test, something all programmers should do.

It has been awhile, but as I recalll, basic permits entry for non-positive integers such as -1 and 0

>> "input between 0 ..."

as such, input of 1 to 29 should succeed and be permitted. Have you tried?

>>  ' If M>0 Or M<100 THEN Print "Out of Range"   '

It has been awhile for my tired eyes, but I'd revisit the directions of angle brackets if the sample inputs misbehave on the output end

Or switch logic, and only print out the good ones. With only 30 inbound, that won't be too many lines
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.