Page 1 of 1

PostPosted: Tue May 15, 2001 12:19 pm
by SimeonB
Hello all,

I am brand new to the world of PocketC and am using it to try and master programming again (been off it for a few years). I have attempted a very simple program, which works successfuly, and goes like this:

// Test

main() {
int myInteger;
myInteger = gets("Enter a number: ");
if (confirm("Are you sure? Happy with " + myInteger))
alert( myInteger);
else
alert("Try again");

}

However, I wanted to make it really clever, and after the message box that says "Try Again" go back to where it asks for a number again. Is there any way of doing this simply, or is it difficult?

Many Thanks

Simeon

PostPosted: Tue May 15, 2001 1:11 pm
by lale
Hi Simeon,

This should do the recursion:

// Test

main() {
int myInteger;
myInteger = gets("Enter a number: ");
if (confirm("Are you sure? Happy with " + myInteger))
alert( myInteger);
else{
alert("Try again");
main();
}
}

Regards,

Lars

PostPosted: Tue May 15, 2001 1:38 pm
by SimeonB
Many thanks, Lars. Obvious really - I was trying all sorts of things, but the simple solution didn't come to me :-)

Thanks again

Simeon

PostPosted: Tue May 15, 2001 3:04 pm
by wolfgang
Hello,

the recursion solution has a possible shortcoming: each time you call the function, a new set of the local variables is created on the stack. I don't know how PocketC handles the stack and how large it is, but if you say "no" often enough in a row it could potentially run full and break the program. That said, a simpler solution is a simple loop:

main()
{
int myInteger, i_done;
i_done = 0;

while (!i_done)
{
myInteger = gets("Enter a number: ");
if (confirm("Are you sure? Happy with " + myInteger))
{
alert( myInteger);
i_done = 1;
}
else
{
alert("Try again");
}
} // end of while loop
}


Wolfgang

PostPosted: Tue May 15, 2001 3:39 pm
by wossname
I hate to say this, but...
I wish PocketC had a Goto statement. It would make life easier, even though it can lead to "spagetti code" with overuse!

What do you guys think? Shall we picket outside the PocketC author's house until we get it? :)

Wossname :)

PostPosted: Tue May 15, 2001 9:37 pm
by gjperker
I had a programming professor who would have thrown you out of class, through a window, for saying that.

TOP DOWN PROGRAMMING!! Goto was forbiden.

But then again, that's just one guy.

Greg Perkowski
greg@perkowski.net

PostPosted: Wed May 16, 2001 5:13 am
by SimeonB
Funnily enough, after I showed a programmer chum of mine the recursion solution, he "tut-tutted" and said exactly the same as Wolfgang - that many instances would be created in memory if you said "no" enough times. He also recommended a loop (he showed me how to do it in a slightly different way than Wolfgang, but I include it for reference). I don't supposse one way is better than the other, just different:

// Test 2

main() {
int myInteger;
int amhappy;

amhappy=0;
while (amhappy=0)
{
myInteger = gets("Enter a number: ");
myhappy = confirm("Are you sure? Happy with " + myInteger);
if myhappy=0 alert("Try again");
}
alert("Your number is: " + myInteger);
}

As for GOTO's, my old programming tutor would have said exactly the same thing Greg!

Simeon

PostPosted: Wed May 16, 2001 3:33 pm
by wossname
I know, I know! :)

I was brought up with Basic programming (various platforms, from AtariST to TI to PalmOS to QuickBasic to Visual Basic (which I worship!)) but I hardly ever used goto, because it is easy to just create more sub's when needed (gosub) but with PocketC its a little more difficult due to the memory restrictions. What I mean is that Memo's are a pain in the ass to write programs on.

I think I could count the number of times i have used GOTO in the last 6 months on the fingers of one hand.

But it would be suitable for PocketC in my opinion, since we are forced to shed some of our ingrained paradigms anyway!

I think the following should be added to Pocketc in future versions:
- Unsigned Byte/Integer/Long data types
- Instantly definable multi-dimension arrays:

int LikeThis[5]<img src=icon_smile_8ball.gif border=0 align=middle>;

- then I would be happy!

I think the PToolboxLib goes a long way to make up for the shortcomings of PocketC though. What do you guys think?

Wossname :)