Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 9 of 9

10192011, 11:53 PM #1
 Join Date
 Oct 2011
 Posts
 41
 Thanks
 27
 Thanked 0 Times in 0 Posts
WIN32 Console Application  Complicated Math  Need Help
Okay, I got a good one for you coding guru's. I'm sort of a noob at coding C++ but I managed to make or partially make a NFL Game Predictor . I thought it would be a good project for me to get back in the coding game. So this is what I came up with.
Code:#define _WIN32_WINNT 0x0500 #include <windows.h> #include <Wincon.h> #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { //Data Table int iTeam1apy; int iTeam2apy; int iTeam1ary; int iTeam2ary; int iTeam1aya; int iTeam2aya; double dTeam1as; double dTeam2as; double dTeam1ai; double dTeam2ai; int iTeam1lp; int iTeam2lp; int iTeam1wl; int iTeam2wl; double dTeam1line; double dTeam2line; double dTeam1 = 10.0; // Home Team Gets 10.0 Points Advantage double dTeam2; double dTeam1p; double dteam2p; double dTotal; double temp; double Dapy = 0.5; double Dary = 1.0; double Daya = 1.5; double Das = 5.0; double Dai = 5.0; //Full Screen Mode HWND hWnd = GetConsoleWindow(); ShowWindow(hWnd,SW_SHOWMAXIMIZED); //Title & Color system("title NFL Game Predictor BETA Handicapping Software"); system("color E2"); //Instructions cout << "Loading NFL Predictor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . \n"; system("PAUSE"); system("cls"); cout << "Instructions: \n \n"; cout << "Enter all the data needed to predict the game. \n \n"; cout << "Offensive Average Passing Yards Per Game \n \n"; cout << "Offensive Average Rushing Yards Per Game \n \n"; cout << "Defensive Average Yards Against Per Game \n \n"; cout << "Defensive Average Sacks Per Game (If less than 1, devide the number of sacks by the number of games played)\n \n"; cout << "Defensive Average Interceptions Per Game (If less than 1, devide the number of interceptions by the number of games played)\n \n"; cout << "Team League Position \n \n"; cout << "Team Win/Lose Streak (Must be more than 3 games) \n \n"; cout << "Sport Book Enter the lines from your sports book \n \n"; system("pause"); system("cls"); //The Home Team cout << "Please Enter The Data For The Home Team. \n"; cout << "Home Team Average Passing Yards Per Game? \n"; cin >> iTeam1apy; cout << "\n"; cout << "Home Team Average Rushing Yards Per Game? \n"; cin >> iTeam1ary; cout << "\n"; cout << "Home Team Average Yards Against Per Game? \n"; cin >> iTeam1aya; cout << "\n"; cout << "Home Team Average Sacks Per Game? \n"; cin >> dTeam1as; cout << "\n"; cout << "Home Team Average Interceptions Per Game? \n"; cin >> dTeam1ai; cout << "\n"; cout << "Home Team League Position? \n"; cin >> iTeam1lp; cout << "\n"; cout << "Home Team Win/Lose Streak? (Must Be Greater Than 3) \n"; cin >> iTeam1wl; cout << "\n"; cout << "Home Team Sports Book Odds, Money Line? (E.g.: 1.85) \n"; cin >> dTeam1line; cout << "\n"; system("pause"); system("cls"); // The Away Team cout << "Please Enter The Data For The Away Team. \n"; cout << "Away Team Average Passing Yards Per Game? \n"; cin >> iTeam2apy; cout << "\n"; cout << "Away Team Average Rushing Yards Per Game? \n"; cin >> iTeam2ary; cout << "\n"; cout << "Away Team Average Yards Against Per Game? \n"; cin >> iTeam2aya; cout << "\n"; cout << "Away Team Average Sacks Per Game? \n"; cin >> dTeam2as; cout << "\n"; cout << "Away Team Average Interceptions Per Game? \n"; cin >> dTeam2ai; cout << "\n"; cout << "Away Team League Position? \n"; cin >> iTeam2lp; cout << "\n"; cout << "Away Team Win/Lose Streak? (Must Be Greater Than 3) \n"; cin >> iTeam2wl; cout << "\n"; cout << "Away Team Sports Book Odds, Money Line? (E.g.: 1.85) \n"; cin >> dTeam2line; cout << "\n"; system("pause"); system("cls"); cout << "CALCULATING ODDS .....\n"; system("pause"); system("CLS"); // The Math // Scoring System: // Average Passing Yards = +0.5 Points Per Yard // Average Passing Yards = +1.0 Points Per Yard // Average Yards Against = 1.5 Points per Yard // Average Sacks = +5.0 Points Per Sack // Average Interceptions = +5.0 Points Per Interception // League Rank = (1) 50.0 Points; (2) 40.0 Points; (3) 30.0 Points; (4) 20.0 Points ... // Win / Lose Streak = (3) +/ 20.0 Points; (4) +/ 25.0 Points; (5) +/ 30.0 Points ... // // Sport Book odds : If dTeam1line < dTeam2line then dTeam1 + 10.0 Points // If dTeam2line < dTeam1line then dTeam1  10.0 Points // // Comparing The Teams: If dTeam1 > dTeam2 then dTeam1 Wins // If dTeam2 > dTeam1 then dTeam2 Wins // // Calculate % for each team: dTeam1 + dTeam2 = dTotal // dTotal / dTeam1 = dTeam1p // dTotal / dTeam2 = dTeam2p // // Display Winning Team and Percantage. // Calculating Odds. // Team 1 temp = iTeam1apy * Dapy; dTeam1 = temp + dTeam1; temp = iTeam1ary * Dary; dTeam1 = temp + dTeam1; temp = iTeam1aya * Daya; dTeam1 = temp + dTeam1; temp = dTeam1as * Das; dTeam1 = temp + dTeam1; temp = dTeam1ai * Dai; dTeam1 = temp + dTeam1; //Team 2 temp = iTeam2apy * Dapy; dTeam2 = temp + dTeam2; temp = iTeam2ary * Dary; dTeam2 = temp + dTeam2; temp = iTeam2aya * Daya; dTeam2 = temp + dTeam2; temp = dTeam2as * Das; dTeam2 = temp + dTeam2; temp = dTeam2ai * Dai; dTeam2 = temp + dTeam2; //If Statments //League Position //Team 1 if (iTeam1lp == 1) dTeam1 = dTeam1 + 50; if (iTeam1lp == 2) dTeam1 = dTeam1 + 40; if (iTeam1lp == 3) dTeam1 = dTeam1 + 30; if (iTeam1lp == 4) dTeam1 = dTeam1 + 20; if (iTeam1lp == 5) dTeam1 = dTeam1 + 10; //Team 2 if (iTeam2lp == 1) dTeam2 = dTeam2 + 50; if (iTeam2lp == 2) dTeam2 = dTeam2 + 40; if (iTeam2lp == 3) dTeam2 = dTeam2 + 30; if (iTeam2lp == 4) dTeam2 = dTeam2 + 20; if (iTeam2lp == 5) dTeam2 = dTeam2 + 10; // Win/Lose Streak if statments // Team 1 if (iTeam1wl == 3) dTeam1 = dTeam1 + 20; if (iTeam1wl == 4) dTeam1 = dTeam1 + 25; if (iTeam1wl >= 5) dTeam1 = dTeam1 + 30; if (iTeam1wl == 3) dTeam1 = dTeam1  20; if (iTeam1wl == 4) dTeam1 = dTeam1  25; if (iTeam1wl >= 5) dTeam1 = dTeam1  30; //Team 2 If Statments if (iTeam2wl == 3) dTeam2 = dTeam2 + 20; if (iTeam2wl == 4) dTeam2 = dTeam2 + 25; if (iTeam2wl >= 5) dTeam2 = dTeam2 + 30; if (iTeam2wl == 3) dTeam2 = dTeam2  20; if (iTeam2wl == 4) dTeam2 = dTeam2  25; if (iTeam2wl >= 5) dTeam2 = dTeam2  30; // Sport Book Lines. If statments if (dTeam1line > dTeam2line) dTeam1 = dTeam1 + 10; if (dTeam2line > dTeam1line) dTeam2 = dTeam2 + 10; // Final cout << "Generated Score: \n"; cout << "Home Team: " << dTeam1 << "Away Team: " << dTeam2; system("pause"); //The End system("pause"); return EXIT_SUCCESS; }
Also... Using double temp; as a variable to store temporary data, does that make sence?
Please, I'm looking forward to your help and suggestions.
Kind Regards
Mat P.
10202011, 01:13 AM
#2
I don't know anything about baseball , so I don't know what to enter to test your program...
If you would like to give us an example input and an example output, I could take a look.
With your math, you can do this:
Code:dTeam1 += (iTeam1apy * Dapy); dTeam1 += (iTeam1ary * Dary); dTeam1 += (iTeam1aya * Daya); dTeam1 += (dTeam1as * Das); dTeam1 += (dTeam1ai * Dai);
Also, I've found sitting down with a pencil, paper, and calculator, you can make short work of figuring out complex math.
Users who have thanked bobleny for this post:
matz0rz (10202011)
10202011, 04:00 AM
#3
 Join Date
 Oct 2011
 Posts
 41
 Thanks
 27
 Thanked 0 Times in 0 Posts
