Program Not Working

I have compiled a program in Dev-C++ and the compiler is showing no errors. However, when I run the program, the program pops up, but then another pop up window apprea reading "Indian Cricket Coach 2011.exe has stopped working". I have gone through the code and I am unable to find any mistakes.

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
199
200
201
#include <iostream.h>
#include <string>

class Team
{
      public:
      std::string Name;
      std::string Location;
      std::string GoverningBody;
      std::string FirstTestMatch;
      std::string FirstODIMatch;
      std::string FirstT20Match;
      std::string PresidentOfGovBody;
      std::string TestCaptain;
      std::string ODICaptain;   
      std::string T20Captain;
};
      
      class TestTeam
      {
      public:
      std::string Name;
      std::string DateOfBirth;
      std::string MajorTeams;
      std::string BattingStyle;
      std::string BowlingStyle;
      unsigned int ODIScore;
      unsigned int TestMatchesPlayed;
      unsigned int TestInnings;
      unsigned int TestRunsScored;
      double TestBattingAvg;
      unsigned int TestHighScore;
      unsigned int TestHundreds;
      unsigned int TestFifties;
      double TestSR;
      unsigned int TestCatches;
      unsigned int TestWicketsTaken;
      double TestBowlingAvg;
      double TestBowlingSR;
      double TestBowlingER;
      };
      
      class ODITeam
      {
      public:
      std::string Name;
      std::string DateOfBirth;
      std::string MajorTeams;
      std::string BattingStyle;
      std::string BowlingStyle;
      unsigned int ODIScore;
      unsigned int ODIMatchesPlayed;
      unsigned int ODIInnings;
      unsigned int ODIRunsScored;
      double ODIBattingAvg;
      unsigned int ODIHighScore;
      unsigned int ODIHundreds;
      unsigned int ODIFifties;
      double ODISR;
      unsigned int ODICatches;
      unsigned int ODIWicketsTaken;
      double ODIBowlingAvg;
      double ODIBowlingSR;
      double ODIBowlingER;
      };

