Identifying variables!!!

Pages: 12
I'm working on a practice problem and I want to know if I have the right variables.
here's an example!!!

America’s National past time is Major League Baseball. The scoring for baseball is made
up of a diamond shaped infield with 3 bases and home plate. Players come up to bat and
try to get hits, so they can get on base and hopefully score runs. There are four different
kind of hits a player can get; a single (making it to first base), a double (getting to second
base), a triple (getting to third base) or a homerun (running around all four bases and
scoring, most of the time the ball is over the fence). Runners advance on hits if they are
already on base.




These are the variables I have down

Bases = 3, home plate, at bats, player on base, runs scored, hits = single(1st base), double(2nd base), triple (3rd base ), Home run(home plate), innings = 9 (while runs are equal at inning9 do inning +1 else return 0;), innings = Bottom, Top; outs = 1,2,3;

I know it looks stupid I just want to know if I'm on the right path???

Thanks guys!!!
What exactly do you mean? Got any code to show us?
What?
This is a practice example I got of line I just wanted to know if I selected the right variables to start writing the right code

I a beginner so I'm basically just doodling as far as the variables I have down
the paragraph I just cut and pasted from a practice problem web site.
closed account (y8h7M4Gy)
Did you try out the tutorial on this site?

Go here :

www.cplusplus.com/doc/tutorial/introduction
Rbi's?, pitcher era's, batting averages, walks?
Last edited on
i think the answer to your question is "YES" you are on the right track, but the way you wrote it is confusing. this would have been best presented in a pseudocode solution or actual code.

Just remember variable names can NOT contain spaces so things like player on base, runs scored, hits = single(1st base) etc will give you errors, unless you change names. to include underscores or CamelCase.
Last edited on
Yeah Edward that site worked out perfect. I figured I should use a multidimensional Array I started out with this

#include <iostream>
#include <string>
using namespace std;
int bases [4] = {1st, 2nd, 3rd, home};
int n, results=0;

int main()
{

But I figured I should put more in my array and make it multi dimensional
and instead of using 1st, 2nd, 3rd
I might just use S, Dbl, Trip, HR;

and then add what BettyBoopts suggested RBi's Pitches etc (Basiclly add all the stats that should go on the score card

Thanks
hey jason, i think its a great project to practice on, I started it myself, I am pretty much a beginner and learning the basics first through 'C++ A Beginner’s Guide' by Herbert Schildt ' . I am on chapter 5 if you are familiar with the writing.

anyhow, here is a partial program I wrote, so far i have one side up to bat then out, i dont have doubles or triples yet, just singles, strikes, balls, outs, foul balls, pitches. anyhow, check it out if you like!

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
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;



int main ()
{
int strike, swing, out, ball, walk, run, yrun, base;
int c,d,s;c=d=1;
strike=out=ball= walk=run =base=0;
yrun = 3;

srand (time(NULL) );

next_pitch:


cout << "pitcher throws the ball!\n";

    swing = rand() % 5 + 1;

if (swing==1){cout << "batter swings! Hit!\n\n";strike=0;ball = 0;base++;}

 if (swing==2){cout << "batter swings! strike!\n\n";strike++;}

 if (swing==3){cout << "batter swings! Foul Ball!\n\n"; if (strike<=1)strike++;}

 if (swing==4){cout << "batter takes the pitch! ball!\n\n";ball++;}

 if (swing==5){cout << "batter takes the pitch, strike!\n\n";strike++;}

    
if(strike ==3){ strike = 0; ball = 0;cout << "strikeout!\n"; out++;}
if(ball ==4){ base=base++;strike = 0; ball = 0;cout << "base on balls!\n\n"; base++;}
cout << "\n\n" << ball << "balls  :" << strike << "strikes  :" << out<< "outs :\n\n";

if (base==1) {cout << "runner on first! \n\n";}

if (base==2) {cout << "runners on first & second! \n\n";}

if (base==3) {cout << "runner on first, second & third! \n\n";}

if (base==4) {cout << "runner scores!\n\n";
cout << " runners on first!second, & third \n\n";}

if (base==4) {run = run++;base = 3;}

cout << "score is Red Sox: " << run<< " Yankees: "<< yrun<<" \n\n";   
if(out==3){ cout << "side out!the Red Sox take the field!(the Crowd goes wild!!!)\n\n"; goto end;}



system ("pause");



   system ("cls");


n: if (c<=15) {cout << "\n";goto next_pitch;++d;goto n;}

goto next_pitch;
end:

return 0;
}
Last edited on
I have a number of... displeasures with that code.

First of all, you shouldn't do his homework for him.
Second of all, the amount of gotos is ridiculous, you shouldn't even have to use one in that assignment.
Thirdly, you used the system() command unnecessarily. [1][2]
Fourth, your indentation is patchy and inconsistent, making your code hard to read.
Finally, don't do his homework for him.

[1]http://cplusplus.com/forum/articles/11153/
[2]http://cplusplus.com/forum/articles/10515/
Last edited on
thanks for the input chris :) like i stated in the above post, I have just started out in the programing world. I began c++ a few weeks ago through 'C++ A Beginner’s Guide' by Herbert Schildt' and have just been introduced to functions(chapter 5) today. I am sure as I go along and with your continuing positive feedback, I will progress on a stellar pace, lol,,, and btw,,, these posts i take on are ones that are good homework assignments for me.