Thanks man, Here is an example of data I would enter.
Home Team
Average Passing Yards Per Game? 200
Average Rushing Yards Per Gamer? 150
Average Yards Against Per Game? 250
Average Sacks Per Game? 0.90
Average Interceptions Per Game? 1.20
Team League Position? 5
Win/Lose Streak 3
Sport Books Line: 1.25
Away Team
Average Passing Yards Per Game? 100
Average Rushing Yards Per Gamer? 50
Average Yards Against Per Game? 250
Average Sacks Per Game? 0.50
Average Interceptions Per Game? 0.20
Team League Position? 15
Win/Lose Streak 5
Sport Books Line: 3.00
NOTE: The Home Team Should Obviously Score Higher Than The Away Team. But the resault is a bunch of random numbers with a couple letters. I'm thinking there is something wrong with the data type. Thanks for your help man.
Last edited by matz0rz; 10202011 at 04:09 AM.
10202011, 05:48 AM
#4
The reason you are getting the random numbers and letters is because you are using dTeam2 without initializing it.
I'm surprised you are not getting an error, but I'm more surprised the program doesn't blow up when ran. Before you start calculating your results, you need to set dTeam2 equal to something; I would set it to 0. I usually initialize everything. This will get rid of your random numbers and letters.
That should help you out.
Also, if you would like:
Code:dTeam1 += 30; // You can also do this dTeam1 = dTeam1 + 30; // instead of this // Like wise, you can do this: dTeam1 = 30; // I believe you can also do this too: dTeam1 *= 30; // I've never tried it, but if * works, this should work too: dTeam1 /= 30;
Users who have thanked bobleny for this post:
matz0rz (10202011)
10202011, 06:27 AM
#5
 Join Date
 Oct 2011
 Posts
 41
 Thanks
 27
 Thanked 0 Times in 0 Posts
