Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: hakeem on August 19, 2004, 05:51:06 AM



Title: Need help please!!!!
Post by: hakeem on August 19, 2004, 05:51:06 AM
I have to write a program where say if  I input 1 then it prints sunday, and if I input 2 then it prints monday and so on....

any help would be apreciated... :wink:


Title: Need help please!!!!
Post by: KiZ on August 19, 2004, 05:58:44 AM
Have you tried yet? If so, lets see some code. We wont just do your homework for you ;)


Title: Need help please!!!!
Post by: citpes on August 19, 2004, 05:59:01 AM
HAHAHAHAHAHAHA :rotfl:  :rotfl:  :rotfl:

just give me a min to compose myself again...
..
..

ok, sorry bout that.

hakeen you read the rules?  This is obviously a homework assingment.  Try listen in class and it would be easy.  You might get lucky and have a bored member here write this but it aint gonna be me.

heres a tip though, look up INPUT and IF THEN ELSE.  there are a span of tuts on the net (even on this very site (WOW :roll: )!)

EDIT: 1 min, damn you dark!


Title: Need help please!!!!
Post by: KiZ on August 19, 2004, 06:56:38 AM
Quote from: "citpes"
EDIT: 1 min, damn you dark!

Heh ;D

But actually, I dont think he was trying to cover the fact that its a homework assignment.


Title: Need help please!!!!
Post by: citpes on August 19, 2004, 07:14:02 AM
would it even be possible to cover that up?

But, yea, hakeem, just look up those commands i gave you in my first post.  Qb's help is actually quite good, just type in the word and hit F1 when the cursor is on it.  maybe you can get the basic idea, then post what you have and you will find that the ppl here are actually really nice.


Title: Need help please!!!!
Post by: hakeem on August 19, 2004, 10:54:37 AM
Allright boys..
have a laugh,
you got me there :lol:
the honest fact is that not all of us here are blessed with computer.
Well anyways i got a bloke here helping me out with it, :wink:  
thanks for tips anyways
Hakeem 8)


Title: Need help please!!!!
Post by: HQSneaker on August 19, 2004, 11:11:07 AM
You should use SELECT CASE for this one, much better than IF


Title: Need help please!!!!
Post by: Z!re on August 19, 2004, 08:54:40 PM
*sigh*

No, SELECT CASE is easier to read, FOR SOME people.

IF is faster.


I posted a thread about it some time ago, called optimization or something...


Title: Need help please!!!!
Post by: whitetiger0990 on August 19, 2004, 09:34:12 PM
Allright boys? Are you assuming that only boys can program? ahh

anyways. I got bored and made a  program like this. and guess what! it doesn't use IF or SELECT! mwahaha
[syntax="QBASIC"]CLS
days$ = "Sunday   Monday   Tueday   WednesdayThursday Friday   Saturday "
lod = 9
INPUT "Gimme a number! ", num
num = ((num < 1) * -1) + num
PRINT RTRIM$(MID$(days$, lod * (num - 1) + 1, lod))[/syntax]

EDIT: actualyl I made a program that you input your name and it writes it to a file and says "welcome back" and stuff. but I trimmed it down =D

EDIT2: you can now enter a number less then 1 and it makes it 1.


Title: Need help please!!!!
Post by: KiZ on August 20, 2004, 05:00:41 AM
Quote from: "Z!re"
*sigh*

No, SELECT CASE is easier to read, FOR SOME people.

IF is faster.


I posted a thread about it some time ago, called optimization or something...


psst... Z!re... I dont think that 1/10000s of a second faster speed is going to matter to a guy who is new to programming and only doing it because its school work. ;D

Quote
guess what! it doesn't use IF or SELECT! mwahaha

O_o    ooooh. Ingenious


Title: Need help please!!!!
Post by: ak00ma on August 20, 2004, 06:12:42 AM
Quote from: "whitetiger0990"
Allright boys? Are you assuming that only boys can program? ahh

anyways. I got bored and made a  program like this. and guess what! it doesn't use IF or SELECT! mwahaha
[syntax="QBASIC"]CLS
days$ = "Sunday   Monday   Tueday   WednesdayThursday Friday   Saturday "
lod = 9
INPUT "Gimme a number! ", num
num = ((num < 1) * -1) + num
PRINT RTRIM$(MID$(days$, lod * (num - 1) + 1, lod))[/syntax]