int main()
{
     system("color 17");
     

     Team Team[8];
     Team[1].Name = "India";
     Team[1].Location = "Indian Subcontinent";
     Team[1].GoverningBody = "BCCI";
     Team[1].PresidentOfGovBody = "Shashank Manohar";
     Team[1].FirstTestMatch = "June 25, 1932 vs England";
     Team[1].FirstODIMatch = "July 13, 1974 vs England";
     Team[1].FirstT20Match = "December 1, 2007 vs South Africa";
     Team[1].TestCaptain = "MS Dhoni";
     Team[1].ODICaptain = "MS Dhoni";
     Team[1].T20Captain = "MS Dhoni";
     
     Team[2].Name = "Australia";
     Team[1].Location = "Oceania";
     Team[1].GoverningBody = "ACA";
     Team[1].PresidentOfGovBody = "Shashank Manohar";
     Team[1].FirstTestMatch = "June 25, 1932 vs England";
     Team[1].FirstODIMatch = "July 13, 1974 vs England";
     Team[1].FirstT20Match = "December 1, 2007 vs South Africa";
     Team[1].TestCaptain = "Ricky Ponting";
     Team[1].ODICaptain = "Michael Clarke";
     Team[1].T20Captain = "Michael Clarke";

     ODITeam ODIPlayer[8][11];
     // Virender Sehwag - Player 1
     ODIPlayer[1][1].Name = "Virender Sehwag "; 
     ODIPlayer[1][1].DateOfBirth = "October 20, 1978";
     ODIPlayer[1][1].MajorTeams = "India, Asia XI, Delhi Daredevils, Delhi, ICC World XI";
     ODIPlayer[1][1].BattingStyle = "Right-hand Opener";
     ODIPlayer[1][1].BowlingStyle = "Right-arm Offbreak";
     ODIPlayer[1][1].ODIMatchesPlayed = 236;
     ODIPlayer[1][1].ODIInnings = 230;
     ODIPlayer[1][1].ODIRunsScored = 7760;
     ODIPlayer[1][1].ODIBattingAvg = 35.1;
     ODIPlayer[1][1].ODIHighScore = 175;
     ODIPlayer[1][1].ODIHundreds = 14;
     ODIPlayer[1][1].ODIFifties = 37;
     ODIPlayer[1][1].ODISR = 104.1;
     ODIPlayer[1][1].ODICatches = 87;
     ODIPlayer[1][1].ODIWicketsTaken = 92;
     ODIPlayer[1][1].ODIBowlingAvg = 42.4;
     ODIPlayer[1][1].ODIBowlingSR = 45.9;
     ODIPlayer[1][1].ODIBowlingER = 5.3;
     

     // Sachin Tendulkar - Player 2
     ODIPlayer[1][2].Name = "Sachin Tendulkar"; 
     ODIPlayer[1][2].DateOfBirth = "April 24, 1973";
     ODIPlayer[1][2].MajorTeams = "India, Asia XI, Mumbai, Mumbai Indians, Yorkshire";
     ODIPlayer[1][2].BattingStyle = "Riht-hand Bat; Test - Middle Order; ODI - Opening";
     ODIPlayer[1][2].BowlingStyle = "Right-arm Offbreak, Legbreak Googly";
     ODIPlayer[1][2].ODIMatchesPlayed = 453;
     ODIPlayer[1][2].ODIInnings = 442;
     ODIPlayer[1][2].ODIRunsScored = 18111;
     ODIPlayer[1][2].ODIBattingAvg = 45.2;
     ODIPlayer[1][2].ODIHighScore = 200;
     ODIPlayer[1][2].ODIHundreds = 48;
     ODIPlayer[1][2].ODIFifties = 95;
     ODIPlayer[1][2].ODISR = 86.3;
     ODIPlayer[1][2].ODICatches = 136;     
     ODIPlayer[1][2].ODIWicketsTaken = 154;
     ODIPlayer[1][2].ODIBowlingAvg = 44.4;
     ODIPlayer[1][2].ODIBowlingSR = 52.2;
     ODIPlayer[1][2].ODIBowlingER = 5.1;

         
     int x;
     int a, b, c, d, e, f;
     int y;
     int z;
     int k;
     int Score;
     int Wicket;
     int BallResult[6];     
     for(x = 1; x <= 2; x++)
     {
           cout << Team[x].Name << endl;
                      for(y = 1; y <= 11; y++)
                {                

                                 cout << ODIPlayer[x][y].Name << endl;
                                 
                }
     cout << "   " << endl;
     }
     srand( time(NULL) );
     for(x = 1; x <=10; x++)
     {
           
     for(z = 1; z <=6; z++)
     {
           BallResult[z] = rand() % 10000 + 1;
     if (BallResult[z] >= 0 && BallResult[z] < 100)
        k == 7;
     else
     if (BallResult[z] >= 100 && BallResult[z] < 900)
        k == 4;
     else
     if (BallResult[z] >= 900 && BallResult[z] < 1200)
        k == 6;
     else
     if (BallResult[z] >= 1200 && BallResult[z] < 5400)
        k == 0;
     else
     if (BallResult[z] >= 5400 && BallResult[z] < 9600)
        k == 1;
     else
     if (BallResult[z] >= 9600 && BallResult[z] < 10000)
        k == 2;
        
     if (k < 7)
          {
               cout << k << " ";
          Score = Score + k;
          }
          else
     if (k == 7)
          {
               cout << "W ";
          Wicket = Wicket + 1;
          }
        }
    cout << endl;
    cout << "-----------------" << endl;
}
    cout << Score << "/" << Wicket << endl;
     
     cin.ignore();
     return 0;
}
The above code worked perfectly and I ran it many times, but once it suddenly stopped responding and has not run since.
closed account (DSLq5Di1)
Run-Time Check Failure #3 - The variable 'k' is being used without being initialized.
1
2
3
4
5
6
7
8
9
     if (BallResult[z] >= 0 && BallResult[z] < 100)
        k == 7; // I presume this should be k = 7?
     else
     if (BallResult[z] >= 100 && BallResult[z] < 900)
        k == 4; // k = 4?
     else
        ... // and so on

     if (k < 7) // run time check failure here 

Also.. C/C++ arrays are zero based. Team[8] has 8 elements ranged 0-7. I recommend a couple of changes,

enum { MAX_TEAM = 8, MAX_PLAYERS = 11 }; // use constants!

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
Team Team[MAX_TEAM];

Team[0].Name = "India"; // Team 1
Team[0].Location = "Indian Subcontinent";

Team[1].Name = "Australia"; // Team 2
Team[1].Location = "Oceania";

ODITeam ODIPlayer[MAX_TEAM][MAX_PLAYERS];

ODIPlayer[0][0].Name = "Virender Sehwag "; // Player 1
ODIPlayer[0][1].Name = "Sachin Tendulkar"; // Player 2
...

for (x = 0; x < 2; x++)
{
    cout << Team[x].Name << endl;

    for (y = 0; y < MAX_PLAYERS; y++)
    {
        cout << ODIPlayer[x][y].Name << endl;
    }
    cout << "   " << endl;
}
// etc.. 

