C++ Help Required

blake

School Cricketer
Joined
Jul 15, 2007
Location
Brisbane, Australia
Online Cricket Games Owned
Wasn't sure where to post this - hopefully this is the right forum for it.

Anyway, I'm fairly new to C++. Been learning it a little bit from some online tutorials for a while now - although nothing consistent. I decided it was about time that I had a crack at trying to whip up a quick cricket simulator.

Now, for a C++ beginner, making a simulator is no easy feat. I'm struggling a lot and need some help with my code.

I thought it might have a chance to work - but when I run it, it doesn't seem to work.

So, let me run through the program for you.

Basically, there is no teams, or any players with the exception of Ponting and Flintoff. I have just tried to create a 1 v 1 type scenario with Flintoff bowling to Ponting each ball and then an amount of runs being determined.

Here is my algorithm to determine the amount of runs for each ball:

Batsman is given fixed values for each area. This represents their ability to play a shot in that location. These are the areas: wide outside off, outside off, off stump, middle stump, leg stump and outside leg. There is no other stats for the batsmen except for these. There is a rough number for each. Ricky Ponting's ability wide outside off is 6.125, his ability outside off is 4.1875, off stump is 4, straight is 4.875, leg stump is 6.875 and outside leg stump is 8.125.

These are the only batting values required.

For the bowlers, they have two values. Consistency and skill. Flintoff's consistency is 67.6, and his skill is 68.5. For now, I ruled them like this:

Skill = 100 - FC Bowling Average
Consistency = 100 - FC Bowling Strike Rate / 2

Here is how each ball is determined (a tricky process).

A random number between 1-50 is generated for the bowler. In my program, the variable for this is 'randcons'.

The bowler's consistency is multiplied by 1.66. Then, the random number just generated between 1-50 is added to the consistency * 1.66.

For example:
Flintoff's consistency = 67.6
Random number = 30

67.6 * 1.66 = 112.2
112.2 + 30 = 142.2
142.2 = 142

Final number = 142

This final number is assigned to the variable 'bowlnum'.

Now, we use the bowlnum to find out which area the ball has landed in. The ideal area is off stump, and then outside off is the next best area to bowl in.

Here's an example for this - if the 'bowlnum' is less than 100, the ball is delivered outside leg. If it is between 101-110. it is leg stump. (This might need to be a little more random, because this means it is impossible for Flintoff to bowl on leg stump or outside leg because his consistency is too high. Still, I'll leave it for now.)

So, now all those formulas have calculated the area of the ball. A lot of work just to find the area, I know. Confusing.

Now, just like the batsman has values for each area of the pitch, there are also DEFAULT values for each area of the pitch for the bowler. These do not change. Wherever the ball lands is given the default value for that amount of the pitch. So, if it lands on off stump, the default value for a bowler is 5. Now, simply, we multiply the bowler's skill by the value.

His skill is 68.5. 68.5 * 5 = 342.5

FINAL BOWLBALL = 342.5

Very, very, very confusing and long-winded. Sorry to bore you through all this.

Now, the final 'bowlball' variable is set at 342.5, just for the first ball. This ball has landed in the off stump area.

Time to work out the batsman's now - this is a bit quicker.

First of all, a random number between 4 and 25 is generated. When it is found, it is divided by 10. (Effectively a random number between 0.4 and 2.5)

Now, I mentioned that the batsman has values for each area of how well he plays a ball in that area. Since the ball has landed on off stump, Ponting's value is 4 in that area.

Now, we multiply 4 by the random number (after it has been divided by 10.) In this example, the random number is 1.8.

4 * 1.8 = 7.2.

So, the final BATBALL value is 7.2.

Now, we simply divide BOWLBALL by BATBALL to get FINALBALL.

The higher the FINALBALL = the better the ball. If the finalball is higher than a certain number which I will determine later, then that will mean the batsman is out. If it is say lower than 20, the batsman will score 4 runs. Rough example.

Anyway.. very confusing. Hope you took all that in..

Still..

THE PROBLEM

I have shown the code underneath. Refer to that if you need.

Right now, I haven't bothered making it display how many runs scored or whatever. I JUST want it to say what the FINALBALL variable was for that ball, just so I know how many runs were scored.

