|
Post by Valerie on Jul 6, 2002 5:48:07 GMT
Hi Everyone, I am very new to QBasic & I need help writing this program. It is for calculating the number of Calories in a 1 ounce serving of food. There has to be 2 Sub programs, one for Input & one for the Output, and one Function. Simple commands, print, let, input. I've got it all working except for the Output. I can't get it to give the Output. If I put the last Print statement into the main program, it works, but I need it in the Sub program. Here is what I have:
CLS LET carb = 0 LET fat = 0 LET prot = 0
CALL GetInputFromUser(carb, fat, prot)
CALL DisplayResult(t) END
SUB GetInputFromUser (carb, fat, prot)
INPUT "Enter Total number of Carbohydrates"; carb INPUT "Enter Total number of Fat"; fat INPUT "Enter Total number of Protein"; prot
END SUB
FUNCTION MultiplyCFPByConstants (carb, fat, prot)
LET carbConst = 4 LET fatConst = 9 LET protConst = 4
MultiplyCFPByConstants = (carb * carbConst) + (fat * fatConst) + (prot * protConst)
END FUNCTION
SUB DisplayResult (t)
PRINT "This is your total number of Calories: "; PRINT MultiplyCFPByConstants(carb, fat, prot) END SUB
Any help would be greatly appreciated and ASAP. Thanks....Valerie
P.S. I tried to sign up 3 times, but it kept giving me errors..Val
|
|
|
Post by pebe2 on Jul 6, 2002 19:29:56 GMT
Hi Valerie, First, you don't need the LET statement in QBasic. carb = 0 is good enough.
Your error is in calling DisplayResult. You need to carry over the required parameters into the sub so the Function can read them.
ie. DisplayResult(carb,fat,prot), not the (t) that you have got.
|
|
|
Post by Valerie on Jul 7, 2002 5:15:55 GMT
PeBe2, Thanks tons. I can't believe I didn't notice that. I think I changed everything else, but that. You are a "Grade Enhancer". My GPA thanks you.... Seriously though, much appreciated.....Valerie
|
|
Ziggi
Full Member
Posts: 87
|
Post by Ziggi on Jul 7, 2002 21:43:24 GMT
Valerie.
You can expand your program by puting DATA for some food like: ----------------------------------------------------------- FOR food = 1 TO 3 READ name$, carb, fat, prot NEXT food
'Data in here is programming example don't use it for diet.
'Food name, carb, fat, prot
DATA Ham, 5, 30, 200 DATA Pizza, 100, 30, 40 DATA Apple, 300, 4, 10 ----------------------------------------------------------- OR in the file ------------------------------------------ fr = FREEFILE OPEN "food.txt" FOR INPUT AS# fr DO WHILE NOT EOF(fr) INPUT #fr,name$,carb, fat, prot LOOP COLSE fr ------------------------------------------ use EDIT.EXE or NOTPAD.EXE and type in your data:
Ham, 5, 30, 200 Pizza, 100, 30, 40 Apple, 300, 4, 10
|
|