Baseball Simulation Program
Dec 3, 2012 at 3:53pm UTC
Hello,
I'm trying to write a Baseball Simulation Program and I need help.
The instructions are here http://wikisend.com/download/477216/baseball.doc
I have a problem with the arrays, and when I try to test the program the results are always the same. for example: The pitcher threw a fastball and the batter didnt swing. 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.
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <time.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;
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();
}
/*
win_loss();
print_results();
save _ data();
read_roster();
announce_roster() ;
*/
Last edited on Dec 3, 2012 at 4:01pm UTC
Dec 3, 2012 at 8:09pm UTC
Call srand ( time(NULL) );
only once and at the beginning of your main().
Topic archived. No new replies allowed.