The problem? It displays this:

The final area was -1.#IND
Press any key to continue . . .

If this worked, it should say the final area was 50, or 80, or something. I don't know why this is happening though and I am frustrated.

Anyway, this has been a long post - I will be very surprised if anyone can actually help.

If anyone feels up to helping me to work out why it is not displaying properly..
I would be extremely grateful.

Definitely rep and a small vCash donation.

So, please.. pimp my program. :(

Code:
//Cricket Game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//Functions
void match();
void ball();
void blank();

//Players

/*
Value 0-5 = Batting Area
Value 6 = Consistency
Value 7 = Skill
*/

double ponting0 = 6.125;
double ponting1 = 4.1875;
double ponting2 = 4;
double ponting3 = 4.875;
double ponting4 = 6.875;
double ponting5 = 8.125;
double ponting6 = 51;
double ponting7 = 45;
double flintoff0 = 3.75;
double flintoff1 = 2.5;
double flintoff2 = 2.5;
double flintoff3 = 2.75;
double flintoff4 = 4;
double flintoff5 = 5;
double flintoff6 = 67.6;
double flintoff7 = 68.5;

//Opening Dialog
int main ()
{
 cout << "Welcome to Blake's Cricket.  \n"
	  << "This is Blake's first cricket related project. \n"  
	  << "First off we will have Ricky Ponting vs Andrew Flintoff. \n\n"
      << "Australia's Batsman: Ricky Ponting \n"
	  << "England's Bowler: Andrew Flintoff \n\n";
		system("PAUSE");
		match();
}

//Go to the match
void match()
{
	system("CLS");
cout << "Welcome to the one vs one battle (Ponting vs Flintoff)\n\n";
system("PAUSE");
ball();
}

//Function for every ball
void ball()
{
system("CLS");
double bowlnum;
double bowlarea;
double batarea;
double bowlball;
double finalball;
double batnum;
int cntballs;

cntballs ++ ;
if(cntballs == 6, cntovers++, cntballs = 0);

srand(time(0));
int randcons = rand() % 50 + 1;
int randbat = rand() % 25 + 4;

bowlnum = (flintoff6 * 1.66) + randcons;
batnum = randbat / 10;

//Outside Leg
if(bowlnum <= 100){
	bowlarea = 0.75;
	batarea = ponting5;
}

//Leg Stump
else if (bowlnum <= 110 && bowlnum >= 101){
	bowlarea = 2.125;
	batarea = ponting4;
}

//Wide Off
else if (bowlnum <= 117 && bowlnum >= 111){
	bowlarea = 2.875;
	batarea = ponting0;
}

//Straight
else if (bowlnum <= 125 && bowlnum >= 118){
	bowlarea = 4.125;
	batarea = ponting3;
}

//Outside Off
else if (bowlnum <= 140 && bowlnum >= 131){
	bowlarea = 4.875;
	batarea = ponting1;
}

//Off Stump
else if (bowlnum <= 150 && bowlnum >= 141){
	bowlarea = 5;
	batarea = ponting2;
}

bowlball = flintoff7 * bowlarea;

finalball = batarea / bowlball;

cout << "The final area was " << finalball;

system("PAUSE");

blank();
}
void blank()
{
	system("CLS");
	ball();
}
 
I don't know C++ but here's one thing I noticed just reading through:

Code:
int cntballs;

cntballs ++ ;

You've not given cntballs as a value. Surely:

Code:
int cntballs = 0;

Can't you step through line by line to debug it?
 
When I build my program, it doesn't display any errors.

Anyway, the cntballs, cntovers variables aren't functioning yet. They aren't even included yet in my final statement - I will be using them when I get everything else ready. Basically, they are only there to display how many balls and overs have been bowled.. '0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1.1' etc.
 
Can't you debug it though and step through it line by line? I can't imagine coding anything without being able to do that...
 
Blake, I reckon you're using Visual C++ 2008, so here's what you should do.

Set a breakpoint on the first instruction of the match() function. To do this, hover the mouse near the instruction, click on the left pane near it so that it gets a big red button :p Or simply select that instruction and search for break points somewhere in the menus.

The next time you run your program, when the program reaches that instruction, it'll "break" meaning it'll take you back to the code, where you can execute it line by line [by pressing F10] and simultaneously checking what states your variables are in after every instruction, helps a great deal to know what is going wrong. :)
 
