Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 29 30 [31] 32 33 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100801 times)

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #450 on: January 31, 2012, 09:14:20 pm »

he's not just doing a while loop, he's doing a do...while loop.
Sorry, sloppy terminology...  "The loop with the 'while' termination" is what I was meaning.  Elsewhere I gave it a fuller description, but obviously it could cause confusion the way I referenced it that shorter way.

And, to that end, I wasn't going to say anything but...
[...]but even if you want it as a do while loop

the cin should be inside of it, or else it would only work the first time. then when it goes back to it to try it again the value that was used as nselector the first time would be kept every other time it ran.
so you should either include the cin inside the loop, and/or reset the nselector value for each rerun of the program.
...but the nSelection is within that loop, isn't it?  It does get refreshed, doesn't it?  I may be getting confused as to language dialect, but it certainly seems to do.


Quote
and I feel stupid for not even noticing he used return instead of cout.
Well, neither did I, I suppose, or rather I was reading it as an internal function that was misbehaving instead of a stand-alone program for which the return value is available to the shell, depending on the system it's on, but probably not what I'd do. ;)
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #451 on: January 31, 2012, 09:20:32 pm »

Spoiler (click to show/hide)


he's not just doing a while loop, he's doing a do...while loop.
Sorry, sloppy terminology...  "The loop with the 'while' termination" is what I was meaning.  Elsewhere I gave it a fuller description, but obviously it could cause confusion the way I referenced it that shorter way.

And, to that end, I wasn't going to say anything but...
[...]but even if you want it as a do while loop

the cin should be inside of it, or else it would only work the first time. then when it goes back to it to try it again the value that was used as nselector the first time would be kept every other time it ran.
so you should either include the cin inside the loop, and/or reset the nselector value for each rerun of the program.
...but the nSelection is within that loop, isn't it?  It does get refreshed, doesn't it?  I may be getting confused as to language dialect, but it certainly seems to do.



first of all, I was too lazy to type out nSelection and figured you'd know what I meant by nselect, lol

and it is withen the loop, but the cin nSelection isn't in the loop in a way that it would EVER be called more than once,

look, it only does
do
    {
        cout << "Please make a selection:" << endl;
        cout << "1:Addition" << endl;
        cout << "2:Subtraction" << endl;
        cout << "3:Multiplication" << endl;
        cout << "4:Division" << endl;
        cin >> nSelection;
    }
while
while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4); equals one
so as soon as you enter a number / assign a number to nSelection that while expression equals 0,
so you'll never be able to cin >> nSelection again.


right? or am I missing something?
« Last Edit: January 31, 2012, 09:24:08 pm by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Rgamer

  • Bay Watcher
  • [RELIGION: JEWISH]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #452 on: January 31, 2012, 09:36:10 pm »

You're right,Valid_Dark. And thanks for the advice, by the way. It fixed ze program very gud. And removing excess gotos is good,so to the guy who advised that (Forgot your name already), thanks a lot.
Logged
Rgamer likes dwarves for their industriousness. He dislikes small dogs for their annoying barking, enjoys a good run, and prefers to consume steak, potatoes, or apples whenever possible. He likes native silver, granite, marble, and ebony.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #453 on: January 31, 2012, 11:44:19 pm »

I really enjoy fixing peoples code, it refreshes my memory on certain aspects I don't always use.
I haven't been coding for very long, but it's always good to share what I know,
my first week learning c++ i was correcting people on some stupid c++ forum,
(someone said +=  -= *= etc. dont work in c++, and I was like whaaaaat??" etc.)
and when I'm learning something new and can't figure out why it's not working I ask for help too, it's much faster to ask someone who's been through it rather than sit and ponder about it for a day or two pulling your hair out trying everything until it works.
although last time I asked for help someone said they'd help me but never did.   :'(
I've only been learning c++ for a few months, all self taught. (from a very good book, i tried learning from online tuts and none were nearly as good as this book I got, and couldn't find any good ebooks, or when I did I found out I much prefer to learn from a physical book)
everything is a learning experience.  I really need to work on my game some more but keep procrastinating.
I have bursts of inspiration that come and go.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #454 on: February 01, 2012, 12:22:39 am »

the ascii of 1-9 is 1-9.

The ascii value of 1-9 is actually 49-57.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #455 on: February 01, 2012, 12:59:12 am »

you're right, I don't remember where I heard that.
anyways I NEVER go from memory when working with ascii, I always look up a chart.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #456 on: February 01, 2012, 07:43:05 am »

first of all, I was too lazy to type out nSelection
You saved a massive one character with "nSelector" instead of "nSelection", you know. ;)  And we've wasted more on the after-talk.  Didn't look like a typo, and I really wanted to make sure we were talking about the same thing.

Quote
and it is withen the loop, but the cin nSelection isn't in the loop in a way that it would EVER be called more than once,
"Please enter 1, 2, 3 or 4:"
<enters '5'>
"Please enter 1, 2, 3 or 4:"
<enters '9'>
"Please enter 1, 2, 3 or 4:"
<enters '2'>
"Finally, right, now I'll do the calculation you desire..."

