Finally... > Creative Projects
Programming Help Thread (For Dummies)
MaximumZero:
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)//baseball
#include <iostream>
using namespace std;
main()
{
char hometeam[35],awayteam[35],result;
int strike,ball,out,run,hit,batter;
cout<<"Spring 2011 Umpire's Assistant @ Your Service"<<endl<<endl;
cout<<"The Item Codes are as follows:"<<endl;
cout<<"Code Description\tCode Description"<<endl;
cout<<"S\tStrike\t\tH\tHit"<<endl;
cout<<"B\tBall\t\tO\tOut"<<endl;
cout<<"F\tFoul Ball\tR\tRun in"<<endl;
cout<<"G\tGame Cancelled"<<endl<<endl;
cout<<"What is the home team Mascot name? ";
cin.getline(hometeam,35);
cout<<"What is the away team Mascot name? ";
cin.getline(awayteam,35);
cout<<endl<<endl<<"*** "<<awayteam<<" now at bat. "<<hometeam<<" takes the field.***\n";
do
{
for (out==0;out<=3;batter++);
cout<<"Inning Status Batter "<<batter<<" # Balls "<<ball<<" # Strikes "<<strike<<" # Outs "<<out<<" # Runs "<<run<<endl;
cin>>result;
switch(result)
{case 'S': {strike++;} cout<<"Strike! \n";
break;
case 'B': {ball++;} cout<<"Ball. \n";
break;
case 'F': {if (strike<=2, (strike++));} cout<<"Foul! \n";
break;
case 'H':{hit++;} cout<<"A base hit! \n";
break;
case 'O': {out++;} cout<<"He's outta there! \n";
break;
case 'R': {run++;} cout<<"A run scores! \n";
break;
case 'G':{return 0;} cout<<"The game has been called. \n";
break;
default: cout<<"******* Invalid Please enter S,B,F,G,H,O or R.\n";
}}while(out<=3);
getchar();
return 0;
}
Nadaka:
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.
Nadaka:
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.
MaximumZero:
*sigh* Well, I guess it's time to re-read the looping chapter. Thanks for your insight.
Stargrasper:
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?
Navigation
[0] Message Index
[#] Next page
Go to full version