Thanks so much Kshitiz - yes, I am using VC++ 2008.

I will try that - hopefully it will help me. Cheers mate. :D
 
I just ran the code and it produced output. However, the numbers were all less than 1 because your bowlball is huge (since you multiplied it by flintoff7) whereas your batarea is tiny (just whatever the default was).

Also, I suggest you initialize all your variables because I kept getting runtime errors and warnings. Its just good practice.
 
Code:
int cntballs;

cntballs ++ ;
[B]if(cntballs == 6, cntovers++, cntballs = 0);[/B]

cntballs has to be initialized to a value first and what does the if loop do? and the cntovers is undeclared!

PS:i get the final value was 1.0766e + 021.so there's no problem with the code i guess but there may be a better alternative than this approach i guess!
 
Last edited:
As barmyarmy said, you have to explicitly initialize cntballs to 0. When you create a new variable in C++ it contains value stored at that location previously which is junk. If you increment that it'll result in unexpected results. The compiler wont show it as an error because it doesnt stop the code from running. cntovers hasn't even been declared. Other than that your main function doesn't return a value. You should either set its return type to void or add a return statement.
 
Chaitan - I haven't yet finished that code. For now, it is just a variable that does not display anywhere. I will fix it up when I get around to writing the actual gameplay up.

AbBh - good point - I'll probably set it to void. I forgot to add a return statement. With the cntovers cntballs thing.. it's just the same as Chaitan's response, I haven't added them into the thing yet so it shouldn't affect the line that I want to write.

Sohum - THANKYOU. This is what I was looking for. What you said makes sense and I will rewrite it to see if it works. I'm pretty sure I just made an amateur mistake (or four) whilst making all the calculations...

Anyway, thankyou everyone for the response. Good to see we have some helpful people here.. and good programmers as well.

Cheers guys. :)
 
Just to note, and I'm not sure how this goes past your compiler, if you're using a variable in your code (such as cntovers) it needs to be declared. Regardless of whether you are semantically using it or not. Initializing variables is just good practice, similar to nulling out allocated memory that you have freed--it prevents hard-to-detect compiler-related bugs from happening.
 
OK, well - I've fixed a little bit of it.

Back with another query.

Most of the time, it works and it says 'The finalball was 79.325', or whatever it is, and I'm fine with that..

However - I am getting a problem sometimes. My next line of code is one that says 'Press any key to continue. . .'.

After this, I wanted to redo the ball so I could find out what the next random code is. I wanted it to go back to the start of the ball function. This works fine.

However, the finalball stays the same for about three seconds, and then it changes. I can press it up to six times before it changes to the next variable. This must be something to do with the random variable not changing often - it only changes every three seconds or so.

Quite often I'm getting a problem as well. Most of the time it changes to 85.6, or 75 or whatever, but it also changes to -1.#IND. I have no idea what this means - whether the variable is too long (too many decimal points?) or what..

Help would be greatly appreciated.




EDIT:

I have fixed the -1.#IND problem after about an hour of work. I realised it meant that the variable was undeclared or it was dividing by 0 or the answer was infinite - that's why it was displaying. I went through my program using breakpoints a lot of times, before finally realising that the code <= 130 and then >= 131 wasn't sufficient - if the answer was something like 130.6 it wouldn't know what to do and it would display that.

Now, I am still having the problem with that it takes quite a few tries to get to the next number - I might have to find some better random generators for C++ because right now I'm a little bit confused.
 
Last edited:
Hey I just read your PM and hadn't been back to this thread before so my response may have been somewhat strange.

Like I said, I'm not too familiar with C++ random number generators, but what may be the problem is that you are seeding the generator every time? Seeding may take a little longer than just generating a number.

What you should do is call "srand" once at the beginning of the program to see it, and then every time you need a new number you call "rand". Take a look at this page. Hope this helps.
 
Cheers Sohum - I will give it a test run.

Edit: Sohum.. Respect! You are an absolute champion mate - thanks for all the help. Now I've got it displaying 50 overs - so I'm making progress. :D
 
Last edited:

Users who are viewing this thread

Top