EDIT: actualyl I made a program that you input your name and it writes it to a file and says "welcome back" and stuff. but I trimmed it down =D

EDIT2: you can now enter a number less then 1 and it makes it 1.


Not bad ;)
But didn't you think of arrays?
Code:

DIM Days$(7)
Days$(0) = "Sunday"
Days$(1) = "Monday"
Days$(2) = "Tuesday"
Days$(3) = "Wednesday"
Days$(4) = "Thursday"
Days$(5) = "Friday"
Days$(6) = "Saturday"
INPUT "Please enter a number (between 0 and 6)", n
n = n MOD 8 'If there's someone who doesn't know how to read!!
PRINT STR$(n) + "-->" + Days$(n)


Title: Need help please!!!!
Post by: Z!re on August 20, 2004, 08:21:42 AM
I thought of arrays, but didn't post, as this is a school assigment...

doh...


Title: Need help please!!!!
Post by: whitetiger0990 on August 20, 2004, 01:37:32 PM
Yeah I thought of arrays. But Mine takes up less room =P
I don't know if it's faster though =\


Title: Need help please!!!!
Post by: Z!re on August 20, 2004, 01:41:08 PM
prolly not, but i dunno...

Dosent matter much, lol =)

The fastest would prolly be:

Code:
if day = 1 then print "monday": end
if day = 2 then print "tuesday": end
if day = 3 then print "wednesday": end
if day = 4 then print "thursday": end
if day = 5 then print "friday": end
if day = 6 then print "saturday": end
if day = 7 then print "sunday": end


Title: Need help please!!!!
Post by: ak00ma on August 20, 2004, 05:38:44 PM
Code:

DIM D$(7)
D$(0) = "Sunday":D$(1) = "Monday":D$(2) = "Tuesday":D$(3) = "Wednesday":D$(4) = "Thursday"
D$(5) = "Friday":D$(6) = "Saturday"
INPUT "Please enter a number (between 0 and 6)", n
PRINT STR$(n MOD 8) + "-->" + D$(n MOD 8)

;)


Title: Need help please!!!!
Post by: whitetiger0990 on August 20, 2004, 05:53:29 PM
Don't you mean : instead of ;?


Title: Need help please!!!!
Post by: ak00ma on August 20, 2004, 06:12:05 PM
Ouups....yeah,yeah....too much C#, too less QB... ;)


Title: Need help please!!!!
Post by: Moneo on August 21, 2004, 09:28:52 PM
WHITEY:
I like your solution, except the boolean attempt to "fix" invalid input numbers. It may work for negative numbers, but it won't work if you input a number 8 and above. What's wrong with:
Code:

if n < 1 or n > 7 then print "Sorry, invalid weekday number.":system


ZIRE:
I like your totally strightforward and straight-line code.

AK00MA:
Your array solution is nice, but:

1) I don't like the 0 to 6 range. It could be confusing to a user. In the US, since the week begins on Sunday, then 1 to 7 is Sunday to Saturday. Some other countries begin the week on Monday. In either case, the weekday numbers are 1 to 7.

2) Your use of the "n mod 8" is cute, but it doesn't solve the problem of invalid input numbers, it only converts it to something else. See my sample number validation code above.

IN GENERAL: If you post a solution for a newbie, give it your best shot, and most of all test it first. Some poor newbie may inherit some of your poorly written or buggy code.
*****


Title: Need help please!!!!
Post by: whitetiger0990 on August 21, 2004, 10:04:19 PM
Moneo: I didn't want to use IF =P I was going to do something with >7 but got lazy.


Title: Need help please!!!!
Post by: Moneo on August 21, 2004, 10:14:41 PM
Ok Whitey, we'll forgive you this time. :wink:


Title: Need help please!!!!
Post by: whitetiger0990 on August 21, 2004, 10:18:40 PM
Quote from: "Moneo"
Ok Whitey, we'll forgive you this time. :wink:

Hey... What's that wink for?  :roll:


Title: Need help please!!!!
Post by: frostyservant on August 22, 2004, 05:51:07 AM
Or...
I'm pretty n00bish myself, so forgive the  :barf:  style (especially the nasty goto loop... it's functional)...


Code:
TYPE daysofweek
name AS STRING * 9
END TYPE
DIM day AS daysofweek

' You would have to write the database entries beforehand,
' obviously...