Quote
as soon as you enter a number / assign a number to nSelection that while expression equals 0,
so you'll never be able to cin >> nSelection again.
Which is how I saw the program as being 'designed' to work.  After asking for the two initial numbers to work with ask (by way of a multichoice) for a single operation to apply to them.  Keep asking until it's a valid choice, then do the appropriate operation and exit.

You were thinking it should repeat the choice?  I'd also surround the two initial 'data' numbers as well as the 'operation' choice number (or other key).

In which case, make sure there's a "I don't want to do anything more with this" choice on the menu, otherwise you can only terminate the program by a Ctrl-C/equivalent.  Perhaps double loop it with a "finished with these numbers, break out of the 'operator choice loop' and hit the 'data numbers input loop' repeater, as well as an option that breaks out of this second one as well.

Code: (Pseudocode) [Select]
RedoAllLoop:
  Set NewNums as True
  Ask for two numbers to calculate with
  RedoOperationLoop:
    Set NewOp as True
    Ask for operation to apply
    Case Operator 1 -> do Operation1 on Numbers; break out of Case
    Case Operator 2 -> do Operation2 on Numbers; break out of Case
    ...Operator 3, 4, any more, ditto...
    Case NewNumbers -> Set NewOp=False; break
    Case Finished -> Set NewNums=False; break
    Case anythingelse -> Tell the user they did it wrong; (optional) break
  repeat RedoOperationLoop while(NewOp && NewNums)
repeat RedoAllLoop while(NewNums)
Goodbye...

As to the char==value, some languages are 'loose' with values being equatable to value-evaluated strings.  Perl, for example and if I've not made a thinko as I type this, has "if ($x == 0)" and "if ($x == '0')" and "if ($x eq '0')" being the same (and also "if (!$x)", but that would also catch it being undefined, which you might want, but maybe don't)...

Powerful, but prone to error.

If you want to work at a very low level, bitwise (by whatever operators the language you use provides), you can convert ASCII numbers to decimals by ANDing the char's ordinal value with 15, but it isn't foolproof (all characters give values, and across 0..15).  Prevalidating by aiming to see zero come out of the ordinal value ANDed with 192, first, helps, and then non-zero from the ordinal ANDed with 48 but you've still got more out-of-range numbers (for decimal), and it rapidly gets complicated if you've not got a neat 0..2n-1 range, and so instead of 1..4 I'd go for 0..3, ORing out the last two bits once I had checked it matches "001100??" (two checking operations needed, either in series or chained together).  Matching 1..4 would mean "00110???" as the template, but then take measures to exclude the four other values that could include.  And, besides, that's unnecessarily complicated for anything beyond low-level.  And I may have typoed/thinkoed along the way, and I'd need to properly go through it to be absolutely sure I've covered all the truth-table possibilities... ;)

edited for a couple of thinkos/typos, more probably remain...
« Last Edit: February 01, 2012, 07:46:00 am by Starver »
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #457 on: February 01, 2012, 08:51:52 am »

Quote
Code: [Select]
Case anythingelse -> Tell the user they did it wrong; (optional) break
cout<<"You did it wrong, fucktard! Do it again, and do it right this time!";
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #458 on: February 01, 2012, 08:53:40 am »

Quote
Code: [Select]
Case anythingelse -> Tell the user they did it wrong; (optional) break
cout<<"You did it wrong, fucktard! Do it again, and do it right this time!";

XD
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #459 on: February 01, 2012, 01:21:43 pm »


Which is how I saw the program as being 'designed' to work.  After asking for the two initial numbers to work with ask (by way of a multichoice) for a single operation to apply to them.  Keep asking until it's a valid choice, then do the appropriate operation and exit.


>.<
i'm stupid, I just saw that he had the goto and assumed what he wanted it to do.
you're probably right and it was probably only designed to run once.
>.<

Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #460 on: February 05, 2012, 11:58:19 am »

Just teaching myself Java, how do you listen for keyboard events? Specifically, the left and right arrow keys.
EDIT: And also, how do you change the text of a JLabel, once it has been drawn?
« Last Edit: February 05, 2012, 03:14:39 pm by Kofthefens »
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #461 on: February 05, 2012, 04:54:54 pm »

Just teaching myself Java, how do you listen for keyboard events? Specifically, the left and right arrow keys.
Extend your Jframe or JPanel with a Keylistener interface. The codes for the left and right keys are VK_LEFT and VK_RIGHT
Quote
EDIT: And also, how do you change the text of a JLabel, once it has been drawn?
Use the setText method of the JLabel. It should redraw automatically. If not, call redraw on it or one of it's parent containers.
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #462 on: February 05, 2012, 06:19:23 pm »

EDIT: Never mind, problem solved.

Editing an Edit: How do you make a random number between a negative and a positive, eg -100 and 100?
« Last Edit: February 06, 2012, 08:25:07 pm by Kofthefens »
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Help Thread (For Dummies)
« Reply #463 on: February 06, 2012, 11:12:24 pm »

Simple math. If there is already a function to generate a random number from a range, use it. If there is one that generates from 0 to <max>, give it <higher - lower> as max, and then add <lower> afterwards. If you can only get one between 0.0 and 1.0, multiply by <higher - lower> and then add <lower>.
Logged
Eh?
Eh!

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #464 on: February 06, 2012, 11:47:47 pm »

Thanks.
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games
Pages: 1 ... 29 30 [31] 32 33 ... 91