×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Data Type Mismatch in QBASIC

Data Type Mismatch in QBASIC

Data Type Mismatch in QBASIC

(OP)
Hi Mikrom,

Your help appreciated please.

I keep getting a TYPE MISMATCH Error on Line 14 ( Line No 240)
Can you help please.

100 REM 6N +- 1 Test V15.3
110 DIM P1 AS LONG
115 DIM P2 AS LONG
120 DIM N1 AS LONG
125 DIM N2 AS LONG
130 INPUT “Number “; N
140 IF N <= 0 THEN GOTO 999
150 N1 = N
160 N2 = N
200 P1 = (6 * N1) -1
210 FOR N1 = 2 TO SQR(P1)
220 IF P1 MOD N1 = 0 THEN PRINT USING “################”; P1; "is Composite": GOTO 300
230 NEXT N1
240 PRINT USING “################”; P1; " is Prime"
300 P2 = (6 * N2) +1
310 FOR N2 = 2 TO SQR(P2)
320 IF P2 MOD N2 = 0 THEN PRINT USING “################”; P2; "is Composite": GOTO 130
330 NEXT N2
340 PRINT USING “################”; P2; " is Prime"
350 GOTO 130
999 END

Pervez

RE: Data Type Mismatch in QBASIC

(OP)
Hi Mikrom,

Further to my post if I use PRINT instead of PRINT USING , the code works fine.
But, PRINT USING should allow me to display the output number in detail instead of in scientific exp notation.

Try the code with N = 3, 4, 5, 6, 59 and 288 to see how it works.

Pervez

RE: Data Type Mismatch in QBASIC

Hi pya,

it's because using one PRINT statement like this does not work

CODE

240 PRINT USING “################”; P1; " is Prime" 
Instead you have to do it with two prints like this:

CODE

240 Print Using "################"; P1,: Print " is Prime" 

RE: Data Type Mismatch in QBASIC

I modified your program source in the sense mentioned above and it seems to work:

CODE

100 Rem 6N +- 1 Test V15.3
110 Dim P1 As Long
115 Dim P2 As Long
120 Dim N1 As Long
125 Dim N2 As Long
130 Input "Number "; N
140 If N <= 0 Then GoTo 999
150 N1 = N
160 N2 = N
200 P1 = (6 * N1) - 1
210 For N1 = 2 To Sqr(P1)
220 If P1 Mod N1 = 0 Then Print Using "################"; P1,: Print " is Composite": GoTo 300
230 Next N1
240 Print Using "################"; P1,: Print " is Prime"
300 P2 = (6 * N2) + 1
310 For N2 = 2 To Sqr(P2)
320 If P2 Mod N2 = 0 Then Print Using "################"; P2,: Print " is Composite": GoTo 130
330 Next N2
340 Print Using "################"; P2,: Print " is Prime"
350 GoTo 130
999 End 

RE: Data Type Mismatch in QBASIC

(OP)
Hi Mikrom,

Thanx for the tip.
Code works for small numbers; I will now test for 10+ digit numbers.

One more question:

Is there a way of combining the 4 DIM statements into 1 line?
eg DIM P1, P2, N1, N2 AS LONG

Thanx

pya

RE: Data Type Mismatch in QBASIC

Yes, instead of four DIM statements

CODE

110 Dim P1 As Long
115 Dim P2 As Long
120 Dim N1 As Long
125 Dim N2 As Long 
you can use one DIM statement

CODE

110 Dim As Long P1, P2, N1, N2 

RE: Data Type Mismatch in QBASIC

(OP)
Hi Mikrom,

Thanx for your help.
The Code works, but ChatGPT is not impressed!

Can this code be improved V15.3
100 REM 6N +- 1 Test V15.3
110 DIM P1, P2, N1, N2 AS _Unsigned _Integer64
120 INPUT “Number “; N
130 IF N <= 0 THEN GOTO 999
140 N1 = N
150 N2 = N
200 P1 = (6 * N1) -1
210 FOR N1 = 2 TO SQR(P1)
220 IF P1 MOD N1 = 0 THEN PRINT USING “################”; P1, : PRINT "is Composite": GOTO 300
230 NEXT N1
240 PRINT USING “################”; P1, : PRINT " is Prime"
300 P2 = (6 * N2) +1
310 FOR N2 = 2 TO SQR(P2)
320 IF P2 MOD N2 = 0 THEN PRINT USING “################”; P2, : PRINT "is Composite": GOTO 120
330 NEXT N2
340 PRINT USING “################”; P2, : PRINT " is Prime"
350 GOTO 120
999 END

The code can be improved in the following ways:

Use meaningful variable names, such as number, prime1, and prime2 instead of N, P1, and P2.
Use proper indentation and spacing to make the code more readable.
Use a function to check for prime numbers instead of duplicating the code in lines 200-240 and 300-340.
Use a do-while or while loop to repeatedly prompt the user for input instead of using GOTO statements.
Consider using a more efficient algorithm for checking prime numbers, such as the Miller-Rabin primality test.
Remove unnecessary lines of code, such as line 110 and 120, instead of using _Unsigned _Integer64, you can use just integer or long.
Add proper error handling for negative input values.
Add comments throughout the code explaining what the code is doing.


I may give up coding and go back to singing!😒

pya

RE: Data Type Mismatch in QBASIC

Hi pya,
yes really, you can improve your code in way suggested by ChatGPT.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close