5 Replies - 2616 Views - Last Post: 05 December 2012 - 06:55 PM Rate Topic: -----

#1 Absolutegangsta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 12

Baseball Simulation Program

Posted 03 December 2012 - 09:41 AM

Hello,
I'm trying to write a Baseball Simulation Program and I need help.
The instructions are here http://wikisend.com/...16/baseball.doc
Instructions
OR IN THE Attached Files
I have a problem with the arrays.I also have a problem with getting the getline name of the players from the two files I have for Chicago and Tigers. I also have another file called inBall.txt ( 85 77 88 74 ) in it. I'm also stuck at the last functions win_loss(),print_results(),save_data(),read_roster(),announce_roster()
The program should include ofstream and instream to get the name of the players from the two files Chicago and Tigers.
I'm also getting errors.
code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <ctime>
#include <Windows.h>

using namespace std;

void begin();
void pitch();
void batter_up();
void team_play();
int main()
{
	begin();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();

	

	
}
	
	void begin()
	{
		cout << "The teams that will be playing today are the" << endl << "Chicago White Sox and the Detroit Tigers." << endl;

	}
	void pitch()
	{

	int pitch = 0;
	Sleep(2500);
	srand ( time(NULL) );
	pitch = rand() % 4 + 1;

	cout << "The pitcher threw a ";
	if(pitch == 1)
	{
		cout <<  "curveball" << endl;
	}
	if(pitch == 2)
	{
		cout << "fastball" << endl;
	}
	if(pitch == 3)
	{
		cout << "sinker" << endl;
	}
	if(pitch == 4)
	{
		cout << "slider" << endl;
	}
	
	
		
	}
	void batter_up()
	{
		int contact = 0;
		int ball = 0, strike = 0, out = 0,foul = 0;
		srand ( time(NULL) );
		contact = rand()% 4 + 1;
		
		if(contact == 1)
		{
			int hit = 0;
			srand ( time(NULL) );
			hit = rand()% 16 + 1;
			cout << "The batter hit a ";
			if(hit == 1 || hit == 2)
			{
				cout << "single " << endl;
			}
			if(hit == 3)
			{
				cout << "double" <<endl;
			}
			if(hit == 4)
			{
				cout << "triple" << endl;
			}
			if(hit == 5)
			{
				cout << "Home Run!!! " << endl;
			}
			if(hit == 6)
			{
				cout << "bunt ";
				int bunt = 0;
				bunt = rand()%4+1;
				if (bunt == 1)
				{
					cout << "single" << endl;

				}
				if(bunt == 2 || bunt == 3 || bunt == 4)
				{
					cout << "out" << endl;
					out = out +  1;
				}
				
			}
			if(hit == 7 || hit == 8 || hit == 9)
			{
				cout << "pop out" << endl;
				out = out +  1;
			}
			if(hit == 10 || hit == 11 || hit ==12)
			{
				cout << "fly out " << endl;
				out = out +  1;
			}
			if(hit == 13 || hit == 14 || hit == 15 || hit == 16)
			{
				cout << "ground out " << endl;
				out = out +  1;
			}
		}
		if(contact == 2 || contact == 3 || contact == 4)
		{
			int notHit = 0;
			srand ( time(NULL) );
			notHit = rand() %5+1;
			if(notHit == 1 || notHit == 2)
			{
				cout << "The batter didnt swing and got a ball" << endl;
				ball = ball + 1;
				if(ball == 4)
				{
					cout << "The batter gets to walk a base" << endl;
				}
				
			}
			if(notHit == 3)
			{
				cout << "The batter got a strike" << endl;
				strike = strike + 1;
				if (strike == 3)
				{
					out = out + 1;
					cout << "The batter is out. " << endl;
				}
			}
			if(notHit == 4)
			{
				cout << "The batter swung and missed" <<endl;
				strike = strike + 1;
				if (strike == 3)
				{
					out = out + 1;
					cout << "The batter is out. " << endl;
				}
			}
			if(notHit == 5)
			{
				cout << "The batter hit a foul ball" <<endl;
				foul = foul + 1;
				if(strike < 2 || foul == 2);
				{
				out = out + 1;
				 cout << "The batter is out. " << endl;
				}

			}


			
		}
		
	if ( out == 3)
	{
		cout << "Next team is up ";
	}
	}
		
		void team_play()
		{
			pitch();
			batter_up();

		}

	void read_roster();
	{
		const int teams = 2;
		const int players = 9;
		string positions[players][teams];
		string positions1[players][teams];
		ifstream chicago;
		ifstream tigers;
		chicago.open("C:\\Documents and Settings\\student\\Desktop\\C_Baseball\\C++ Baseball\\Chicago.txt");
		tigers.open("C:\\Documents and Settings\\student\\Desktop\\C_Baseball\\C++ Baseball\\Tigers.txt");
		if (chicago.fail() || tigers.fail()){
		cout << "\nAn input file could not be read.";
		cin.get();
		exit(1);
	}
	for(int i = 0; i < players; i++){
		chicago >> positions[i][0];
		getline(chicago, positions[i][1]);
	}
	for(int i = 0; i < players; i++){
		tigers >> positions1[i][0];
		getline(tigers, positions1[i][1]);
	}
	for(int i = 0; i < players; i++){
		cout << positions[i][0] << "\t";
		cout << positions[i][1] << endl;
	}
	for (int i = 0; i < players; i++){
		cout << positions1[i][0] << "\t";
		cout << positions1[i][1] << endl;
	}
	chicago.close();
	tigers.close();

}
	/*
	win_loss();
	print_results();
	save_data();
	
	read_roster();
	announce_roster() ;
	*/

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Baseball Simulation Program

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Baseball Simulation Program