I'll try it tomorrow morning and let you know then. Thanks man
10212011, 05:45 PM
#6
 Join Date
 Oct 2011
 Posts
 41
 Thanks
 27
 Thanked 0 Times in 0 Posts
I think I got it to work, here's the final code thanks to your help.
Code:#define _WIN32_WINNT 0x0500 #include <windows.h> #include <Wincon.h> #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { //Data Table int iTeam1apy = 0; int iTeam2apy = 0; int iTeam1ary = 0; int iTeam2ary = 0; int iTeam1aya = 0; int iTeam2aya = 0; double dTeam1as = 0; double dTeam2as = 0; double dTeam1ai = 0; double dTeam2ai = 0; int iTeam1lp = 0; int iTeam2lp = 0; int iTeam1wl = 0; int iTeam2wl = 0; double dTeam1line = 0; double dTeam2line = 0; double dTeam1 = 10.0; // Home Team Gets 10.0 Points Advantage double dTeam2 = 0; double dTeam1p = 0; double dTeam2p = 0; double dTotal = 0; double temp = 0; double Dapy = 0.5; double Dary = 1.0; double Daya = 0.5; double Das = 5.0; double Dai = 5.0; //Full Screen Mode HWND hWnd = GetConsoleWindow(); ShowWindow(hWnd,SW_SHOWMAXIMIZED); //Title & Color system("title NFL Game Predictor BETA Handicapping Software"); system("color E2"); //Instructions cout << "Loading NFL Predictor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . \n"; system("PAUSE"); system("cls"); cout << "Instructions: \n \n"; cout << "Enter all the data needed to predict the game. \n \n"; cout << "Offensive Average Passing Yards Per Game \n \n"; cout << "Offensive Average Rushing Yards Per Game \n \n"; cout << "Defensive Average Yards Against Per Game \n \n"; cout << "Defensive Average Sacks Per Game (If less than 1, devide the number of sacks by the number of games played)\n \n"; cout << "Defensive Average Interceptions Per Game (If less than 1, devide the number of interceptions by the number of games played)\n \n"; cout << "Team League Position \n \n"; cout << "Team Win/Lose Streak (Must be more than 3 games) \n \n"; cout << "Sport Book Enter the lines from your sports book \n \n"; system("pause"); system("cls"); //The Home Team cout << "Please Enter The Data For The Home Team. \n"; cout << "Home Team Average Passing Yards Per Game? \n"; cin >> iTeam1apy; cout << "\n"; cout << "Home Team Average Rushing Yards Per Game? \n"; cin >> iTeam1ary; cout << "\n"; cout << "Home Team Average Yards Against Per Game? \n"; cin >> iTeam1aya; cout << "\n"; cout << "Home Team Average Sacks Per Game? \n"; cin >> dTeam1as; cout << "\n"; cout << "Home Team Average Interceptions Per Game? \n"; cin >> dTeam1ai; cout << "\n"; cout << "Home Team League Position? \n"; cin >> iTeam1lp; cout << "\n"; cout << "Home Team Win/Lose Streak? (Must Be Greater Than 3) \n"; cin >> iTeam1wl; cout << "\n"; cout << "Home Team Sports Book Odds, Money Line? (E.g.: 1.85) \n"; cin >> dTeam1line; cout << "\n"; system("pause"); system("cls"); // The Away Team cout << "Please Enter The Data For The Away Team. \n"; cout << "Away Team Average Passing Yards Per Game? \n"; cin >> iTeam2apy; cout << "\n"; cout << "Away Team Average Rushing Yards Per Game? \n"; cin >> iTeam2ary; cout << "\n"; cout << "Away Team Average Yards Against Per Game? \n"; cin >> iTeam2aya; cout << "\n"; cout << "Away Team Average Sacks Per Game? \n"; cin >> dTeam2as; cout << "\n"; cout << "Away Team Average Interceptions Per Game? \n"; cin >> dTeam2ai; cout << "\n"; cout << "Away Team League Position? \n"; cin >> iTeam2lp; cout << "\n"; cout << "Away Team Win/Lose Streak? (Must Be Greater Than 3) \n"; cin >> iTeam2wl; cout << "\n"; cout << "Away Team Sports Book Odds, Money Line? (E.g.: 1.85) \n"; cin >> dTeam2line; cout << "\n"; system("pause"); system("cls"); cout << "CALCULATING ODDS .....\n"; system("pause"); system("CLS"); // The Math // Scoring System: // Average Passing Yards = +0.5 Points Per Yard // Average Passing Yards = +1.0 Points Per Yard // Average Yards Against = 1.5 Points per Yard // Average Sacks = +5.0 Points Per Sack // Average Interceptions = +5.0 Points Per Interception // League Rank = (1) 50.0 Points; (2) 40.0 Points; (3) 30.0 Points; (4) 20.0 Points ... // Win / Lose Streak = (3) +/ 20.0 Points; (4) +/ 25.0 Points; (5) +/ 30.0 Points ... // // Sport Book odds : If dTeam1line < dTeam2line then dTeam1 + 10.0 Points // If dTeam2line < dTeam1line then dTeam1  10.0 Points // // Comparing The Teams: If dTeam1 > dTeam2 then Home Team Wins // If dTeam2 > dTeam1 then Away Team Wins // // Calculate % for each team: dTeam1 + dTeam2 = dTotal // dTotal / dTeam1 = dTeam1p // dTotal / dTeam2 = dTeam2p // // Display Winning Team and Percantage. // Calculating Odds. // Team 1 // temp = iTeam1apy * Dapy; //dTeam1 = temp + dTeam1; // temp = iTeam1ary * Dary; // dTeam1 = temp + dTeam1; // temp = iTeam1aya * Daya; // dTeam1 = temp + dTeam1; // temp = dTeam1as * Das; // dTeam1 = temp + dTeam1; // temp = dTeam1ai * Dai; // dTeam1 = temp + dTeam1; dTeam1 += (iTeam1apy * Dapy); dTeam1 += (iTeam1ary * Dary); dTeam1 += (iTeam1aya * Daya); dTeam1 += (dTeam1as * Das); dTeam1 += (dTeam1ai * Dai); //Team 2 // temp = iTeam2apy * Dapy; // dTeam2 = temp + dTeam2; // temp = iTeam2ary * Dary; // dTeam2 = temp + dTeam2; // temp = iTeam2aya * Daya; // dTeam2 = temp + dTeam2; // temp = dTeam2as * Das; // dTeam2 = temp + dTeam2; // temp = dTeam2ai * Dai; // dTeam2 = temp + dTeam2; dTeam2 += (iTeam2apy * Dapy); dTeam2 += (iTeam2ary * Dary); dTeam2 += (iTeam2aya * Daya); dTeam2 += (dTeam2as * Das); dTeam2 += (dTeam2ai * Dai); //If Statments //League Position //Team 1 if (iTeam1lp == 1) dTeam1 += 50; // dTeam1 = dTeam1 + 50; if (iTeam1lp == 2) dTeam1 += 40; // dTeam1 = dTeam1 + 40; if (iTeam1lp == 3) dTeam1 += 30; // dTeam1 = dTeam1 + 30; if (iTeam1lp == 4) dTeam1 += 20; // dTeam1 = dTeam1 + 20; if (iTeam1lp == 5) dTeam1 += 10; // dTeam1 = dTeam1 + 10; //Team 2 if (iTeam2lp == 1) dTeam2 += 50; // dTeam2 = dTeam2 + 50; if (iTeam2lp == 2) dTeam2 += 40; // dTeam2 = dTeam2 + 40; if (iTeam2lp == 3) dTeam2 += 30; // dTeam2 = dTeam2 + 30; if (iTeam2lp == 4) dTeam2 += 20; // dTeam2 = dTeam2 + 20; if (iTeam2lp == 5) dTeam2 += 10; // dTeam2 = dTeam2 + 10; // Win/Lose Streak if statments // Team 1 if (iTeam1wl == 3) dTeam1 += 20; // dTeam1 = dTeam1 + 20; if (iTeam1wl == 4) dTeam1 += 25; // dTeam1 = dTeam1 + 25; if (iTeam1wl >= 5) dTeam1 += 30; // dTeam1 = dTeam1 + 30; if (iTeam1wl == 3) dTeam1 = 20; // dTeam1 = dTeam1  20; if (iTeam1wl == 4) dTeam1 = 25; // dTeam1 = dTeam1  25; if (iTeam1wl >= 5) dTeam1 = 30; // dTeam1 = dTeam1  30; //Team 2 If Statments if (iTeam2wl == 3) dTeam2 += 20; // dTeam2 = dTeam2 + 20; if (iTeam2wl == 4) dTeam2 += 25; // dTeam2 = dTeam2 + 25; if (iTeam2wl >= 5) dTeam2 += 30; // dTeam2 = dTeam2 + 30; if (iTeam2wl == 3) dTeam2 = 20; // dTeam2 = dTeam2  20; if (iTeam2wl == 4) dTeam2 = 25; // dTeam2 = dTeam2  25; if (iTeam2wl >= 5) dTeam2 = 30; // dTeam2 = dTeam2  30; // Sport Book Lines. If statments if (dTeam1line < dTeam2line) dTeam1 += 10; // dTeam1 = dTeam1 + 10; if (dTeam2line < dTeam1line) dTeam2 += 10; // dTeam2 = dTeam2 + 10; // Final dTotal = dTeam1 + dTeam2; dTeam1p = (dTeam1 / dTotal) * 100; dTeam2p = (dTeam2 / dTotal) * 100; cout << "Generated Odds: \n"; cout << "Home Team: " << " " << dTeam1p << "% Chance To Win \n" << " Away Team: " << " " << dTeam2p << "% Chance To Win\n"; //The End system("pause"); return EXIT_SUCCESS; }
10212011, 07:24 PM
#7
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,074
 Thanks
 1
 Thanked 328 Times in 324 Posts
