ok so in my program i'm supposed to read data from a text file and store it in arrays then make calculations from my array.

so there will be three functions for the program 1 will be the readfile() which will read the file the other will be calculatedata() which will pass the arrays as parameters from my readfile() then calculate three different calculations. my last function is obtainreport() which will return and report my calculaltions in a .txt file

now i will be reading NFL stats from this year's season, the file below is the data i will be reading. i will store the yards from every team in an array and then add them all up then divide by 1728 converting it into miles that's my first calculation. my second calculation will simply be adding all the passing yards up and reporting it, and my third calculation will be sorting through all the teams points and reporting the highest point total.

now like i said before i would have to store all the numbers in yards, pass yards and points and store them in seperate. i have written some code, but for some reason the program is not behaving like i want it to. it's storing the right values in the arrays but when i pass the values through the calculatedata() the array is not being passed through and is giving me just a single value.

can someone help me how should i go about doing this?? i will comment my code so you can see exactly what it's doing and what i want it to do. THANK YOU!!

RK TEAM YDS YDS/G PASS P YDS/G RUSH R YDS/G PTS PTS/G
1 Philadelphia 4951 412.6 3066 255.5 1885 157.1 271 22.6
2 New Orleans 4946 449.6 3566 324.2 1380 125.5 362 32.9
3 New England 4724 429.5 3510 319.1 1214 110.4 331 30.1
4 Green Bay 4414 401.3 3345 304.1 1069 97.2 382 34.7
5 Carolina 4386 398.7 2898 263.5 1488 135.3 252 22.9
6 Dallas 4291 390.1 2988 271.6 1303 118.5 270 24.5
7 San Diego 4246 386.0 3037 276.1 1209 109.9 249 22.6
8 Houston 4177 379.7 2508 228.0 1669 151.7 293 26.6
9 Detroit 4144 376.7 2997 272.5 1147 104.3 316 28.7
10 Pittsburgh 4120 374.5 2914 264.9 1206 109.6 233 21.2
11 NY Giants 4111 373.7 3206 291.5 905 82.3 252 22.9
12 Oakland 4106 373.3 2465 224.1 1641 149.2 260 23.6
13 Atlanta 4025 365.9 2730 248.2 1295 117.7 259 23.5
14 Buffalo 3800 345.5 2458 223.5 1342 122.0 261 23.7
15 Tampa Bay 3742 340.2 2624 238.5 1118 101.6 199 18.1
16 Chicago 3729 339.0 2389 217.2 1340 121.8 288 26.2
17 Baltimore 3692 335.6 2604 236.7 1088 98.9 272 24.7
18 Cincinnati 3681 334.6 2488 226.2 1193 108.5 259 23.5
19 Seattle 3582 298.5 2333 194.4 1249 104.1 216 18.0
20 Washington 3546 322.4 2596 236.0 950 86.4 183 16.6
21 Tennessee 3515 319.5 2540 230.9 975 88.6 226 20.5
22 Miami 3480 316.4 2268 206.2 1212 110.2 212 19.3
23 Arizona 3473 315.7 2270 206.4 1203 109.4 213 19.4
24 Minnesota 3464 314.9 1969 179.0 1495 135.9 214 19.5
25 NY Jets 3463 314.8 2359 214.5 1104 100.4 256 23.3
26 Denver 3441 312.8 1684 153.1 1757 159.7 221 20.1
27 San Francisco 3395 308.6 1979 179.9 1416 128.7 262 23.8
28 Kansas City 3346 304.2 2011 182.8 1335 121.4 153 13.9
29 Cleveland 3255 295.9 2190 199.1 1065 96.8 165 15.0
30 St. Louis 3251 295.5 2099 190.8 1152 104.7 140 12.7
31 Indianapolis 3080 280.0 1991 181.0 1089 99.0 150 13.6
32 Jacksonville 2750 250.0 1444 131.3 1306 118.7 138 12.5

//functions.cpp

#include <iostream>
#include <fstream>
#include<string>
#include"functions.h"
#include<iomanip>
#define FILE_IN "NFL_stats.txt"//name of data file
using namespace std;

//bringing in my parameters that i set in my driver.cpp
void readFile(string teams,float yards[],float passyards[],float points[])
{
	
        //i will be trashing the first line in the file 
	string trash;
	float ftrash1, ftrash2, ftrash3,ftrash4,ftrash5;//the rest of the columns that
                                                        //are trash
	int i =0;
	

	ifstream input; //name of ifstream

	input.open(FILE_IN);//opening the file

	if(!input)//if it does not open exit out
	{
		cout<<"\n Can't find input file " << FILE_IN;
		cout<<"\n Exiting program, bye bye \n ";
		exit(1);
	}
	
        //getting my first line and trashing it the rank, yards yp/g line
	getline(input,trash);
	
	while(!input.eof())//reading until the end of file
	{
	input>>trash;//trashing numbers
	input.ignore();//removing enter key
	

   //getting team names after a tab
	
getline(input,teams,'\t');

//storing yards column passyards column and points column in array the rest
   //are being trashed since i will not be using them tried to do a for loop here
   // but to no avail program would not read in values like this method does 
   //could this be what im doing wrong??

input>>yards[i]>>ftrash1>>passyards[i]>>ftrash2>>ftrash3>>ftrash4>>points[i]>>ftrash5;
	
	//couting so the user can see the data this is how i know values are where
        //they are supposed to be.

cout<<teams<<"\t"<<yards[i]<<"\t"<<ftrash1<<"\t"<<passyards[i]<<"\t"<<ftrash2<<\t"<<ftrash3<<"\t"<<ftrash4<<"\t"<<points[i]<<"\t"<<ftrash5<<"\n";
	
	
}
	

}
void calculatedata(float yards[], float passyards[], float points[])
{
	float sum = 0., miles = 0.;
	
	for (int i = 0; i < 32; ++i)
	{                       // ALL VALUES FROM READFILE() yards[i] ARE NOT IN THERE
		sum+=yards[i]; //yards[i] IS COMING OUT TO 2750 WHICH IS WRONG!!!
	}

		miles =sum/1728; 
		cout<<miles;          //miles is coming out to 1.59144 WAY WRONG

}

void writeOutput()
{
	
}
Driver.cpp

#include <iostream>
#include <fstream>
#include<string>
#include "functions.h"

using namespace std;

int main()
{
	float yards[32], passyards[32],points[32];
	string teams;
	for(int i = 0; i<32; i++)
	{
		yards[i]=0;
		passyards[i]=0;
		points[i]=0;
	}
	
	
	
	readFile (teams, yards,passyards,points);
	calculatedata(yards, passyards, points);
	writeOutput();

	

return 0;
}
//functions.h

using namespace std;
void readFile(string teams,float yards[32],float passyards[32],float points[32] );
void calculatedata(float yards[32], float passyards[32], float points[32]);
void writeOutput();

PLEASE HELP ME FIGURE THIS OUT!

Recommended Answers

All 13 Replies

Initializing variables to zero.

float yards[32] = {0.0f},
      passyards[32] = {0.0f}, 
      points[32] = {0.0f};

If you're using visual studio you can set a "breakpoint" and debug the application.
See the link in my signature for a youtube video on it.

Chances are you aren't reading them from the file properly--or perhaps the float datatype doesn't have the capacity to hold such a large sum of numbers. Try a double.

Personally, I would have done it somewhat differently, using an array of the following structure:

struct TeamRecord
{
    string name;
    int yards;
    int passyards;
    int points;
}

But that's just me. Note that I used integers for the various stats; this is because all of the stats you are interested in are, in fact, whole number values. More about this shortly.

One of the problems you have in the code is that you never increment i in calculatedata( ), which means that you keep reading to the same location over ansd over again... or you would be, if it didn't stop mysteriously on the first line. I'm still trying to figure that out, myself.

I think I've got it figured out. The problem is in the line getline(input, team, '\t'); . Because the text file does not, in actual fact, have any tabs, it ends up reading the whole file into that one string.

BTW, what is the correct figure for the number of miles passed? Just curious.

miles would be 76.3

Hmmn, the reason I ask - well, one reason anyway - is because I think you have the wrong value for yards per mile. According to all of the sources I've checked, it should be 1760, not 1728. However, the program as I wrote it comes up with 70.79 miles when using the 1728 value, and only 69.5 miles when using the 1760 value. I'm trying to see where I got it wrong.

yeah you were right also it's not just reading in theteam name when calling for teams its reading part of it. i did a test just couting teams and its calling out the team name but also some of other values along with not the entire file though.

my program is going bonkers!!

Are you allowed to use the C++ STL in this program? (vector, list, etc.)

Are you allowed to use the C++ STL in this program? (vector, list, etc.)

no vectors, just arrays!

What about structures? I mentioned that it would make it easier to work with the data, as you'd have only one array to manipulate, pass to the functions and so on. The version of the program I wrote, for example, used an array of the following struct:

struct Team
{
    int rank;
    std::string name;
    int yards;
    float yards_avg;
    int passyards;
    float pass_avg;
    int rushyards;
    float rush_avg;
    int points;
    float points_avg;
};

This makes it a lot easier to work with, and to keep track of both the data you are using and that which you aren't (when reading in the data, that is).

im not too knowledgeable on structures but i'll give it a try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.