Last edited on
Hey look guys
first of all this isn't homework my semester is done all ready
second of all I'm headed more towards doing it in a score card or record keeping format

Betty that looks nice it looks more like a gaming type code to where you can actually have a simulated game

Look the problem actually only asked for innings, strikes and runs
so everybody chill out with the Ciscal and Ebert Talk!!!
and I'd rather figure it out on my own this is just for sharing while on Christmas break

Thanks guys!!!
lmao @ ciscal and ebert, thanks jason:) i do however need to learn to neaten up my code and not depend on goto's so much,, i am learning functions now so I think thatll do the trick for the goto issue. it is a neat part of what will be a much larger program when its done, i think i will enjoy developing it. but once again, thanks for the input.
Last edited on
BettyBoopTS
This is the site I get my practice problems from
check it out!!!


http://watts.cs.sonoma.edu/practice.html

Peace!!!




Thanks Jason!, I visited the site and am working on the battleship assignment!
I eliminated the goto's and added functions to my baseball program, I tried to clean up the indents as best I could, I kept the system commands because the system pause helps the 'interaction' feel of the game, and the system cls is necessary to avoid clutter on the screen, so i kept those in until I learn something better that will achieve the same things.

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

#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>
using namespace std;
void batter();// batter prototype
void runner();// runner prototype
void stats();//stats prototype
void side_out();// side out prototype

int strike=0, swing=0, out=0, ball=0, walk=0, run=0, yrun=3, base=0;

int c=1,d=1,s=1;


int main ()

{
     system ("cls" );

               cout << "Yankees Vs. Red Sox!  ";
 
     system ("pause");

     batter();

     side_out();


return 0;

}
 
void batter()

{
     if(out==3)return;

srand (time(NULL) ); 

                 cout << "pitcher throws the ball!\n\n";

 
swing = rand() % 5 + 1;

     if (swing==1)
               {cout << "batter swings! Hit!\n\n";strike=0;ball = 0;base++;}

     if (swing==2)
               {cout << "batter swings! strike!\n\n";strike++;}

     if (swing==3)
               {cout << "batter swings! Foul Ball!\n\n"; if (strike<=1)strike++;}

     if (swing==4)
               {cout << "batter takes the pitch! ball!\n\n";ball++;}

     if (swing==5)
               {cout << "batter takes the pitch, strike!\n\n";strike++;}

stats();

}

void stats()

{
  
     if(strike ==3){ strike = 0; ball = 0; 
                cout << "strikeout!\n"; out++;}

     if(ball ==4){ base=base++;strike = 0; ball = 0;
                cout << "base on balls!\n\n"; base++;}

                cout << "\n\n" << ball << "balls  :" << strike << "strikes  :" << out<< "outs :\n\n";


                cout << "score is Red Sox: " << run<< " Yankees: "<< yrun<<" \n\n";   




runner();

}

void runner()

{


     if (base==1) 
               {cout << "runner on first! \n\n";}

     if (base==2) 
               {cout << "runners on first & second! \n\n";}

     if (base==3) 
               {cout << "runner on first, second & third! \n\n";}

     if (base==4) 
               {cout << "runner scores!\n\n";

                cout << " runners on first!second, & third \n\n";}

     if (base==4) 
               {run = run++;base = 3;}



     system ("pause");
 
     system ("cls"  );

              cout << "\n";

     if (out<3 ) batter();
    

}

void side_out()

{

     if(out==3){cout << "side out!the Red Sox take the field! \n";
                cout << "(the Crowd goes wild!!!)\n\n";return;}

}
I linked you to a page full of alternatives, use them!
http://cplusplus.com/forum/articles/10515/

Furthermore, you don't need system("pause");
http://cplusplus.com/forum/beginner/1988/#msg7263

You can do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void __sleep__(int ms) { /* This is just an example of a relatively cross-platform method of sleeping */
#ifdef _WIN32
    Sleep(ms); /* Sleep() takes a parameter of milliseconds */
#else
    sleep(ms / 1000); /* Whereas sleep() takes a parameter of seconds */
#endif
}
class KeepRunning { /* http://cplusplus.com/forum/beginner/1988/#msg7682 */
    public:
        ~KeepRunning() {
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); /* Skip enter presses */
            __sleep__(2000); /* Sleep 2 seconds */
        }   
};

}


As Grey Wolf explained later in that thread; if you create an instance of that KeepRunning class, e.g.
1
2
3
4
5
int main(void) {
    KeepRunning _keep_running;

    return 0;
}

before exiting, that destructor HAS to be called to destroy the _keep_running variable we placed on the stack. So regardless of what happens (even if you get abnormal termination) that code will always be called.
Last edited on
thanks chris :) its much appreciated help
Sorry if I appear a little impatient... I don't mean to sound harsh.
Pages: 12