Thanks I did what you suggested, but the problem is not resolving.
closed account (1vRz3TCk)
You need to learn how to index arrays properly;

1
2
3
4
5
int BallResult[6];
//...
for(z = 1; z <=6; z++)
/...
    BallResult[z] //... 

Here when z is 6, BallResult[z] is out of bounds. The index for BallResult is 0 to 5.
closed account (DSLq5Di1)
Ah sorry, there is a few more problems.. Score / Wicket are used but uninitialized, causing run-time check errors on lines 185 & 191.

Your for loop on line 161 is going out of bounds,
1
2
3
4
5
6
for(z = 1; z <=6; z++) // BallResult[6], elements range from 0..5
for (z = 0; z < 6; z++)
{
    BallResult[z] = rand() % 10000 + 1;
    ...
}


-edit-
Oops didn't see your post CodeMonkey ^^
Last edited on
closed account (1vRz3TCk)
You are also probably exceeding the stack size with your data structures.
Thanks CodeMonkey and sloppy9, but again I compiled it and ran, but the program still did not work. Its curious becuase it was working fine, but one fine day I ran the file but it stopped working, even using the improperly-indexed arrays as you pointed out. I don't get any errors while compiling. Even when I open the .cpp file in Visual C++ 2010, the Build menu does not appear on the Menu bar, while the 'Start Debugging' option in the Debug menu is disabled. Could this mean that a build is in progress ? If so, how do I stop the build ?
If I was exceeding the stack size, then why was it running fine till it stopped suddenly without me changing the code ?
closed account (1vRz3TCk)
Maybe Dev-C++ is no longer working fully and generating duff code (if you open the .cpp file in VC it will not build it unless it is part of a project). I don't use Dev-C++ so you may have to look around but try to do a rebuild or a clean build followed by a build (or a the very least delete the .exe and build to make sure that a new exe is built).


With regards to the stack size, to be honest I don't know what the effect of exceeding the stack size would be (other than a posable stack overflow). the Size of ODITeam looks to be about 240 Bytes, multiply that by 11 and that by 8 and you have 21120 Bytes for ODIPlayer. Adding the chunk for Team and you will be heading towards 24 KB, VC has a default function stack size of 16KB.
No I found the problem. It was the class member 'unsigned int ODIScore' in the class 'ODIPlayer'. I removed it and compiled and ran the program and it worked.
Aside

As I have time to check those things I usually take for granted...

sizeof(Team) = 320 bytes x 8 = 2560 bytes
sizeof(ODITeam) = 240 bytes x 8 x 11 = 21120 bytes
sizeof(int) = 4 (assuming 32-bit) x (12 + array of 6) = 72 (? are all the ints used?)

In total = 23752 bytes

For VC++ the default stack size is 1 Mb, so this is just over 2% of the stack,

I take it that gcc uses at least 1 MB by default? (Googling for "gcc" plus "default stack size" I see values of 1, 2, and 4 Mb being mentioned)

Andy
closed account (1vRz3TCk)
For VC++ the default stack size is 1 Mb, so this is just over 2% of the stack,

For the whole program, yes.

I wrote:
VC has a default function stack size of 16KB.

http://msdn.microsoft.com/en-us/library/7yhee2f0.aspx
@CodeMonkey - Sorry : I obviously need to up the "resolution" I'm using to read the more detailed threads...

But C6262 is an analysis warning that you're using more than sensible for a given function, rather than an actual limit. I think you can the whole of the available stack in one go (well, what's left after the CRT start-up has got at it)

Compared with 16 k, the stack usage is approx 50% more than desired. While I would not usually (?) expect to use that many variables, 2% doesn't much worry me, as that corresponds (very naively) to a call depth of 50, which I would find much more alarming.

Does anyone happen to know the granularity used for per-function stack allocations? As 64 k is the usual system granularity, and the warning is at 16k, this granularity must be 16k or less.

As I only own the Std version of VC++, I don't get warning C6262 when I compile the code as listed above (plus your assorted repairs). But I must remember to check this kind of thing out if I get to use the Team edition.

Andy
Last edited on
closed account (1vRz3TCk)
I was probably a little too terse in my posts. I don't mean to say that it is an error, just that putting large chunks of memory on the stack on a single function is something to be wary of.
I totally agree with you.

As I had the time to check what going, I was decided to follow it up. And now have a better feel for what the stack is getting up to and, thanks to your post, what's recommended.
Topic archived. No new replies allowed.