I am currently working on a C++ project that will calculate the passer rating for NFL QBs. The code is required to extract data from a txt file that is organized like this:
1 2 3 4 5 6
Andy Dalton CIN 252 410 2954 21 15
Andrew Luck IND 226 386 2593 15 7
Robert Griffin III WSH 222 372 2714 14 10
Alex Smith KC 235 398 2443 14 5
Carson Palmer ARI 250 395 2887 16 15
EJ Manuel BUF 127 217 1385 8 4
Basically, I need to extract the info from each column and use the values from each line to calculate the passer rating for each QB, then reorganize the values in a neat table. Right now, I am having trouble figuring out how I can extract the data from each line and save the values into different variables I can use to calculate the passer rating.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
string filename, firstName, lastName, fullName, teamABV, line;
ifstream qbinfo;
int completions, attempts, yards, touchdowns, interceptions;
int main()
{
cout << "This program will calculate the average passer rating based " << endl;
cout << "on data from a text file." << endl;
cout << "Please enter your full file's path." << endl;
cin >> filename;//Get file name for user input
qbinfo.open(filename.c_str());//Open input file
while (qbinfo.fail())//In case of error
{
cout << filename << " is an incorrect input. " << endl;
cout << "Please reenter your full file name and path." << endl;//Re-do
cin >> filename;
}
system("pause");
}
Unfortunately, this is all I have so far. I will note that, as for the format of the text file, columns 1-20 include the QB's name, 22-24, include the team abbreviation, and from 26 on is separated by white space. Can someone help me pass this road block?
Read it in, in one string, using the getline(). Then take out the first 20 letters, and this will be your QB's name. Then stick the rest into a stringstream and read it using the >> operator.
I'll explain more if you need elaboration, but it's probably faster for you to just look up what a stringstream is.