Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Trouble making a variable count up, to track number of tries.

Author
Message
Koded
13
Years of Service
User Offline
Joined: 15th Apr 2011
Location:
Posted: 16th Apr 2011 04:36


This is part of one of the tutorials, and I am trying to make GuessCount increase by one, per try at the game, store the number, and print it to GuessTotalCount later in the program. I racked my brain for about an hour trying to figure out how to make it work, closest I got was displaying Guess Count of 1 no matter how many times it takes you to guess the number.


Thanks,


Koded

Mostly fail... sometimes epic
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Apr 2011 11:55
you need:

INC GuessCount, 1

Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 16th Apr 2011 19:41
GuessCount=() rem set guess count to zero

This line is sort of legal, but it's best to actually assign it a value.

GuessCount=0 rem set guess count to zero

Agent
20
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 17th Apr 2011 00:20
Hi Koded,

I'm with Kevin. I've never seen anyone try to clear a variable with empty parenthesis before! Use a zero instead.

When you check to see if the guess is higher or lower than the target value, you've used the expression GuessCount+1 in a PRINT statement to tell the player how many attempts he's already had. I understand what you're doing here, and you've got the right idea, but while I can see that you expect GuessCount to increment by 1 here it actually doesn't.

Here's what's happening: The program reaches the PRINT statement and knows it's about to display something to the screen. It goes through the expressions involved, starting with the string "This was Guess Number " and then moving on to evaluate the little equation at the end (GuessCount+1). What happens here is that the value of all variables replaces the variable names used. In other words, if GuessCount contains the value 5, then this expression evaluates to 5+1. The final output will be the string of text, followed by the number 6 (which is what 5+1 evaluates to). The actual contents of the variable involved isn't changed by this process - we're only reading the variable, not writing to it. This is why you always get a count of 1, no matter how many times you enter a guess: you have set the value of your counter to zero but you are never actually increasing it, meaning the little equation always says 0 + 1.

What you want to do, as BatVink suggested, is to add a new line of code immediately above your PRINT statement. Here's what it'll say:

INC GuessCount, 1

The INC command is one that writes a new value to a variable. You can specify what variable you want to adjust, and then after the comma comes the value you want to add to the variable. Using a ,1 adds one to the value of the variable. Using a ,5 will add five to that value.

So, after issuing this INC command, the actual contents of your variable will have incremented by one. Now you won't need the equation in your PRINT statement at all - it'll look like this instead:

PRINT "This was Guess number "; GuessCount

Then you just need to write a check for a number that's too high, and another one to check if they hit the nail on the head!

Koded
13
Years of Service
User Offline
Joined: 15th Apr 2011
Location:
Posted: 17th Apr 2011 11:05
Thanks* I really appreciate the detailed explanation. Everything is working as intended now. I'm down right determined to learn DBP, there is such a wealth of information here, I'm rather disappointed I didn't purchase DB a long time ago when I first learned of it(which was right around the time DarkBasic was taking flight). I was skeptical of the language, which was obviously naive of me.


Anyways, I'm glad to be here, and you'll definitely see me popping in with questions from time to time. And trust me, I wont post in less I've invested several hours of my own time trying to figure it out, even if my dilemma turns out an extremely simple fix lol.

Thanks again guys, and talk to you again soon*

Mostly fail... sometimes epic
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 17th Apr 2011 21:04
Quote: "Anyways, I'm glad to be here, and you'll definitely see me popping in with questions from time to time. And trust me, I wont post in less I've invested several hours of my own time trying to figure it out, even if my dilemma turns out an extremely simple fix lol. "


We appreciate it.

The help files will teach you proper code syntax... just type in a command and hit F1.

And TDKs tutorials will teach you the basics:
http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

Login to post a reply

Server time is: 2024-11-24 04:45:40
Your offset time is: 2024-11-24 04:45:40