Glad you got your program working but a couple suggestions to make the program easier to work with and easier to debug.
1) Put all the team variables within a struct. That way you don't have so many local variables loose and if you decide to add a new variable, you only need to add it once and all the teams get it.
2) If you use the first suggestion you could split off some of the repetitive tasks to functions or at least make it a loop so you don't have the exact same code with the only difference being what team it is changing.
Doing both of those suggestions would dramatically reduce the amount of code in your program.
OracleGuy
Users who have thanked oracleguy for this post:
matz0rz (10222011)
10222011, 12:07 AM
#8
 Join Date
 Oct 2011
 Posts
 41
 Thanks
 27
 Thanked 0 Times in 0 Posts
10222011, 01:31 AM
#9
My favorite c and c++ reference site is:
http://www.cplusplus.com/
They have a good c++ structure tutorial:
http://www.cplusplus.com/doc/tutorial/structures/
Your structure will probably look a lot like this:
That allows you to do this:Code:struct Team { int iTeamApy; int iTeamAry; int iTeamAya; double dTeamAs; double dTeamAi; };
If you want to get really fancy, you can create a team object to store all the properties and methods for dealing with your teams. This is of course overkill for your program, but after you try the structure thing, you may wish to give object oriented programing:Code:Team team1; Team team2; team1.dTeamAi = 40; team2.dTeamAi = 26;
http://www.cplusplus.com/doc/tutorial/classes/
For other c++ related tutorials you may wish to check this out:
http://www.cplusplus.com/doc/tutorial/
Oh, I forgot about the pointer thing. You can create a function that gets all of the data for your team like this:
Then, in your main program you can call your function like this:Code:void GetTeamInfo(Team *pTeam) { cout << "Team Average Passing Yards Per Game? \n"; cin >> pTeam > iTeamApy; cout << "\n"; cout << "Team Average Rushing Yards Per Game? \n"; cin >> pTeam > iTeamAry; cout << "\n"; cout << "Team Average Yards Against Per Game? \n"; cin >> pTeam > iTeamAya; cout << "\n"; cout << "Team Average Sacks Per Game? \n"; cin >> pTeam > dTeamAs; cout << "\n"; cout << "Team Average Interceptions Per Game? \n"; cin >> pTeam > dTeamAi; cout << "\n"; system("pause"); system("cls"); }
You can do something similar with the math portion of your program too.Code:Team team1; Team team2; cout << "Please Enter The Data For The Home Team. \n"; GetTeamInfo(&team1); cout << "Please Enter The Data For The Away Team. \n"; GetTeamInfo(&team2); cout << team1.iTeamApy << "\n"; cout << team2.iTeamApy << "\n";
You may wish to look into c++ pointers as well:
http://www.cplusplus.com/doc/tutorial/pointers/
Last edited by bobleny; 10222011 at 01:51 AM.
Users who have thanked bobleny for this post:
matz0rz (10292011)