PDA

View Full Version : help!



ic3_man
04-12-2005, 05:12 PM
I am a complete newbie to the world of programming and I need some help in the creation of this program. This is a homework assignment, I just need someone to tell me what to put in the procedures part because it seems like he wants most of the commands in the main program and there;s nothing left for the procedures. Unless I'm looking at this the wrong way and he posted the procedure instead and it's not the main program...please help!!!

program evenGame;
var bet: integer; { the amount bet by the user }
amount: integer; { the amount won or lost }
num: integer; { the random number }
win: boolean; { win = true or false }

{ insert your "even" procedure here }

begin
write('How much would you like to bet? ');
readln(bet);

{ Add 2 lines here to }
{ create a random number, 1 to 36 }

even(num,bet,win,amount);

writeln('The number is: ', num);

{ Add an If Statement which uses writeln statements }
{ to print a winning or losing message. }
{ Hint: test the value of the "win" variable. }

end.

BBCode pascal tags added by moderator during clean-up.

ic3_man
04-12-2005, 07:47 PM
alright it turns out that it was the beginning of a procedure. This is as far as I got and it says that my parameters (win, amount, bet, num) "does not seem to be initialzed." Why is that? Can someone point out what I did wrong?

program evenGame;
var bet: integer; { the amount bet by the user }
amount: integer; { the amount won or lost }
num: integer; { the random number }
win: boolean; { win = true or false }

procedure even(bet: integer; num: integer; amount: integer; win: boolean);
begin
write('How much would you like to bet? ');
readln(bet);

randomize;
num := 1 + random(36);

even(num,bet,amount,win);
writeln('The number is: ', num);
read(num);
if (num mod 2 = 0) then
writeln('You win ', bet*3)
else if (num mod 2 = 1) then
writeln('You lose');
end;

begin
even(num,bet,amount,win);
end.

BBCode pascal tags added by moderator during clean-up.

WILL
04-12-2005, 08:40 PM
Hi ic3_man, welcome to PGD. :)

Just a couple of recommendation to help you out right off the bat. First, try using the Pascal BBCode tags provided in your reply Toolbar. it'll make the code clearer and easier to view.

Second, I'd suggest you find a good book on the basics of Pascal or better yet, Object Pascal. Whats the difference? One specifically is a more current, Object Oriented Programming (OOP) language and the other is the older, non-OOP version of the same language. As an alternative you can also find some good materials in the PGD Library (http://www.pascalgamedevelopment.com/library.php) on the Pascal langage it's self. In fact I'd bookmark it, as you are a beginner these resources will be a great place to get started with.

Lastly, if you are using something like Delphi that has helpfiles included, I'd strongly recommend getting used to the language reference help files. I practically learned most of what I know about the language using Turbo Pascal 7 and Delphi 3 help files. :)


As for your problem, I'd have gone with a different naming scheme for your procedure's parameters. Because what you have done is altered the local variables instead of the global ones that will stick around after the procedure has closed.

OR you could have done this instead:

procedure even(var bet, num, amount: integer; var win: boolean);

Which will allow the procedure to alter the variables passed to it while calling it.

Hope this helps. :) BTW, what compiler(s) are you using?

ic3_man
04-12-2005, 09:11 PM
It turns out that was the problem and I redid the parameters like you did. Now I just have a problem with the program not going on past 'How much would you like to be?' It's looping, but I think I can figure it out from here. Thanks!

Crisp_N_Dry
04-12-2005, 09:45 PM
Well despite the fact that it means you would be cheating on your homework, a little help never hurt anyone.

Here's the working code.


program EvenGame;

{ Global Variables }
var
Amount: Integer;
Quit: String;

procedure Even;
{ Local Variables }
var
Bet: Integer;
Num: Integer;
begin
Write('How much would you like to bet? ');
ReadLn(Bet);
If Bet<Amount Then
begin
Amount:=Amount-Bet;
Num := 1 + Random(36);
WriteLn('The number is: ', num);
{ If we win }
If (Num Mod 2 = 0) then
begin
{ The multiplier here could be anything but since this is a game of 50/50 chance, it would be odd to have anything other than a double or nothing payout }
WriteLn('You win ', Bet*2) ;
Amount:=Amount+(Bet*2);
{ If we lose }
end else
WriteLn('You lose, good day sir!');
end Else
WriteLn('You do not have enough money to cover the bet');
end;

begin
Randomize;
Amount:=100;
WriteLn('You now have ',amount);
Repeat
Even;
WriteLn('You now have ',amount);
WriteLn('Would you like to quit? [Y] or [N]');
ReadLn(Quit);
Until (Quit='Y') or (Quit='y');
end.


Aah, this takes me back. While I haven't compiled this code I have read it through thoroughly and I'm confident it will work first time and if it doesn't Im sure a few of the regulars will soon let me know and I will be laughed off the boards.

BTW welcome to PGDev.

WILL
04-12-2005, 11:56 PM
It's only cheating if you don't learn anything. ;) ...or there's money involved, which I doubt. :)

ic3_man
05-12-2005, 01:28 AM
Thanks and I found the flaws in my current program. And thank you for welcoming me to the community. I'll see you guys around!