Posted 04 December 2012 - 12:37 PM

It would be very helpful if you could post the exact errors that you are getting in your code as it is pretty difficult for us to go through 200 odd lines of code.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 Absolutegangsta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 12

Re: Baseball Simulation Program

Posted 04 December 2012 - 01:02 PM

View Postraghav.naganathan, on 04 December 2012 - 12:37 PM, said:

It would be very helpful if you could post the exact errors that you are getting in your code as it is pretty difficult for us to go through 200 odd lines of code.

regards,
Raghav


I can't see the players name in the program nor their positions or Whois the pitcher or batter... etc
I don't know how to calculate the wins and display the final score after 9 innings
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Baseball Simulation Program

Posted 04 December 2012 - 10:48 PM

Hi you generally only need to use srand once in a program - near the beginning of main will do.

    srand ( time(NULL) );




If you can, instead of the arrays a struct for the players could be better.

struct Player
{
  string name;
  string position;
};


int main()
{

  // teams
  Player chicago_team[9];
  Player detroit_team[9];

}




You have a semicolon here which should produce errors.

	void read_roster();
	{




You could read one file at a time with one function.

const int NUMPLAYERS = 9;

// ...

bool load_roster(Player team[], 
                 const char *filename)
{
  // create file stream
  // open file
  
  for(int i = 0; i < NUMPLAYERS; i++) {
    infile >> team[i].name;
    getline(infile, team[i].position);
  }

  // close file
}

void display_roster(Player team[]);

void read_roster(Player home_team[],    
                 const char home_filename[], 
                 Player visitor_team[], 
                 const char visitor_filename[]);




Edit: array of references removed.

This post has been edited by #define: 05 December 2012 - 06:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 Absolutegangsta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 12

Re: Baseball Simulation Program

Posted 05 December 2012 - 12:55 PM

View Post#define, on 04 December 2012 - 10:48 PM, said:

If you can, instead of the arrays a struct for the players could be better.


Hello,
I have to use arrays.
I modified my code a bit and thanks for the srand tip.

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <ctime>
#include <Windows.h>

using namespace std;

void begin();
void pitch();
void batter_up();
void team_play();
void read_roster();


int main()
{
	begin();
	read_roster();
	srand ( time(NULL) );
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	team_play();
	
	

	
}
	
	void begin()
	{
		ifstream ball;
	ball.open("C:\\Documents and Settings\\student\\Desktop\\C_Baseball\\C++ Baseball\\inBall.txt");
	if (ball.fail()){
		cout << "\ninball.txt could not be read.";
		cin.get();
		exit(1);
	}
	int team1wins = 0;
	int team1losses = 0;
	int team2wins = 0;
	int team2losses = 0;
	ball >> team1wins;
	ball >> team1losses;
	ball >> team2wins;
	ball >> team2losses;
	cout << "Welcome to the Baseball Simulation Game." << endl;
	cout << "The home team is the Chicago White Sox with " << team1wins << " wins and " << team1losses << " losses." << endl;
	cout << "The visiting team is the Detroit Tigers with " << team2wins << " wins and " << team2losses << " losses." << endl;
	ball.close();


	}
	void pitch()
	{

	int pitch = 0;
	Sleep(2500);
	pitch = rand() % 4 + 1;

	cout << "The pitcher threw a ";
	if(pitch == 1)
	{
		cout <<  "curveball" << endl;
	}
	if(pitch == 2)
	{
		cout << "fastball" << endl;
	}
	if(pitch == 3)
	{
		cout << "sinker" << endl;
	}
	if(pitch == 4)
	{
		cout << "slider" << endl;
	}
	
	
		
	}
	void batter_up()
	{
		int contact = 0;
		int ball = 0, strike = 0, out = 0,foul = 0;
		contact = rand()% 4 + 1;
		
		if(contact == 1)
		{
			int hit = 0;
			srand ( time(NULL) );
			hit = rand()% 16 + 1;
			cout << "The batter hit a ";
			if(hit == 1 || hit == 2)
			{
				cout << "single " << endl;
			}
			if(hit == 3)
			{
				cout << "double" <<endl;
			}
			if(hit == 4)
			{
				cout << "triple" << endl;
			}
			if(hit == 5)
			{
				cout << "Home Run!!! " << endl;
			}
			if(hit == 6)
			{
				cout << "bunt ";
				int bunt = 0;
				bunt = rand()%4+1;
				if (bunt == 1)
				{
					cout << "single" << endl;

				}
				if(bunt == 2 || bunt == 3 || bunt == 4)
				{
					cout << "out" << endl;
					out = out +  1;
				}
				
			}
			if(hit == 7 || hit == 8 || hit == 9)
			{
				cout << "pop out" << endl;
				out = out +  1;
			}
			if(hit == 10 || hit == 11 || hit ==12)
			{
				cout << "fly out " << endl;
				out = out +  1;
			}
			if(hit == 13 || hit == 14 || hit == 15 || hit == 16)
			{
				cout << "ground out " << endl;
				out = out +  1;
			}
		}
		if(contact == 2 || contact == 3 || contact == 4)
		{
			int notHit = 0;
			notHit = rand() %5+1;
			if(notHit == 1 || notHit == 2)
			{
				cout << "The batter didnt swing and got a ball" << endl;
				ball = ball + 1;
				if(ball == 4)
				{
					cout << "The batter gets to walk a base" << endl;
				}
				
			}
			if(notHit == 3)
			{
				cout << "The batter got a strike" << endl;
				strike = strike + 1;
				if (strike == 3)
				{
					out = out + 1;
					cout << "The batter is out. " << endl;
				}
			}
			if(notHit == 4)
			{
				cout << "The batter swung and missed" <<endl;
				strike = strike + 1;
				if (strike == 3)
				{
					out = out + 1;
					cout << "The batter is out. " << endl;
				}
			}
			if(notHit == 5)
			{
				cout << "The batter hit a foul ball" <<endl;
				foul = foul + 1;
				if(strike < 2 || foul == 2);
				{
				out = out + 1;
				 cout << "The batter is out. " << endl;
				}

			}


			
		}
		
	if ( out == 3)
	{
		cout << "Next team is up ";
	}
	}
		
		void team_play()
		{
			pitch();
			batter_up();
			

		}

	void read_roster()
	{
		const int teams = 2;
		const int players = 9;
		string positions[players][teams];
		string positions1[players][teams];
		ifstream chicago;
		ifstream tigers;
		chicago.open("C:\\Documents and Settings\\student\\Desktop\\C_Baseball\\C++ Baseball\\Chicago.txt");
		tigers.open("C:\\Documents and Settings\\student\\Desktop\\C_Baseball\\C++ Baseball\\Tigers.txt");
		if (chicago.fail() || tigers.fail()){
		cout << "\nAn input file could not be read.";
		cin.get();
		exit(1);
	}
	for(int i = 0; i < players; i++){
		chicago >> positions[i][0];
		getline(chicago, positions[i][1]);
	}
	for(int i = 0; i < players; i++){
		tigers >> positions1[i][0];
		getline(tigers, positions1[i][1]);
	}
	for(int i = 0; i < players; i++){
		cout << positions[i][0] << "\t";
		cout << positions[i][1] << endl;
	}
	for (int i = 0; i < players; i++){
		cout << positions1[i][0] << "\t";
		cout << positions1[i][1] << endl;
	}
	chicago.close();
	tigers.close();

}



I can't assign the names to the pitcher or the batter when the game starts?
#define
How can I get the scores after the game finishes and display the winner?
Thank you.

Welcome to the Baseball Simulation Game.
The home team is the Chicago White Sox with 85 wins and 77 losses.
The visiting team is the Detroit Tigers with 88 wins and 74 losses.
Septimo         Pitcher
Gimenez         Catcher
Hudson          3rd Base
Konerko         1st Base
Lopez           2nd Base
Viciedo         Right Field
Danks           Center Field
Wise            Outfield
Ramirez         Shortstop
Fielder         1st Base
Infante         2nd Base
Cabrera         3rd Base
Holaday         Catcher
Berry           Center Field
Young           Left Field
Scherzer                Pitcher
Boesch          Right Field
Peralta         Shortstop
The pitcher threw a slider
The batter swung and missed
The pitcher threw a curveball
The batter hit a pop out
The pitcher threw a fastball
The batter got a strike
The pitcher threw a fastball
The batter got a strike
The pitcher threw a slider
The batter didnt swing and got a ball
The pitcher threw a fastball
The batter hit a foul ball
The batter is out.
The pitcher threw a slider
The batter hit a foul ball
The batter is out.
The pitcher threw a slider
The batter hit a fly out
The pitcher threw a slider
The batter swung and missed
Press any key to continue . . .


Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,635
  • Joined: 19-February 09

Re: Baseball Simulation Program

Posted 05 December 2012 - 06:55 PM

Variables can be passed to functions.


#include <iostream>
#include <fstream>
#include <string>

using namespace std;


const int NUMPLAYERS  = 9;
const int NAMEANDPOST = 2;

bool load_roster(string team[NUMPLAYERS][NAMEANDPOST], const char *filename);
void display_roster(string team[NUMPLAYERS][NAMEANDPOST]);


int main()
{
  string chicago[NUMPLAYERS][NAMEANDPOST];

  load_roster(chicago, "chicago.txt");
  display_roster(chicago);

  cout << "Press enter to continue. " << endl;
  cin.get();
  return(0);
}


bool load_roster(string team[NUMPLAYERS][NAMEANDPOST], const char *filename)
{
  // create file stream
  ifstream infile;
  // open file
  infile.open(filename);
  if(!infile)
    return false;

  for(int i = 0; i < NUMPLAYERS; i++) {
    infile >> team[i][0];         // name
    getline(infile, team[i][1]);  // position
  }

  // close file
  infile.close();

  return true;
}


void display_roster(string team[NUMPLAYERS][NAMEANDPOST])
{
  for(int i = 0; i < NUMPLAYERS; i++){
    cout << team[i][0] << "\t";
    cout << team[i][1] << endl;
  }
}




Simpler data types will need references to return values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1