OPEN "daysofweek.dat" FOR RANDOM AS #1 LEN = LEN(day)
recording:
INPUT "Number of day of week"; record
IF record < 1 OR record > 7 then goto recording
GET 1, record, day
PRINT "day.name"
CLOSE
END


Title: Need help please!!!!
Post by: na_th_an on August 22, 2004, 08:44:06 AM
Code:
DO
   INPUT "Number of day of week"; record
LOOP WHILE record < 1 OR record > 7


;)


Title: Need help please!!!!
Post by: frostyservant on August 22, 2004, 09:49:26 AM
:P

Yeah, I know, I know...


Title: Need help please!!!!
Post by: Z!re on August 22, 2004, 10:41:24 AM
Code:
INPUT "Number of day of week"; record
record=((record*2)\2) MOD 8


or

Code:
INPUT "Number of day of week"; record
record=ABS(record) MOD 8


lol....


Title: Need help please!!!!
Post by: Moneo on August 22, 2004, 09:10:53 PM
Quote from: "na_th_an"
Code:
DO
   INPUT "Number of day of week"; record
LOOP WHILE record < 1 OR record > 7


;)


As usual, Nathan, a brilliant, stuctured solution. My problem is that I recognize the good solution, but I still code the old way with IFs.
*****


Title: Need help please!!!!
Post by: Moneo on August 22, 2004, 09:19:24 PM
Zire,

Your use of the MOD 8 is an attempt to force invalid input to a valid number. That's a wrong approach. If the user inputs an invalid number you have to inform him either with a message, or as Nathan does, by not accepting the input.

Besides, the MOD 8 will give you zero if the input is an 8 or other multiple of 8. This zero will certainly cause an error in the program or give garbage as a result.

Input has to be scrubbed, not forced to an acceptable value.

Do you get what I mean?
*****


Title: Need help please!!!!
Post by: Z!re on August 22, 2004, 11:27:38 PM
Yup...

I'm mostly messing around :P

I still think my if's are the best solution, simple, functional, easy to understand.

Could even add a print after all the if's saying something like:
"You messed up!"

=)



BTW, it's Z!re...


Title: Need help please!!!!
Post by: ak00ma on August 23, 2004, 09:29:50 AM
ok...I got it...

Nath's way:
Code:

DIM D$(1 TO 7)
D$(1) = "Sunday":D$(2) = "Monday":D$(3) = "Tuesday":D$(4) = "Wednesday":D$(5) = "Thursday"
D$(6) = "Friday":D$(7) = "Saturday"
DO
 INPUT "Please enter a number (between 1 and 7)", n
LOOP WHILE n < 1 AND n > 7
PRINT STR$(n) + "-->" + D$(n)


Title: Need help please!!!!
Post by: KiZ on August 23, 2004, 02:25:26 PM
Gah! The ages of you being away from you shows ak00ma! You forgot the loop. =P

But i guess that was just a daft mistake..


Title: Need help please!!!!
Post by: ak00ma on August 23, 2004, 03:14:32 PM
Oh,..., what should I say? ... I just forgot it....Stupid me....Uuuuups...
Take one that makes you happy ;)
(I HAVE BEEN GONE FOR TOO LONG...REALLY)

BTW,..., another way:
Code:

DO
 INPUT "Please input a number (between 1 and 7)", n
LOOP WHILE n < 1 OR n > 7
FOR i = 1 TO n
 READ Day$
NEXT i
PRINT STR$(n) + "-->" + Day$
DATA "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"

Ok,ok,..., it's longer...


Title: Need help please!!!!
Post by: whitetiger0990 on August 23, 2004, 03:22:14 PM
Stop editing your post. I saw you make that mistake!


Title: Need help please!!!!
Post by: ak00ma on August 23, 2004, 04:30:19 PM
In my last post I said that I HAD MADE a mistake
Quote from: "ak00ma"

Oh,..., what should I say? ... I just forgot it....Stupid me....Uuuuups...
Take one that makes you happy  
(I HAVE BEEN GONE FOR TOO LONG...REALLY)

... so I edited the code so that it is right...what's wrong with that?


Title: Need help please!!!!
Post by: whitetiger0990 on August 23, 2004, 05:40:06 PM
Nothing  :roll:


Title: Need help please!!!!
Post by: ak00ma on August 24, 2004, 06:23:35 AM
ok...then everything is just fine.... :roll: