QBasic Nested for loop

Reply

Join Date: Apr 2004
Posts: 3
Reputation: krackhead is an unknown quantity at this point 
Solved Threads: 0
krackhead krackhead is offline Offline
Newbie Poster

QBasic Nested for loop

 
0
  #1
Apr 12th, 2004
im taking a qbasic class and am having a very hard time on this program...here is the question:
Using the ASCII table and the CHR$ function, write a program that prints the following output using a nested for next loop:
A
AB
ABC
ABCD
ABCDE

--------------------------------------------------------------------
heres what I have so far...i keep getting a "type mismatch":

CLS
FOR Outer = 1 TO 5 STEP 1
FOR in = CHR$(65) TO CHR$(70) <------TYPE MISMATCH?????
PRINT in;
NEXT in
PRINT
NEXT Outer

what am i doing wrong? any help on this would be greatly appreciated....thanks
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 26
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: need help please asap....very frustrated

 
0
  #2
Apr 15th, 2004
Originally Posted by krackhead
CLS
FOR Outer = 1 TO 5 STEP 1
FOR in = CHR$(65) TO CHR$(70) <------TYPE MISMATCH?????
PRINT in;
NEXT in
PRINT
NEXT Outer

what am i doing wrong? any help on this would be greatly appreciated....thanks
Well you are making it too difficult, the reason is write in front of you. CHR$(65) gives you what value?

...
Look at your ASCII Table if you don't know off hand.....

It is the Character A .

Now, look at your code. For in = CHR$(65) Stop right there!
If CHR$(65) gives a value of A and in is no doubt an integer value this is where you mismatch starts. You care comparing a Character String (albiet one character) to an Integer! So it unfortunately won't work. Can only compare like datatypes.

So some Conversion will be needed and I think you need to check your logic for this code....

Hope this helps.... If not let me know if you need more help!
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 3
Reputation: krackhead is an unknown quantity at this point 
Solved Threads: 0
krackhead krackhead is offline Offline
Newbie Poster

Re: QBasic Nested for loop

 
0
  #3
Apr 22nd, 2004
I sill don't quite get it. Every time I try and switch it up to change values I get another error. I know im close but just cant figure it out. any suggestions?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 762
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: QBasic Nested for loop

 
0
  #4
Apr 27th, 2004
first of all, you don't need STEP, because the default step value is always 1. Secondly, A FOR statement is looking for an integer. CHR$() returns a character, which is not a number. Leave the CHR$() out of the FOR header. Look at the example below.
FOR outer = 1 to 5
	FOR in = 1 to outer
		PRINT CHR$(in+64)
	NEXT in
NEXT outer

Now lets run through the code step by step. First time through, "outer" will equal 1. So the following statement would actually be "FOR in = 1 to 1". "in" will equal 1 as well. Next line "PRINT CHR$(in+64)" This will be the same as "PRINT CHR$(65)" because "in" is 1. Now the loops starts back from the top. This time, "outer" will equal 2. So therefore, the following FOR statement would really mean "FOR in = 1 to 2". So that FOR loop will run through twice. The first time "in" being 1 and the second time "in" being 2. Which would print CHR$(1+64) and CHR$(2+64). And the pattern repeats until "outer" finally equals 5. Hope this helped you a little.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 3
Reputation: sgtfubar is an unknown quantity at this point 
Solved Threads: 0
sgtfubar sgtfubar is offline Offline
Newbie Poster

Re: QBasic Nested for loop

 
0
  #5
Sep 30th, 2004
I just had a problem similar to this one. But I ended up making it harder than it really was. It all clicked when I figured out FOR is looking for integers instea of characters. Here's what I did:

CLS

FOR num = 32 TO 255
PRINT CHR$(num)
NEXT num

END


It can't get more simple than that.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 869
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: QBasic Nested for loop

 
0
  #6
Oct 6th, 2004
The code below should work if Qbasic works at all like BCX:

dim inner, outer

FOR outer = 1 to 5
FOR inner = 1 to outer
PRINT CHR$(inner + 64); ' stay on line
NEXT innner
PRINT ' new line feed
NEXT outer
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 3
Reputation: krackhead is an unknown quantity at this point 
Solved Threads: 0
krackhead krackhead is offline Offline
Newbie Poster

Re: QBasic Nested for loop

 
0
  #7
Oct 6th, 2004
Originally Posted by vegaseat
The code below should work if Qbasic works at all like BCX:

dim inner, outer

FOR outer = 1 to 5
FOR inner = 1 to outer
PRINT CHR$(inner + 64); ' stay on line
NEXT innner
PRINT ' new line feed
NEXT outer
Well I do appreciate your help, but I took this class back in April and this is no longer useful to me. I got a B+........thanks anyway
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC