Umpire's assistant

Hello all,

I am having an issues with my code and a question. This is an assignment for class and I do not want the answer given to me. I have to write a code for an Umpire;s assistant. I found the following link that is very similar to my assignment, but look to be long dead.

http://www.cplusplus.com/forum/beginner/107515/

The output should look accordingly.

Delayed Spring Training 2014
Umpire's Assistant at your service.

The Items codes are as follows:
Code Description Code Description
S Strike H Hit
B Ball O Out
F Foul Ball R Run in
G Game Canceled

*******Visitor team at bat Home team in the field*******

Inning status Batter #1 # Balls 0 # Strikes 0 # Outs 0 # Runs 0
1st batter (count of 1): S
Inning status Batter #1 # Balls 0 # Strikes 1 # Outs 0 # Runs 0
1st batter (count of 2): F
Inning status Batter #1 # Balls 0 # Strikes 2 # Outs 0 # Runs 0
1st batter (count of 3): s
Inning status Batter #2 # Balls 0 # Strikes 0 # Outs 1 # Runs 0
2nd batter (count of 1): X

******Invalid Please enter S,B,F,G,H,O, or R

Inning status Batter #2 # Balls 0 # Strikes 0 # Outs 1 # Runs 0
2nd batter (count of 1): H
Inning status Batter #2 # Balls 0 # Strikes 0 # Outs 2 # Runs 0
2nd batter (count of 1): o

THREE OUTS!!!!

Is there another half inning (Y/N)?


My code runs great except for;

1) My while loop does not terminate when 'n' or 'N' is entered.

I.E.
Is there another half inning (Y/N)? N
Inning status Batter #1 # Balls 0 # Strikes 0 # Outs 0 # Runs 0
1st batter (count of 1):


2) Is there is an easier way to put the suffix on the batter numbers other than using if else statements?

I.E.
1st batter (count of 1):
2nd batter (count of 1):
3rd batter (count of 1):
4th batter (count of 1):

I think the problem is in the newInning function, but I just can not find it. Any and all help will be greatly appreciated.

Thanks,
Nightspear

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
 #include <iostream>
#include <string>
using namespace std;
int newInning(char,int, int, int, int, int, int);


int main()
{
    char code, inning, atBat='V';;
    int ball=0, bat=1, out=0, strike=0, hs=1, vs=1, cnt=1, run=0;

    //menu
    cout<<"Delayed Spring Training 2014\nUmpire's Assistant At Your Service\n\n";

    cout<<"\n\nThe menu is: "<<endl;
    cout<<"\n Item \t Code Description \t Item\t Code Description"<<endl;
    cout<<"  R\tRun Scored \t \t  O\tOut"<<endl;
    cout<<"  F\tFoul Ball \t \t  S\tStrike"<<endl;
    cout<<"  B\tBall\t\t\t  H\tHit"<<endl;
    cout<<"  G\tGame Canceled"<<endl<<endl;
    cout<<"\n********** Visitor team is at bat Home team is in the field**********\n\n";

    do
    {
        cout<<"Inning Status\tBatter # "<<bat<<"\t # Balls "<<ball;
        cout<<" # Strike "<<strike<<" # Outs "<<out<<" # Runs "<<run<<endl;
        cout<<bat<<" Batter (count of "<<cnt<<"): ";
        cin>>code;
        switch (toupper(code))
        {
         case 'S':"Strike";
                    if (strike<2)
                        {++strike;
                        ++cnt;}
                    else if (strike==2)
                    {
                        strike=0;
                        ball=0;
                        cnt=1;
                        out++;
                        ++bat;
                        if (out==3)
                           newInning(atBat,out,strike,ball,cnt,run,bat);

                    }
                        break;

         case 'B':"Ball";
                if (ball<3)
                   { ++ball;
                    ++cnt;}
                else if (ball==3)
                   {
                        ball=0;
                        strike=0;
                        cnt=1;
                        ++bat;

                   }
                    break;

        case 'F':"Foul Ball";
                cnt++;
                if (strike<2)
                    strike++;

                break;

        case 'G':"Game Canceled";
                return 0;
                break;

        case 'H':"Hit";
                bat++;
                ball=0;
                strike=0;
                cnt=1;
                break;

        case 'O':"Out";
                if (out<2)
                    {
                        ++out;
                        ++bat;
                    }
                else if (out==2)
                {
                    cout<<"\n\t\t\t\t\Three Outs!!!\n\n\n";
                        newInning(atBat,out,strike,ball,cnt,run,bat);
                }
                break;
        case 'R': "Run In";
                run++;

        default: cout<<"\n\n**********Invalid selection. Please enter S, B, F, G, H, O, or R**********\n\n";
        }

}
    while (inning!='n' || inning!='N');

    cin.get();
    cin.get();
    return 0;
}
int newInning(char,int, int, int, int, int, int)
{
    char inning, atBat;
    int ball=0, bat=1, out=0, strike=0, hs=1, vs=1, cnt=1, run=0;
    cout<<"Is there another half Inning (Y/N)? ";
    cin>>inning;
    if (inning=='n' ||inning=='N')
        return 0;

 if(atBat=='H')
 {
      atBat='V';
      cout<<"\n**********Visitor team is at bat Home team is in the field**********\n\n";
      out=0;
      strike=0;
      ball=0;
      cnt=1;
      run=0;
      bat=1;
 }
 else if (atBat=='V')
 {
      atBat='H';
      cout<<"\n**********Home team is at bat Visitor team is in the field**********\n\n";
      out=0;
      strike=0;
      ball=0;
      cnt=1;
      run=0;
      bat=1;
 }
 return inning;
}
Last edited on
Line 105: You're declared 7 argument types, but you haven't given your arguments names, therefore you can't be using the values that were passed when newInning was called at line 89. The local variables you declare at line 108 are not the same variables as were declared with the same names in main.
Thanks for the reply AbstractionAnon.

Just to be sure I am understanding you correctly, I need to pass the argument into the function on line 105. I.E. int newInning(char atBat, int strike, int ball, int out, int cnt, int bat, int run)

Do the local variables need to be the same on line 108 as they are in the main? I no longer need the "hs" nor the "vs" because the scores will not be displayed at the end.

Thanks again,
Nightspear
Just to be sure I am understanding you correctly, I need to pass the argument into the function on line 105.

Correct.

Do the local variables need to be the same on line 108 as they are in the main?

The argument names you use on line 105 do not need to be the same as in main.
You want to delete the variables on line 108 that you're using as agument names.
AbstractionAnon,

When I pass the variables into the function I get a "Shadow of a Variable"error. So, I took out the declaration of the variables in the function, which makes it run with the same loop termination problem. It seems that the if statement on line 125 is not running when 'n' or 'N' is entered. Do you have any ideas why? I also added a few if else statements at the beginning to address the suffix question I had earlier.

Here is my revised 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
#include <iostream>
#include <string>
using namespace std;
int newInning(char atBat, int out, int strike, int ball, int cnt, int run, int bat);

int main()
{
    char code, inning, atBat='V';;
    int ball=0, bat=1, out=0, strike=0, cnt=1, run=0;

    //menu
    cout<<"Delayed Spring Training 2014\nUmpire's Assistant At Your Service\n\n";

    cout<<"\n\nThe menu is: "<<endl;
    cout<<"\n Item \t Code Description \t Item\t Code Description"<<endl;
    cout<<"  R\tRun Scored \t \t  O\tOut"<<endl;
    cout<<"  F\tFoul Ball \t \t  S\tStrike"<<endl;
    cout<<"  B\tBall\t\t\t  H\tHit"<<endl;
    cout<<"  G\tGame Canceled"<<endl<<endl;
    cout<<"\n********** Visitor team is at bat Home team is in the field**********\n\n";

    do
    {
        if (bat==1)
        {cout<<"Inning Status\tBatter # "<<bat<<"\t # Balls "<<ball;
        cout<<" # Strike "<<strike<<" # Outs "<<out<<" # Runs "<<run<<endl;
        cout<<bat<<"st Batter (count of "<<cnt<<"): ";
        cin>>code;}
        else if(bat==2)
        {cout<<"Inning Status\tBatter # "<<bat<<"\t # Balls "<<ball;
        cout<<" # Strike "<<strike<<" # Outs "<<out<<" # Runs "<<run<<endl;
        cout<<bat<<"nd Batter (count of "<<cnt<<"): ";
        cin>>code;}
        else if(bat==3)
        {cout<<"Inning Status\tBatter # "<<bat<<"\t # Balls "<<ball;
        cout<<" # Strike "<<strike<<" # Outs "<<out<<" # Runs "<<run<<endl;
        cout<<bat<<"rd Batter (count of "<<cnt<<"): ";
        cin>>code;}
        else
        {cout<<"Inning Status\tBatter # "<<bat<<"\t # Balls "<<ball;
        cout<<" # Strike "<<strike<<" # Outs "<<out<<" # Runs "<<run<<endl;
        cout<<bat<<"th Batter (count of "<<cnt<<"): ";
        cin>>code;}
        switch (toupper(code))
        {
         case 'S':"Strike";
                    if (strike<2)
                        {++strike;
                        ++cnt;}
                    else if (strike==2)
                    {
                        strike=0;
                        ball=0;
                        cnt=1;
                        out++;
                        ++bat;
                        if (out==3)
                           newInning(atBat,out,strike,ball,cnt,run,bat);

                    }
                        break;

         case 'B':"Ball";
                if (ball<3)
                   { ++ball;
                    ++cnt;}
                else if (ball==3)
                   {
                        ball=0;
                        strike=0;
                        cnt=1;
                        ++bat;

                   }
                    break;

        case 'F':"Foul Ball";
                cnt++;
                if (strike<2)
                    strike++;

                break;

        case 'G':"Game Canceled";
                return 0;
                break;

        case 'H':"Hit";
                bat++;
                ball=0;
                strike=0;
                cnt=1;
                break;

        case 'O':"Out";
                if (out<2)
                    {
                        ++out;
                        ++bat;
                    }
                else if (out==2)
                {
                    cout<<"\n\t\t\t\tThree Outs!!!\n\n\n";
                        newInning(atBat,out,strike,ball,cnt,run,bat);
                }
                break;
        case 'R': "Run In";
                run++;

        default: cout<<"\n\n**********Invalid selection. Please enter S, B, F, G, H, O, or R**********\n\n";
        }

}
    while (inning!='n');

    cin.get();
    cin.get();
    return 0;
}
int newInning(char atBat, int out, int strike, int ball, int cnt, int run, int bat)
{
    char inning;
    cout<<"Is there another half Inning (Y/N)? ";
    cin>>inning;
    if (inning=='n' || inning=='N')
              return 0;

    if(atBat=='H')
         {
              atBat='V';
              cout<<"\n**********Visitor team is at bat Home team is in the field**********\n\n";
              out=0;
              strike=0;
              ball=0;
              cnt=1;
              run=0;
              bat=1;
         }
     else
         {
              atBat='H';
              cout<<"\n**********Home team is at bat Visitor team is in the field**********\n\n";
              out=0;
              strike=0;
              ball=0;
              cnt=1;
              run=0;
              bat=1;
         }

     return inning;
    }
AbstractionAnon,

Just letting you know I found the problem. Instead of passing in the arguments I passed in their addresses. My program seems to be running perfectly now.

Thank you again for all your help,
Nightspear
It seems that the if statement on line 125 is not running when 'n' or 'N' is entered.

The statement is executing, however at lines 58 and 104 you ignore the value returned by the function call. You probably want to check to see if a 0 was returned.

Lines 132-137 and 143-148, these statements won't change the values of the corresponding variables in main. To do that, you need to pass the variables by reference. You're currently passing them by value, so you're operating a a copy of the variable.
Topic archived. No new replies allowed.