0

I have code for a number generator, that randomly selects numbers between 1 and 30. I want to use a batch file to make the dinner selections for each week so I don't have to do it myself. My problem is that I don't know how to assign the dinner names to the numbers, then i want it to check if the dinners meet a 'dinner rating', this makes sure that the dinner is healthy enough, if it doesn't meet the rating then the code will/should make it re-pick the dinners. This is the number generator: (don't tell me that i could do it a better way, I'm sort of a noob)

@echo off
echo.
echo             Welcome %username%
ping localhost -n 2 >nul
echo        This Is The Dinner Selector.
ping localhost -n 2 >nul
echo This Program Will Select The Weekly Dinner.
ping localhost -n 2 >nul
set /a selection1=%random% %%30 +1
set /a selection2=%random% %%30 +1
set /a selection3=%random% %%30 +1
set /a selection4=%random% %%30 +1
set /a selection5=%random% %%30 +1
echo %selection1%
echo %selection2%
echo %selection3%
echo %selection4%
echo %selection5%

Please help me find a solution to my problem, i am bored of picking healthy dinners by my self.

user2975367
  • 917
  • 1
  • 7
  • 7

2 Answers2

0

Ok, not entirely sure what you want, but this may be it:

Main.bat

@echo off
setlocal Enabledelayedexpansion
echo.
echo             Welcome %username%
sleep 2
echo        This Is The Dinner Selector.
sleep 2
echo This Program Will Select The Weekly Dinner.
sleep 2
Echo.
Echo.
Echo How healthy do you want your food [0 - Anything :: 9 - max]
choice /c "1234567890" /n /m ": " /d 0 /t 20
set rate=%errorlevel%
set /a count=0
set /a total=0
for /f %%a in (Dinner.txt) do (set /a total+=1)


:: START LOOP ----------------------
:choose
set /a count+=1
:random
set /a process=0
set sel=
set /a sel=%random% %% %total% + 1

for /f "tokens=1,2 delims=:" %%a in (Dinner.txt) do (
    set /a process+=1
    if !process! EQU !sel! (
        if %%b GEQ !rate! (
            set sel=%%a
            )
        )
    )

if "%sel%" EQU "" (Echo %sel%
) Else (
goto :random)

if count leq 5 goto choose
:: END LOOP   ----------------------

cls
Echo Enjoy your meal!
sleep 3
Exit

Note this can result in repetitions and/or infinite/really-long loops if the health condition is high.

Dinner.txt

This is a list of all your choices. it can be as long/short as you want as the batch file adapts to its size.

In there are a total of 5 choices

; This is how you format the dinner file: [Food]:[Healthyness 0 - 9]
; Lines starting with semi-colons are ignored (if you want a blank line comment it)
; Examples:
;
; Vegetarian
Salad:9
Pasta:8
;
;
; Non-Veg
Uncooked Meat:2
Pizza:5
Burger:6
Pure Oil:0

i'm yet to test this, and can also guarantee there will be an error since it is quite mundane and long, but i'm sure a bit of error-testing will do the job

Mona.

Community
  • 1
  • 1
Monacraft
  • 6,510
  • 2
  • 17
  • 29
  • can i just put any meals i want and give it a rating such as `Pasta:6` then the batch file will adapt, is that what your saying? – user2975367 Nov 12 '13 at 21:01
  • when it asks me how healthy i want it, i put in 5 (for example) then it just waits. You try to run the program on your computer and see if it does it to yours because im bummed – user2975367 Nov 12 '13 at 21:11
  • Give me a few hours and I'll see whats the problem. – Monacraft Nov 12 '13 at 21:37
  • Also, the batch file will adapt not to what ratings you give it, but on how many food items you have in the list, not necesarily 30. – Monacraft Nov 12 '13 at 21:38
0

The way to manage several elements of same type, each one identified by an index, is via an array. I strongly suggest you to learn about this topic, and also about Delayed Expansion of environment variables, before you try to use arrays in Batch. Once you get the basics, you may do something like this:

set dinner[1]=Name of first dinner
set dinner[2]=Name of second dinner
set dinner[3]=Name of third dinner, etc

Or, in a more advanced way:

set i=0
for %%a in (Pasta:9 Dessert:5 Soup:8) do (
   for /F "tokens=1,2 delims=:" %%b in ("%%a") do (
      set /A i+=1
      set dinner[!i!]=%%b
      set rating[!i!]=%%c
   )
)

For example:

set /A selection=%random% %% 30 + 1
if !rating[%selection%]! gtr 7 echo Rating of !dinner[%selection%]! correct: greater than 7
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108