Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3 ... 91

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

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Programming Help Thread (For Dummies)
« on: April 27, 2011, 11:00:28 am »

I created the Math Help Thread, so that it stopped cluttering up the sad/rage threads. So, I'm creating this one, specifically for people to ask for help so that that doesn't clutter up other threads.

So, I have a question to get the ball rolling:

I'm trying to write a baseball scoreboard program in C++ (for skool), and when I output a couple of variables for the first time, they output garbage. I should probably not be seeing over a million outs when the first batter is up. Here's what I've got (yes, it's incomplete, and sloppy, and ugly, but it compiles.)

Spoiler (click to show/hide)
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

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #1 on: April 27, 2011, 11:07:18 am »

your variables are uninitiated. Whatever data is in that particular memory at the time you declare a variable in c++ is its initial value. So you need to make sure you initialize those variables with good data (probably 0 in this case) before you read them.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #2 on: April 27, 2011, 04:01:10 pm »

Also:

Your for loop is a nearly infinite empty loop and improperly constructed. The for loop has 3 sections separated by semi-colons, the first section is run before the first iteration, the middle must be a boolean expression that determines if the loop terminates at the end of each iteration, the final is an expression that runs after that test.

In that for loop, the first has a relational operator rather than the typical assignment. Also the last part increments a value not set or tested in the other parts, so it will roll over and most likely blow up after a few billion cycles. And the terminating semicolon makes it execute no other lines of code.

You never increment your outs, so your while loop will also not terminate.

I am willing to provide some help and guidance for languages I know, that is mostly java, c# in great detail and a lot of other languages (including c/c++) in passing.

Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #3 on: April 27, 2011, 04:06:15 pm »

*sigh* Well, I guess it's time to re-read the looping chapter. Thanks for your insight.
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

Stargrasper

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #4 on: April 28, 2011, 10:00:07 pm »

Looks like others caught the biggest errors already.  Is there a reason hometeam and awayteam are char arrays?  C++ has a built-in string class you can use.  I don't think you even have to include anything to use it.

And what are you actually trying to accomplish with those do and for loops?  Track a single inning?
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #5 on: April 28, 2011, 11:02:50 pm »

Half an inning, then ask if the user wants another half inning, and switch teams at bat and in field. I seem to have no idea how to do that.

Also, thanks for reminding me about the string class. It'll probably come in handy.
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

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #6 on: April 29, 2011, 12:57:06 am »

Yes, strings are very useful. Higher level languages often treat them as a primitive data type, rather then a class, because they are that useful. Know the string, love the string, be the string. Just be careful how you use them, they are for presenting data, not logical comparison, we have enums for that.

Now, for making it switch sides and such, I think the best way to start that is though delegation of responsibility. Make a function that will run over an innings, that takes two teams as arguments. That way, if you want teams switched for your second innings, just change their order in the signature.

Gantolandon

  • Bay Watcher
  • He has a fertile imagination.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #7 on: April 29, 2011, 01:26:44 pm »

Quote
Code: [Select]
for (out==0;out<=3;batter++);

This is probably a good moment to tell you about differences between "==" and "=". The first one is for comparison, the other one for assignment. Many of languages with syntax based on C (Java for example) will prevent you from mistaking them, but C and C++ will not.

It is considered perfectly OK to do something like you did here. Or something like that:

Code: [Select]
if (x=1)
It will be considered perfectly normal by the compiler, though some more decent may throw a warning. But this code will execute and the expression in brackets will always be considered as true. Therefore, when you compare things, it's a good idea to put the variable on the right side:

Code: [Select]
if (1==x)
This way, if you accidentally miss one "=", the compiler will actually throw an error.
« Last Edit: April 29, 2011, 01:30:35 pm by Gantolandon »
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #8 on: May 02, 2011, 11:43:06 am »

Okay. Talked to my prof, cleaned up the code quite a bit, and now it's throwing me "Expected (stuff) at end of input" errors, even though said stuff is there.

Here's the code:

Spoiler (click to show/hide)
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

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Programming Help Thread (For Dummies)
« Reply #9 on: May 02, 2011, 11:51:19 am »

count your squiggly brackets. between the inner "do" and "while" you have one more "{" than "}".

Also... It has been a long time since I did c++, so I don't recall the exact syntax of the switch statement, but I notice some inconsistent usage of grouping brackets in there. At the very least it is bad coding style.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #10 on: May 02, 2011, 11:53:26 am »

be the string.
I'm afraid I can knot do that.
Logged

Gantolandon

  • Bay Watcher
  • He has a fertile imagination.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #11 on: May 03, 2011, 06:02:40 am »

Quote
Also... It has been a long time since I did c++, so I don't recall the exact syntax of the switch statement, but I notice some inconsistent usage of grouping brackets in there. At the very least it is bad coding style.

Yeah, most of them (such as {strike++, count++;}) definitely shouldn't be there. Not to mention that statements should be separated by semicolons, not commas.
« Last Edit: May 03, 2011, 06:04:52 am by Gantolandon »
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #12 on: May 03, 2011, 06:51:32 am »

And don't be afraid of using more lines and whitespace. In general you should only have one statement per line, ie one semicolon per line, with the exception of for loop declarations. It improves readability, and once you start using a debugger (which is inevitable), having multiple statements on one line makes it hard to set breakpoints properly.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #13 on: May 03, 2011, 06:59:03 am »

Yea, adding white space doesn't make the program run any slower.  :P
As personal preference, even with one line blocks inside an if statment, I still use {}, as

statement;
statement;
statement;
if (condition)
{
    statement;
}
statement;

Makes it a lot easier to find your block when debugging, as opposed to.

statement;
statement;
statement;
if (condition) statement;
statement;


A lot of people don't do this, as it takes longer to type out, but better to spend time in the first iteration, then spend lots of time trying to read code in debug.

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #14 on: May 03, 2011, 07:00:06 am »

See, that was the way I started doing it, then I got yelled at for it. Time to go back to the prettier format.
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
Pages: [1] 2 3 ... 91